-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Expand file tree
/
Copy pathtest_setup_plan_python_parity.py
More file actions
430 lines (369 loc) 路 14.3 KB
/
Copy pathtest_setup_plan_python_parity.py
File metadata and controls
430 lines (369 loc) 路 14.3 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""Parity tests for the Python setup-plan port."""
from __future__ import annotations
from pathlib import Path
import pytest
from tests.conftest import requires_bash
from tests.parity_helpers import (
HAS_POWERSHELL,
POWERSHELL_EXE,
bash_cmd,
break_wrap_layer,
clean_env,
install_composition_stack,
install_scripts,
json_stdout,
make_repo,
normalize_repo_paths,
ps_cmd,
py_cmd,
run,
write_feature_json,
)
SCRIPT = "setup-plan"
TEMPLATE_BODY = "# Plan Template\n\nBody.\n"
def _setup_repo(tmp_path: Path, name: str = "proj", template: bool = True) -> Path:
repo = make_repo(tmp_path, name)
install_scripts(repo, SCRIPT)
write_feature_json(repo)
(repo / "specs" / "001-my-feature").mkdir(parents=True)
if template:
templates = repo / ".specify" / "templates"
templates.mkdir(parents=True)
(templates / "plan-template.md").write_text(TEMPLATE_BODY, encoding="utf-8")
return repo
@pytest.fixture
def repo(tmp_path: Path) -> Path:
return _setup_repo(tmp_path)
@requires_bash
def test_python_fresh_copy_matches_bash(tmp_path: Path) -> None:
repo_a = _setup_repo(tmp_path, "proj-a")
repo_b = _setup_repo(tmp_path, "proj-b")
bash = run(bash_cmd(repo_a, SCRIPT, "--json"), repo_a)
py = run(py_cmd(repo_b, SCRIPT, "--json"), repo_b)
assert py.returncode == bash.returncode == 0
assert normalize_repo_paths(py.stdout, repo_b) == normalize_repo_paths(
bash.stdout, repo_a
)
assert normalize_repo_paths(py.stderr, repo_b) == normalize_repo_paths(
bash.stderr, repo_a
)
for repo in (repo_a, repo_b):
plan = repo / "specs" / "001-my-feature" / "plan.md"
assert plan.read_bytes() == TEMPLATE_BODY.encode("utf-8")
@requires_bash
def test_all_variants_materialize_composed_plan_template(tmp_path: Path) -> None:
repos = [
_setup_repo(tmp_path, "bash"),
_setup_repo(tmp_path, "powershell"),
_setup_repo(tmp_path, "python"),
]
expected = ""
for current in repos:
expected = install_composition_stack(
current, "plan-template", TEMPLATE_BODY
)
results = [
run(bash_cmd(repos[0], SCRIPT, "--json"), repos[0]),
run(py_cmd(repos[2], SCRIPT, "--json"), repos[2]),
]
checked_repos = [repos[0], repos[2]]
if HAS_POWERSHELL:
results.insert(1, run(ps_cmd(repos[1], SCRIPT, "-Json"), repos[1]))
checked_repos.insert(1, repos[1])
assert all(result.returncode == 0 for result in results)
for current in checked_repos:
assert (
current / "specs" / "001-my-feature" / "plan.md"
).read_text(encoding="utf-8") == expected
@requires_bash
def test_all_variants_fail_for_broken_plan_composition(tmp_path: Path) -> None:
repos = [
_setup_repo(tmp_path, "bash"),
_setup_repo(tmp_path, "powershell"),
_setup_repo(tmp_path, "python"),
]
for current in repos:
install_composition_stack(current, "plan-template", TEMPLATE_BODY)
break_wrap_layer(current, "plan-template")
results = [
run(bash_cmd(repos[0], SCRIPT, "--json"), repos[0]),
run(py_cmd(repos[2], SCRIPT, "--json"), repos[2]),
]
if HAS_POWERSHELL:
results.append(run(ps_cmd(repos[1], SCRIPT, "-Json"), repos[1]))
assert all(result.returncode != 0 for result in results)
@requires_bash
@pytest.mark.parametrize("args", [("--json",), ()], ids=["json", "text"])
def test_python_existing_plan_matches_bash(repo: Path, args: tuple[str, ...]) -> None:
plan = repo / "specs" / "001-my-feature" / "plan.md"
plan.write_text("# existing\n", encoding="utf-8")
bash = run(bash_cmd(repo, SCRIPT, *args), repo)
py = run(py_cmd(repo, SCRIPT, *args), repo)
assert py.returncode == bash.returncode == 0
assert py.stdout == bash.stdout
assert py.stderr == bash.stderr
assert plan.read_text(encoding="utf-8") == "# existing\n"
@requires_bash
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
def test_all_variants_reject_unknown_options(tmp_path: Path) -> None:
repos = [
_setup_repo(tmp_path, "bash"),
_setup_repo(tmp_path, "powershell"),
_setup_repo(tmp_path, "python"),
]
bash = run(bash_cmd(repos[0], SCRIPT, "--json", "--bogus"), repos[0])
ps = run(ps_cmd(repos[1], SCRIPT, "-Json", "--bogus"), repos[1])
py = run(py_cmd(repos[2], SCRIPT, "--json", "--bogus"), repos[2])
assert bash.returncode == ps.returncode == py.returncode == 1
assert bash.stdout == ps.stdout == py.stdout == ""
assert bash.stderr == ps.stderr == py.stderr == (
"ERROR: Unknown option '--bogus'\n"
)
assert all(
not (current / "specs" / "001-my-feature" / "plan.md").exists()
for current in repos
)
@requires_bash
def test_python_missing_template_matches_bash(tmp_path: Path) -> None:
repo_a = _setup_repo(tmp_path, "proj-a", template=False)
repo_b = _setup_repo(tmp_path, "proj-b", template=False)
bash = run(bash_cmd(repo_a, SCRIPT, "--json"), repo_a)
py = run(py_cmd(repo_b, SCRIPT, "--json"), repo_b)
assert py.returncode == bash.returncode == 0
assert normalize_repo_paths(py.stderr, repo_b) == normalize_repo_paths(
bash.stderr, repo_a
)
for repo in (repo_a, repo_b):
plan = repo / "specs" / "001-my-feature" / "plan.md"
assert plan.read_text(encoding="utf-8") == ""
@requires_bash
@pytest.mark.parametrize(
("registry", "expected"),
[
(
'{"presets": {"alpha": {"priority": "high"}, "beta": {"priority": 1}}}',
"# beta plan\n",
),
(
'{"presets": {"alpha": {"priority": 2}, "beta": {"priority": 1}, "gamma": {"priority": null}}}',
"# beta plan\n",
),
("[]", "# alpha plan\n"),
('{"presets":[]}', "# alpha plan\n"),
('{"presets":null}', "# alpha plan\n"),
],
ids=[
"mixed_priorities",
"null_priority",
"list_root",
"list_presets",
"null_presets",
],
)
def test_all_variants_normalize_or_fallback_for_registry(
tmp_path: Path, registry: str, expected: str
) -> None:
"""Priorities normalize canonically; malformed shapes fall back to directories."""
repos = [
_setup_repo(tmp_path, "bash", template=False),
_setup_repo(tmp_path, "powershell", template=False),
_setup_repo(tmp_path, "python", template=False),
]
for repo in repos:
presets = repo / ".specify" / "presets"
for name, body in (
(".hidden", "# hidden\n"),
("beta", "# beta plan\n"),
("alpha", "# alpha plan\n"),
):
(presets / name / "templates").mkdir(parents=True)
(presets / name / "templates" / "plan-template.md").write_text(
body, encoding="utf-8"
)
(presets / ".registry").write_text(
registry, encoding="utf-8"
)
bash = run(bash_cmd(repos[0], SCRIPT, "--json"), repos[0])
py = run(py_cmd(repos[2], SCRIPT, "--json"), repos[2])
results = [(bash, repos[0]), (py, repos[2])]
if HAS_POWERSHELL:
results.insert(
1,
(run(ps_cmd(repos[1], SCRIPT, "-Json"), repos[1]), repos[1]),
)
assert all(result.returncode == 0 for result, _ in results)
assert len(
{
normalize_repo_paths(result.stdout, repo)
for result, repo in results
}
) == 1
assert len(
{
normalize_repo_paths(result.stderr, repo)
for result, repo in results
}
) == 1
for _, repo in results:
plan = repo / "specs" / "001-my-feature" / "plan.md"
assert plan.read_text(encoding="utf-8") == expected
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
def test_powershell_broken_registry_fallback_sorts_directories(
tmp_path: Path,
) -> None:
repo = _setup_repo(tmp_path, "powershell", template=False)
presets = repo / ".specify" / "presets"
for name in ("alpha", "beta"):
templates = presets / name / "templates"
templates.mkdir(parents=True)
(templates / "plan-template.md").write_text(
f"# {name} plan\n", encoding="utf-8"
)
(presets / ".registry").write_text("{broken", encoding="utf-8")
common = repo / ".specify" / "scripts" / "powershell" / "common.ps1"
common_ps = str(common).replace("'", "''")
alpha_ps = str(presets / "alpha").replace("'", "''")
beta_ps = str(presets / "beta").replace("'", "''")
repo_ps = str(repo).replace("'", "''")
command = f"""
. '{common_ps}'
function Get-ChildItem {{
@(
[PSCustomObject]@{{ Name = 'beta'; FullName = '{beta_ps}' }}
[PSCustomObject]@{{ Name = 'alpha'; FullName = '{alpha_ps}' }}
)
}}
Resolve-Template -TemplateName 'plan-template' -RepoRoot '{repo_ps}'
"""
result = run(
[POWERSHELL_EXE, "-NoProfile", "-Command", command],
repo,
)
assert result.returncode == 0
assert result.stderr == ""
assert Path(result.stdout.strip()).read_text(encoding="utf-8") == (
"# alpha plan\n"
)
@requires_bash
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
@pytest.mark.parametrize(
"context", ["missing", "invalid_json", "invalid_utf8", "invalid_init_dir"]
)
def test_all_variants_feature_context_error_matches(
tmp_path: Path, context: str
) -> None:
repo = make_repo(tmp_path)
install_scripts(repo, SCRIPT)
env = None
if context == "invalid_json":
(repo / ".specify" / "feature.json").write_text(
"{not json", encoding="utf-8"
)
elif context == "invalid_utf8":
(repo / ".specify" / "feature.json").write_bytes(b"\xff")
elif context == "invalid_init_dir":
env = clean_env()
env["SPECIFY_INIT_DIR"] = str(tmp_path / "missing")
bash = run(bash_cmd(repo, SCRIPT, "--json"), repo, env)
ps = run(ps_cmd(repo, SCRIPT, "-Json"), repo, env)
py = run(py_cmd(repo, SCRIPT, "--json"), repo, env)
assert bash.returncode == ps.returncode == py.returncode == 1
assert bash.stdout == ps.stdout == py.stdout == ""
assert bash.stderr == ps.stderr == py.stderr
@requires_bash
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
@pytest.mark.parametrize(
"registry",
[
'{"presets":{"alpha":{"enabled":false,"priority":1}}}',
'{"presets":{"alpha":"invalid"}}',
],
ids=["disabled", "invalid_metadata"],
)
def test_all_variants_ignore_inactive_preset_template(
tmp_path: Path, registry: str
) -> None:
repos = [
_setup_repo(tmp_path, "bash"),
_setup_repo(tmp_path, "powershell"),
_setup_repo(tmp_path, "python"),
]
for current in repos:
preset_templates = (
current / ".specify" / "presets" / "alpha" / "templates"
)
preset_templates.mkdir(parents=True)
(preset_templates / "plan-template.md").write_text(
"# Disabled preset\n", encoding="utf-8"
)
(current / ".specify" / "presets" / ".registry").write_text(
registry, encoding="utf-8"
)
bash = run(bash_cmd(repos[0], SCRIPT, "--json"), repos[0])
ps = run(ps_cmd(repos[1], SCRIPT, "-Json"), repos[1])
py = run(py_cmd(repos[2], SCRIPT, "--json"), repos[2])
assert bash.returncode == ps.returncode == py.returncode == 0
assert normalize_repo_paths(bash.stdout, repos[0]) == normalize_repo_paths(
ps.stdout, repos[1]
) == normalize_repo_paths(py.stdout, repos[2])
assert normalize_repo_paths(bash.stderr, repos[0]) == normalize_repo_paths(
ps.stderr, repos[1]
) == normalize_repo_paths(py.stderr, repos[2])
for current in repos:
assert (
current / "specs" / "001-my-feature" / "plan.md"
).read_text(encoding="utf-8") == TEMPLATE_BODY
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
def test_python_json_output_matches_powershell(repo: Path) -> None:
plan = repo / "specs" / "001-my-feature" / "plan.md"
plan.write_text("# existing\n", encoding="utf-8")
ps = run(ps_cmd(repo, SCRIPT, "-Json"), repo)
py = run(py_cmd(repo, SCRIPT, "--json"), repo)
assert py.returncode == ps.returncode == 0
assert json_stdout(py) == json_stdout(ps)
@requires_bash
@pytest.mark.parametrize("args", [("--json",), ()], ids=["json", "text"])
def test_all_variants_emit_feature_dir_not_specs_dir(
repo: Path, args: tuple[str, ...]
) -> None:
r"""Pin the output key name, not just cross-port agreement.
The other tests here compare the ports against each other, so all three
could regress to ``SPECS_DIR`` together and still pass. This asserts the
contract absolutely: the key is ``FEATURE_DIR``, it carries the feature
directory rather than the specs root, and the old name is gone.
``SPECS_DIR`` means the specs root in ``create-new-feature.sh``, so
re-emitting it here would reintroduce one name for two paths.
The value is matched by suffix because the ports legitimately differ in
path flavour -- under MSYS bash reports ``/tmp/...`` where the Python and
PowerShell ports report ``C:\...``. The suffix still separates
``specs/001-my-feature`` from a bare ``specs``, which is the regression
this guards.
"""
json_mode = args == ("--json",)
suffix = ("specs", "001-my-feature")
commands = [bash_cmd(repo, SCRIPT, *args), py_cmd(repo, SCRIPT, *args)]
if HAS_POWERSHELL:
commands.append(ps_cmd(repo, SCRIPT, *(("-Json",) if json_mode else ())))
for cmd in commands:
result = run(cmd, repo)
assert result.returncode == 0, result.stderr
assert "SPECS_DIR" not in result.stdout
if json_mode:
payload = json_stdout(result)
assert isinstance(payload, dict)
assert sorted(payload) == [
"BRANCH",
"FEATURE_DIR",
"FEATURE_SPEC",
"IMPL_PLAN",
]
value = payload["FEATURE_DIR"]
else:
lines = dict(
line.split(": ", 1)
for line in result.stdout.splitlines()
if ": " in line
)
assert "FEATURE_DIR" in lines
value = lines["FEATURE_DIR"]
assert tuple(value.replace("\\", "/").rstrip("/").split("/")[-2:]) == suffix