-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_e2e.py
More file actions
executable file
·268 lines (212 loc) · 8.28 KB
/
Copy pathtest_e2e.py
File metadata and controls
executable file
·268 lines (212 loc) · 8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
"""End-to-end tests for the cauldron CLI binary.
Runs the compiled CLI binary with various subcommands and verifies
that outputs and exit codes are correct. Uses a temporary directory
for all state so tests are isolated from any existing data.
Usage:
python3 scripts/test_e2e.py [--binary path/to/cauldron]
"""
import argparse
import os
import shutil
import subprocess
import sys
import tempfile
import textwrap
class TestRunner:
def __init__(self, binary: str):
self.binary = os.path.abspath(binary)
self.passed = 0
self.failed = 0
self.errors: list[str] = []
self.tmpdir = tempfile.mkdtemp(prefix="cauldron_e2e_")
self.db_path = os.path.join(self.tmpdir, "test.db")
def cleanup(self):
if os.path.exists(self.tmpdir):
shutil.rmtree(self.tmpdir)
def run(self, args: list[str], env_extra: dict | None = None,
expect_fail: bool = False) -> subprocess.CompletedProcess:
"""Run the cauldron binary with the given arguments."""
env = os.environ.copy()
# Override the data directory so we don't touch user data
env["XDG_DATA_HOME"] = self.tmpdir
env["HOME"] = self.tmpdir
if env_extra:
env.update(env_extra)
result = subprocess.run(
[self.binary] + args,
capture_output=True,
text=True,
env=env,
timeout=30,
cwd=self.tmpdir,
)
return result
def test(self, name: str, args: list[str], *,
expect_fail: bool = False,
expect_stdout: str | None = None,
expect_not_stdout: str | None = None,
expect_returncode: int | None = None):
"""Run a single test case."""
try:
result = self.run(args, expect_fail=expect_fail)
# Check return code
if expect_returncode is not None:
if result.returncode != expect_returncode:
self._fail(name, f"expected rc={expect_returncode}, got rc={result.returncode}\n"
f"stdout: {result.stdout[:500]}\nstderr: {result.stderr[:500]}")
return result
elif not expect_fail and result.returncode != 0:
self._fail(name, f"expected success but got rc={result.returncode}\n"
f"stderr: {result.stderr[:500]}")
return result
elif expect_fail and result.returncode == 0:
self._fail(name, "expected failure but got rc=0")
return result
# Check stdout contents
if expect_stdout and expect_stdout not in result.stdout:
self._fail(name, f"expected stdout to contain '{expect_stdout}'\n"
f"actual stdout: {result.stdout[:500]}")
return result
if expect_not_stdout and expect_not_stdout in result.stdout:
self._fail(name, f"expected stdout NOT to contain '{expect_not_stdout}'\n"
f"actual stdout: {result.stdout[:500]}")
return result
self._pass(name)
return result
except subprocess.TimeoutExpired:
self._fail(name, "timed out after 30 seconds")
return None
except Exception as e:
self._fail(name, f"exception: {e}")
return None
def _pass(self, name: str):
self.passed += 1
print(f" PASS {name}")
def _fail(self, name: str, reason: str):
self.failed += 1
self.errors.append(f"{name}: {reason}")
print(f" FAIL {name}")
for line in reason.strip().split("\n"):
print(f" {line}")
def summary(self):
total = self.passed + self.failed
print(f"\n{'='*60}")
print(f"Results: {self.passed}/{total} passed, {self.failed} failed")
if self.errors:
print(f"\nFailures:")
for err in self.errors:
print(f" - {err.split(chr(10))[0]}")
print(f"{'='*60}")
def find_binary() -> str:
"""Find the cauldron binary in typical build locations."""
candidates = [
"target/release/cauldron",
"target/debug/cauldron",
]
for c in candidates:
if os.path.isfile(c) and os.access(c, os.X_OK):
return c
return ""
def main():
parser = argparse.ArgumentParser(description="Cauldron CLI E2E tests")
parser.add_argument("--binary", default="",
help="Path to the cauldron binary (default: auto-detect)")
args = parser.parse_args()
binary = args.binary or find_binary()
if not binary or not os.path.isfile(binary):
print(f"ERROR: cauldron binary not found at '{binary}'")
print("Build it first with: cargo build --release -p cauldron-cli")
sys.exit(1)
print(f"Using binary: {binary}")
runner = TestRunner(binary)
try:
run_tests(runner)
finally:
runner.cleanup()
runner.summary()
sys.exit(1 if runner.failed > 0 else 0)
def run_tests(t: TestRunner):
print("\n--- Database Commands ---")
t.test("db init",
["db", "init", "--path", t.db_path],
expect_stdout="Database initialized")
t.test("db init idempotent",
["db", "init", "--path", t.db_path],
expect_stdout="Database initialized")
t.test("db seed",
["db", "seed", "--path", t.db_path],
expect_stdout="Seed data loaded")
# Query requires the default DB path to exist in the data dir
# We use the --path flag for init/seed but query uses default_db_path()
# So we also init+seed at the default location
default_db = os.path.join(t.tmpdir, "cauldron", "cauldron.db")
os.makedirs(os.path.dirname(default_db), exist_ok=True)
t.run(["db", "init", "--path", default_db])
t.run(["db", "seed", "--path", default_db])
t.test("db query existing game",
["db", "query", "1245620"],
expect_stdout="Elden Ring")
t.test("db query nonexistent game",
["db", "query", "99999"],
expect_stdout="No game found")
t.test("db recommend",
["db", "recommend", "1245620"],
expect_stdout="Recommended graphics backend")
print("\n--- Bottle Commands ---")
result = t.test("bottle create",
["bottle", "create", "E2E-Test-Bottle", "--wine-version", "wine-9.0"],
expect_stdout="Created bottle")
# Parse the bottle ID from output
bottle_id = None
if result and result.stdout:
for line in result.stdout.split("\n"):
if "ID:" in line:
bottle_id = line.split("ID:")[1].strip()
break
t.test("bottle list",
["bottle", "list"],
expect_stdout="E2E-Test-Bottle")
if bottle_id:
t.test("bottle delete",
["bottle", "delete", bottle_id],
expect_stdout="Deleted bottle")
else:
t.test("bottle delete (skipped - no ID)", ["--version"],
expect_stdout="cauldron")
t.test("bottle list after delete",
["bottle", "list"],
expect_stdout="No bottles found")
print("\n--- Wine Commands ---")
t.test("wine list",
["wine", "list"],
expect_stdout="Available Wine versions")
t.test("wine installed",
["wine", "installed"])
print("\n--- Performance Commands ---")
t.test("perf system-info",
["perf", "system-info"],
expect_stdout="System Information")
t.test("perf cache-list",
["perf", "cache-list"],
expect_stdout="No shader caches found")
print("\n--- Shell Completions ---")
t.test("completions zsh",
["completions", "zsh"])
t.test("completions bash",
["completions", "bash"])
print("\n--- Error Cases ---")
t.test("invalid subcommand",
["nonexistent"],
expect_fail=True)
t.test("bottle delete nonexistent",
["bottle", "delete", "nonexistent-id-12345"],
expect_fail=True)
t.test("missing required args",
["bottle", "create"],
expect_fail=True)
t.test("db query missing app_id",
["db", "query"],
expect_fail=True)
if __name__ == "__main__":
main()