-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cli_args.py
More file actions
376 lines (293 loc) · 13.1 KB
/
Copy pathtest_cli_args.py
File metadata and controls
376 lines (293 loc) · 13.1 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
Regression tests for CLI argument parity between claude-code-chat-browser and
cursor-chat-browser-python.
Every flag/default documented here must match the reference (cursor) project so
that users switching between the two tools experience zero CLI friction.
Run:
pytest tests/test_cli_args.py -v
"""
import ast
import sys
import os
import importlib
import argparse
import types
import pytest
# Ensure the repo root is on sys.path when tests are run from any directory.
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, REPO_ROOT)
from app import build_cli_parser
from scripts.export import build_parser
def _is_app_run_call(node: ast.AST) -> bool:
if not isinstance(node, ast.Call):
return False
func = node.func
return (
isinstance(func, ast.Attribute)
and func.attr == "run"
and isinstance(func.value, ast.Name)
and func.value.id == "app"
)
def _call_passes_hardcoded_debug_true(call: ast.Call) -> bool:
"""True if this Call passes literal True for debug (kwarg or positional)."""
for kw in call.keywords:
if kw.arg == "debug" and isinstance(kw.value, ast.Constant) and kw.value.value is True:
return True
for arg in call.args:
if isinstance(arg, ast.Constant) and arg.value is True:
return True
return False
def _debug_kwarg_uses_args(call: ast.Call) -> bool:
for kw in call.keywords:
if kw.arg != "debug":
continue
val = kw.value
return (
isinstance(val, ast.Attribute)
and isinstance(val.value, ast.Name)
and val.value.id == "args"
and val.attr == "debug"
)
return False
def _ast_mentions_args_debug(node: ast.AST) -> bool:
for child in ast.walk(node):
if (
isinstance(child, ast.Attribute)
and child.attr == "debug"
and isinstance(child.value, ast.Name)
and child.value.id == "args"
):
return True
return False
def _use_reloader_kwarg_tied_to_debug(call: ast.Call) -> bool:
for kw in call.keywords:
if kw.arg == "use_reloader":
return _ast_mentions_args_debug(kw.value)
return False
# ---------------------------------------------------------------------------
# export.py argument tests
# ---------------------------------------------------------------------------
class TestExportParserFlags:
"""Every flag that cursor's export.py exposes must also exist here."""
def setup_method(self):
self.parser = build_parser()
def _parse(self, argv: list[str]) -> argparse.Namespace:
return self.parser.parse_args(argv)
# -- --since ----------------------------------------------------------------
def test_since_defaults_to_none_at_top_level(self):
args = self._parse([])
assert args.since is None # default; cmd_export normalises to "all"
def test_since_all(self):
args = self._parse(["--since", "all"])
assert args.since == "all"
def test_since_last(self):
args = self._parse(["--since", "last"])
assert args.since == "last"
def test_since_invalid_value_raises(self):
with pytest.raises(SystemExit):
self._parse(["--since", "yesterday"])
def test_since_subcommand_default_is_all(self):
args = self._parse(["export"])
assert args.since == "all"
def test_since_subcommand_last(self):
args = self._parse(["export", "--since", "last"])
assert args.since == "last"
def test_since_incremental(self):
args = self._parse(["--since", "incremental"])
assert args.since == "incremental"
def test_since_before_export_subcommand_recovered(self):
"""Flags before ``export`` must not be lost to subparser defaults."""
from scripts import export as export_mod
argv = ["--since", "last", "export"]
args = self._parse(argv)
assert args.since == "all" # argparse quirk without recovery
for k, v in export_mod._prefixed_export_option_overrides(argv).items():
setattr(args, k, v)
assert args.since == "last"
def test_since_incremental_before_export_recovered(self):
from scripts import export as export_mod
argv = ["--since", "incremental", "export"]
args = self._parse(argv)
assert args.since == "all"
for k, v in export_mod._prefixed_export_option_overrides(argv).items():
setattr(args, k, v)
assert args.since == "incremental"
def test_prefixed_out_before_export(self):
from scripts import export as export_mod
argv = ["--out", "/tmp/z", "export"]
args = self._parse(argv)
assert args.out is None
for k, v in export_mod._prefixed_export_option_overrides(argv).items():
setattr(args, k, v)
assert args.out == "/tmp/z"
# -- --out ------------------------------------------------------------------
def test_out_default_is_none(self):
args = self._parse([])
assert args.out is None # cmd_export normalises to os.getcwd()
def test_out_explicit(self):
args = self._parse(["--out", "/tmp/exports"])
assert args.out == "/tmp/exports"
def test_out_subcommand(self):
args = self._parse(["export", "--out", "/tmp/exports"])
assert args.out == "/tmp/exports"
# -- --no-zip ---------------------------------------------------------------
def test_no_zip_default_false(self):
args = self._parse([])
assert args.no_zip is False
def test_no_zip_flag(self):
args = self._parse(["--no-zip"])
assert args.no_zip is True
def test_no_zip_subcommand(self):
args = self._parse(["export", "--no-zip"])
assert args.no_zip is True
# -- --exclude-rules / -e (cursor parity) ----------------------------------
def test_exclude_rules_long_form_default_none(self):
args = self._parse([])
assert args.exclude_rules is None
def test_exclude_rules_long_form(self):
args = self._parse(["--exclude-rules", "/path/to/rules.txt"])
assert args.exclude_rules == "/path/to/rules.txt"
def test_exclude_rules_short_form(self):
args = self._parse(["-e", "/path/to/rules.txt"])
assert args.exclude_rules == "/path/to/rules.txt"
def test_exclude_rules_subcommand_long(self):
args = self._parse(["export", "--exclude-rules", "/path/rules.txt"])
assert args.exclude_rules == "/path/rules.txt"
def test_exclude_rules_subcommand_short(self):
args = self._parse(["export", "-e", "/path/rules.txt"])
assert args.exclude_rules == "/path/rules.txt"
# -- --base-dir -------------------------------------------------------------
def test_base_dir_default_none(self):
args = self._parse([])
assert args.base_dir is None
def test_base_dir_explicit(self):
args = self._parse(["--base-dir", "/home/user/.claude/projects"])
assert args.base_dir == "/home/user/.claude/projects"
def test_base_dir_subcommand(self):
args = self._parse(["export", "--base-dir", "/home/user/.claude/projects"])
assert args.base_dir == "/home/user/.claude/projects"
# -- --format ---------------------------------------------------------------
def test_format_default_none_at_top_level(self):
args = self._parse([])
assert args.format is None # cmd_export normalises to "md"
def test_format_md(self):
args = self._parse(["--format", "md"])
assert args.format == "md"
def test_format_json(self):
args = self._parse(["--format", "json"])
assert args.format == "json"
def test_format_both(self):
args = self._parse(["--format", "both"])
assert args.format == "both"
def test_format_invalid_raises(self):
with pytest.raises(SystemExit):
self._parse(["--format", "csv"])
# -- subcommand dispatch ----------------------------------------------------
def test_no_subcommand_command_attr_is_none(self):
args = self._parse([])
assert getattr(args, "command", None) is None
def test_list_subcommand(self):
args = self._parse(["list"])
assert args.command == "list"
def test_stats_subcommand(self):
args = self._parse(["stats"])
assert args.command == "stats"
def test_export_subcommand(self):
args = self._parse(["export"])
assert args.command == "export"
# -- --help does not raise (just exits 0) -----------------------------------
def test_help_exits_zero(self):
with pytest.raises(SystemExit) as exc_info:
self._parse(["--help"])
assert exc_info.value.code == 0
# ---------------------------------------------------------------------------
# app.py argument tests
# ---------------------------------------------------------------------------
class TestAppArgparse:
"""app.py CLI must expose the same flags as cursor's app.py."""
def test_host_default_is_localhost(self):
"""Default host must be 127.0.0.1 to match cursor which binds to localhost only."""
parser = build_cli_parser()
args = parser.parse_args([])
assert args.host == "127.0.0.1"
def test_host_override(self):
parser = build_cli_parser()
args = parser.parse_args(["--host", "127.0.0.1"])
assert args.host == "127.0.0.1"
def test_debug_default_is_false(self):
parser = build_cli_parser()
args = parser.parse_args([])
assert args.debug is False
def test_debug_explicit_true(self):
parser = build_cli_parser()
args = parser.parse_args(["--debug"])
assert args.debug is True
def test_app_py_debug_not_hardcoded_true(self):
"""app.run() must wire debug from args, not a literal True."""
app_path = os.path.join(REPO_ROOT, "app.py")
with open(app_path, encoding="utf-8") as f:
tree = ast.parse(f.read(), filename=app_path)
app_run_calls = [n for n in ast.walk(tree) if _is_app_run_call(n)]
assert app_run_calls, "expected at least one app.run() call in app.py"
assert not any(_call_passes_hardcoded_debug_true(c) for c in app_run_calls)
assert any(_debug_kwarg_uses_args(c) for c in app_run_calls)
def test_port_default(self):
parser = build_cli_parser()
args = parser.parse_args([])
assert args.port == 5000
def test_port_override(self):
parser = build_cli_parser()
args = parser.parse_args(["--port", "8080"])
assert args.port == 8080
def test_base_dir_default_none(self):
parser = build_cli_parser()
args = parser.parse_args([])
assert args.base_dir is None
def test_base_dir_override(self):
parser = build_cli_parser()
args = parser.parse_args(["--base-dir", "/tmp/projects"])
assert args.base_dir == "/tmp/projects"
def test_exclude_rules_default_none(self):
parser = build_cli_parser()
args = parser.parse_args([])
assert args.exclude_rules is None
def test_exclude_rules_long_form(self):
parser = build_cli_parser()
args = parser.parse_args(["--exclude-rules", "/tmp/rules.txt"])
assert args.exclude_rules == "/tmp/rules.txt"
def test_exclude_rules_short_form(self):
"""Cursor's app.py uses -e as the short form; claude must too."""
parser = build_cli_parser()
args = parser.parse_args(["-e", "/tmp/rules.txt"])
assert args.exclude_rules == "/tmp/rules.txt"
def test_app_py_actual_argparse_has_exclude_rules(self):
"""Smoke-test: import app module and verify argparse accepts -e."""
result = os.popen(
f'{sys.executable} -c "'
'import sys, os; sys.path.insert(0, os.path.abspath(\\\".\\\"));"'
)
# Lightweight check: parse the app.py source for the flag definition
app_path = os.path.join(REPO_ROOT, "app.py")
with open(app_path, "r", encoding="utf-8") as f:
src = f.read()
assert '"--exclude-rules"' in src or "'--exclude-rules'" in src
assert '"-e"' in src
def test_app_py_host_default_is_localhost(self):
"""app.py source must declare 127.0.0.1 as the --host default, matching cursor."""
app_path = os.path.join(REPO_ROOT, "app.py")
with open(app_path, "r", encoding="utf-8") as f:
src = f.read()
assert '"127.0.0.1"' in src
def test_app_py_use_reloader_is_platform_aware(self):
"""use_reloader must be opt-in via --debug and gated on non-Windows platforms."""
app_path = os.path.join(REPO_ROOT, "app.py")
with open(app_path, encoding="utf-8") as f:
tree = ast.parse(f.read(), filename=app_path)
app_run_calls = [n for n in ast.walk(tree) if _is_app_run_call(n)]
assert app_run_calls
assert all(_use_reloader_kwarg_tied_to_debug(c) for c in app_run_calls)
with open(app_path, encoding="utf-8") as f:
src = f.read()
assert "sys.platform" in src
assert "win32" in src
assert "use_reloader=False" not in src