Repository navigation
Expand file tree
/
Copy pathtest_cli.py
More file actions
795 lines (655 loc) · 26.1 KB
/
Copy pathtest_cli.py
File metadata and controls
795 lines (655 loc) · 26.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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
8000
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
"""Tests for the CLI seam (`minibwa_dist.cli.main`), using synthetic repos.
No `gh` calls and no network here -- these exercise argument parsing and the
glue between `assemble()`/`stamp_dev_version()` and the process exit code, the
same way `distro-sync.yml` invokes `python -m minibwa_dist.cli sync`.
"""
from __future__ import annotations
import json
import shutil
from pathlib import Path
import pytest
import minibwa_dist.cli as cli_module
from minibwa_dist.cli import main
from minibwa_dist.manifest import load_manifest
from minibwa_dist.render import BLOCK_BEGIN
from minibwa_dist.tests.conftest import (
branch_touching,
commit_file,
conflicting_branch,
run,
stub_aligner,
)
_ONE_FEATURE = """
[[feature]]
name = "{name}"
branch = "feat/{name}"
required = {required}
output = "identical"
summary = "{name}"
upstream = {{ status = "unsubmitted" }}
"""
def _write_manifest(tmp_path: Path, features_toml: str) -> Path:
path = tmp_path / "features.toml"
path.write_text(features_toml)
return path
def _feature_toml(name: str, *, required: bool = False) -> str:
return _ONE_FEATURE.format(name=name, required="true" if required else "false")
def _sync_argv(manifest: Path, repo: Path, *extra: str) -> list[str]:
return [
"--manifest",
str(manifest),
"sync",
"--repo",
str(repo),
"--base",
"master",
"--out",
"dist-next",
"--remote",
"fork",
*extra,
]
def test_sync_cold_assembly_merges_disjoint_features(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A fresh repo, no rerere cache: the ordinary cold-start path."""
branch_touching(repo, "feat/a", "a.c", "a\n")
branch_touching(repo, "feat/b", "b.c", "b\n")
base_sha = run(repo, "rev-parse", "master")
manifest = _write_manifest(
tmp_path,
8000
"""
[[feature]]
name = "a"
branch = "feat/a"
required = false
output = "identical"
summary = "a"
upstream = { status = "unsubmitted" }
[[feature]]
name = "b"
branch = "feat/b"
required = false
output = "identical"
summary = "b"
upstream = { status = "unsubmitted" }
""",
)
rc = main(_sync_argv(manifest, repo))
assert rc == 0
out = json.loads(capsys.readouterr().out)
assert out["merged"] == ["a", "b"]
assert out["dropped"] == []
assert out["version"] is None
assert out["base"] == base_sha, (
"the release job proves a tag matches the tree it names by reading this field "
"back out of dist-manifest.json"
)
run(repo, "checkout", "-q", "dist-next")
assert (repo / "a.c").exists() and (repo / "b.c").exists()
def test_sync_drops_an_optional_feature_and_reports_it(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
conflicting_branch(repo, "feat/x")
manifest = _write_manifest(
tmp_path,
"""
[[feature]]
name = "x"
branch = "feat/x"
required = false
output = "identical"
summary = "x"
upstream = { status = "unsubmitted" }
""",
)
rc = main(_sync_argv(manifest, repo))
assert rc == 0
out = json.loads(capsys.readouterr().out)
assert out["merged"] == []
assert out["dropped"] == [{"name": "x", "files": ["src.c"]}]
def test_sync_with_stamp_produces_the_expected_version_string(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
commit_file(repo, "minibwa.h", '#define MB_VERSION "0.6-r416"\n', "add header")
expected_sha = run(repo, "rev-parse", "--short", "master")
manifest = _write_manifest(tmp_path, "")
rc = main(_sync_argv(manifest, repo, "--stamp"))
assert rc == 0
out = json.loads(capsys.readouterr().out)
assert out["version"] == f"0.6-nh13.dev+{expected_sha}"
run(repo, "checkout", "-q", "dist-next")
assert (repo / "minibwa.h").read_text() == f'#define MB_VERSION "{out["version"]}"\n'
def test_sync_stop_at_exits_zero_and_explains_training_mode(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""S10: the expected `--stop-at` outcome must not read as a build failure."""
conflicting_branch(repo, "feat/x")
manifest = _write_manifest(
tmp_path,
"""
[[feature]]
name = "x"
branch = "feat/x"
required = false
output = "identical"
summary = "x"
upstream = { status = "unsubmitted" }
""",
)
rc = main(_sync_argv(manifest, repo, "--stop-at", "x"))
assert rc == 0
err = capsys.readouterr().err
assert "stopped at 'x' for rerere training" in err
assert "ASSEMBLY FAILED" not in err
def test_sync_required_conflict_still_fails_even_with_stop_at_set(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A genuine required-feature conflict must still exit 1, even when
`--stop-at` names a different (later, never-reached) feature.
`later` is a real manifest entry, ordered after the required feature that
fails: `--stop-at` is now validated against the manifest up front, so a name
that matches nothing tests the typo path (below), not this one.
"""
conflicting_branch(repo, "feat/req")
branch_touching(repo, "feat/later", "later.c", "later\n")
manifest = _write_manifest(
tmp_path, _feature_toml("req", required=True) + _feature_toml("later")
)
rc = main(_sync_argv(manifest, repo, "--stop-at", "later"))
assert rc == 1
err = capsys.readouterr().err
assert "ASSEMBLY FAILED" in err
assert "required feature 'req'" in err
assert run(repo, "rev-parse", "--abbrev-ref", "HEAD") == "master", (
"a failed build must hand the worktree back, whether or not --stop-at was set"
)
def test_sync_rejects_a_stop_at_that_names_no_feature(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A typo used to run an ordinary full build, drop the feature the maintainer
meant to train on, and exit 0 -- the opposite of what was asked, silently.
"""
conflicting_branch(repo, "feat/x")
manifest = _write_manifest(tmp_path, _feature_toml("x"))
rc = main(_sync_argv(manifest, repo, "--stop-at", "x-typo"))
assert rc == 1
err = capsys.readouterr().err
assert "ASSEMBLY FAILED" in err
assert "names no feature" in err
assert "valid names: x" in err
def test_sync_missing_branch_exits_one(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""Branch custody at the CLI seam: distro-sync.yml relies on this exit code
to stop before it builds, gates and pushes a tree assembled without it.
"""
manifest = _write_manifest(tmp_path, _feature_toml("gone"))
rc = main(_sync_argv(manifest, repo))
assert rc == 1
assert "ASSEMBLY FAILED" in capsys.readouterr().err
def test_sync_training_mode_names_the_remote_to_publish_the_cache_to(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""`save()` runs at the moment of the conflict, so it captures a pre-image and
nothing else: the resolution only reaches CI if the maintainer re-runs sync
and pushes. And `origin` is UPSTREAM in a local checkout, so the follow-up
has to name the remote the caller actually chose.
"""
conflicting_branch(repo, "feat/x")
manifest = _write_manifest(tmp_path, _feature_toml("x"))
assert main(_sync_argv(manifest, repo, "--stop-at", "x")) == 0
err = capsys.readouterr().err
assert "git push fork rerere-cache" in err
assert "Re-run this same sync" in err
def test_sync_restores_and_then_replays_the_rerere_cache_branch(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""The cache wiring in `_sync`: save-on-the-way-out, restore-on-the-way-in.
Deleting either call is invisible to a suite that only tests
`rerere_cache.save`/`restore` in isolation -- the nightly sync still exits 0
and still emits JSON, it just re-hits every conflict and drops the feature.
So drive it the way a maintainer does: train, resolve, sync once to persist
the completed resolution onto the cache branch, wipe the local `rr-cache`
the way a fresh runner has it, and sync again.
"""
conflicting_branch(repo, "feat/x")
manifest = _write_manifest(tmp_path, _feature_toml("x"))
assert main(_sync_argv(manifest, repo, "--stop-at", "x")) == 0
(repo / "src.c").write_text("line1\nRESOLVED\n")
run(repo, "add", "src.c")
run(repo, "-c", "commit.gpgsign=false", "commit", "--no-edit")
run(repo, "reset", "--hard", "HEAD~1")
run(repo, "checkout", "-q", "master")
# A normal sync: this is the run whose `save()` sees a complete resolution.
assert main(_sync_argv(manifest, repo)) == 0
assert json.loads(capsys.readouterr().out)["merged"] == ["x"], "rerere should replay locally"
assert run(repo, "rev-parse", "--verify", "refs/heads/rerere-cache"), "save() must have run"
# Now be a cold runner: the cache branch exists, the local cache does not.
cache = Path(run(repo, "rev-parse", "--path-format=absolute", "--git-common-dir")) / "rr-cache"
shutil.rmtree(cache)
assert main(_sync_argv(manifest, repo)) == 0
out = capsys.readouterr()
assert json.loads(out.out)["merged"] == ["x"], (
"with no local rr-cache the feature can only merge if restore() repopulated it"
)
assert "rerere: restored 1 resolution(s)" in out.err
def test_sync_reports_orphan_over_published_cache_instead_of_a_traceback(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A fresh `git clone` is exactly the state `OrphanOverPublishedCache`
exists to catch: the cache branch is published upstream (a remote-tracking
ref exists), but this clone's `git fetch` never landed it locally as
`refs/heads/rerere-cache`. `_sync` calls `rerere_cache.save()`
unconditionally from its `finally`, so that exception used to propagate
straight out of `main()` as an unhandled traceback -- discarding the JSON
of an assembly that had actually succeeded. `main()` must turn it into an
actionable message and exit 1 instead.
"""
rr = repo / ".git" / "rr-cache" / "aaaa1111"
rr.mkdir(parents=True)
(rr / "postimage").write_text("some resolution\n")
tip = run(repo, "rev-parse", "master")
run(repo, "update-ref", "refs/remotes/fork/rerere-cache", tip)
manifest = _write_manifest(tmp_path, "")
rc = main(_sync_argv(manifest, repo))
assert rc == 1
err = capsys.readouterr().err
assert "rerere-cache" in err
assert "fetch" in err.lower(), "the message must say to fetch the branch, not just fail"
def test_reconcile_write_emits_a_manifest_that_loads(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""What the bot commits, the engine must be able to read.
`distro-reconcile.yml` runs exactly this, then commits, force-pushes and
opens a PR -- with no step that tries to load the result. A graduated
feature written back as `status = "merged"` therefore shipped a manifest
nothing could parse, and merging that PR wedged sync, reconcile and test.
"""
manifest = _write_manifest(
tmp_path,
"""
# --- a heading that introduces the block below ---
[[feature]]
name = "graduate"
branch = "feat/graduate"
required = false
output = "identical"
summary = "upstream took this one"
upstream = { pr = 33, status = "open" }
# --- a heading that must survive the deletion above ---
[[feature]]
name = "stays"
branch = "feat/stays"
required = false
output = "identical"
summary = "still ours"
upstream = { pr = 34, status = "open" }
""",
)
states = tmp_path / "prs.json"
states.write_text(json.dumps({"33": "merged", "34": "closed"}))
rc = main(["--manifest", str(manifest), "reconcile", "--pr-states", str(states), "--write"])
assert rc == 0
out = json.loads(capsys.readouterr().out)
assert out["graduated"] == ["graduate"]
reloaded = load_manifest(manifest)
assert [f.name for f in reloaded.features] == ["stays"]
assert reloaded.features[0].upstream.status == "rejected"
assert "a heading that must survive the deletion above" in manifest.read_text()
def test_reconcile_write_refuses_a_rewrite_it_cannot_load_back(
tmp_path: Path, capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch
) -> None:
"""`apply_changes` is tested elsewhere to always hand back loadable text,
but `_reconcile` must not simply trust that invariant at the one point it
is about to become a committed file: `distro-reconcile.yml` commits,
force-pushes and opens a PR with no step that loads the result, so this
check is the only thing standing between a bad rewrite and a manifest
nothing can parse landing in git history.
Simulated by monkeypatching `apply_changes` to hand back malformed text --
not because today's `apply_changes` is known to produce it, but because
the CLI's own refusal must not depend on that ever remaining true.
"""
monkeypatch.setattr(cli_module, "apply_changes", lambda text, changes: "not [valid toml")
manifest = _write_manifest(
tmp_path,
_feature_toml("a").replace(
'upstream = { status = "unsubmitted" }', 'upstream = { pr = 33, status = "open" }'
),
)
original = manifest.read_text()
states = tmp_path / "prs.json"
states.write_text(json.dumps({"33": "closed"}))
rc = main(["--manifest", str(manifest), "reconcile", "--pr-states", str(states), "--write"])
assert rc == 1
assert "ERROR" in capsys.readouterr().err
assert manifest.read_text() == original, (
"a rewrite that cannot be read back must not be written"
)
def test_reconcile_without_write_leaves_the_manifest_untouched(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""The read-only preview a maintainer runs first must stay read-only."""
text = _feature_toml("a").replace(
'upstream = { status = "unsubmitted" }', 'upstream = { pr = 33, status = "open" }'
)
manifest = _write_manifest(tmp_path, text)
states = tmp_path / "prs.json"
states.write_text(json.dumps({"33": "closed"}))
rc = main(["--manifest", str(manifest), "reconcile", "--pr-states", str(states)])
assert rc == 0
assert manifest.read_text() == text, "no --write means no write"
out = json.loads(capsys.readouterr().out)
assert out["changes"] == [{"feature": "a", "old": "open", "new": "rejected"}]
def test_notes_render_the_dropped_section(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
manifest = _write_manifest(tmp_path, _feature_toml("a"))
upstream = tmp_path / "upstream.md"
upstream.write_text("upstream said things")
rc = main(
[
"--manifest",
str(manifest),
"notes",
"--upstream-notes",
str(upstream),
"--upstream-tag",
"v0.6",
"--dropped",
"a",
]
)
assert rc == 0
out = capsys.readouterr().out
assert "upstream said things" in out
assert "not included" in out.lower() and "`a`" in out
8BD0
div>
def test_notes_render_an_untagged_base(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
manifest = _write_manifest(tmp_path, _feature_toml("a"))
upstream = tmp_path / "upstream.md"
upstream.write_text("- r427: fixed a bug (74bdd48)")
sha = "74bdd485f2517fdd322faa1fd8eb36ecf297743d"
rc = main(
[
"--manifest",
str(manifest),
"notes",
"--upstream-notes",
str(upstream),
"--upstream-tag",
"v0.7",
"--upstream-ref",
sha,
]
)
assert rc == 0
out = capsys.readouterr().out
assert f"/commit/{sha}" in out and "since v0.7" in out
def test_upstream_version_prints_the_version_at_a_rev(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
commit_file(repo, "minibwa.h", '#define MB_VERSION "0.7-r427-dirty"\n', "r427")
manifest = _write_manifest(tmp_path, "")
rc = main(
[
"--manifest",
str(manifest),
"upstream-version",
"--repo",
str(repo),
"--rev",
"HEAD",
"--nearest-tag",
"v0.7",
]
)
assert rc == 0
assert capsys.readouterr().out == "0.7-r427\n"
def test_upstream_version_exits_nonzero_on_a_maj_min_mismatch(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""The workflow captures stdout with `$(...)`; a failure must be an exit
code, not a version string that happens to be wrong.
"""
commit_file(repo, "minibwa.h", '#define MB_VERSION "0.8-r430-dirty"\n', "bump early")
manifest = _write_manifest(tmp_path, "")
rc = main(
[
"--manifest",
str(manifest),
"upstream-version",
"--repo",
str(repo),
"--rev",
"HEAD",
"--nearest-tag",
"v0.7",
]
)
assert rc == 1
captured = capsys.readouterr()
assert captured.out == "" and "nearest upstream tag" in captured.err
def test_docs_writes_the_readme_block_and_the_graveyard(tmp_path: Path) -> None:
manifest = _write_manifest(tmp_path, _feature_toml("a"))
readme = tmp_path / "README.md"
readme.write_text("# minibwa\n\nUpstream prose.\n")
graveyard = tmp_path / "GRAVEYARD.md"
rc = main(
[
"--manifest",
str(manifest),
"docs",
"--readme",
str(readme),
"--graveyard",
str(graveyard),
]
)
assert rc == 0
assert BLOCK_BEGIN in readme.read_text() and "Upstream prose." in readme.read_text()
assert graveyard.read_text().startswith("# Graveyard")
def test_stamp_sets_an_exact_version(repo: Path, tmp_path: Path) -> None:
"""The release workflow's version rewrite, no longer a second copy in sed."""
(repo / "minibwa.h").write_text('#define MB_VERSION "0.6-dev+abc123"\n')
manifest = _write_manifest(tmp_path, "")
rc = main(
["--manifest", str(manifest), "stamp", "--repo", str(repo), "--version", "0.6-nh13.1"]
)
assert rc == 0
assert (repo / "minibwa.h").read_text() == '#define MB_VERSION "0.6-nh13.1"\n'
_stub_aligner = stub_aligner
def test_gates_subcommand_exits_nonzero_when_the_sam_differs(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""The gate's verdict is the whole point of the step, and it used to live as
two hand-copied heredocs no linter or test could reach.
"""
fixtures = tmp_path / "fixtures"
fixtures.mkdir()
for name in ("chrM-human.fa.gz", "chrM-read_1.fa.gz", "chrM-read_2.fa.gz"):
(fixtures / name).write_bytes(b"")
stock = _stub_aligner(tmp_path / "stock", "@HD\tVN:1.6\nr1\t0\tchrM\t1\t60\t5M\n")
same = _stub_aligner(tmp_path / "same", "@HD\tVN:1.6\nr1\t0\tchrM\t1\t60\t5M\n")
different = _stub_aligner(tmp_path / "different", "@HD\tVN:1.6\nr1\t0\tchrM\t1\t42\t5M\n")
manifest = _write_manifest(tmp_path, _feature_toml("a"))
def gates(candidate: Path, workdir: str) -> int:
return main(
[
"--manifest",
str(manifest),
"gates",
10000
"--candidate",
str(candidate),
"--stock",
str(stock),
"--fixtures",
str(fixtures),
"--workdir",
str(tmp_path / workdir),
"--repo",
str(tmp_path),
]
)
assert gates(same, "pass") == 0
assert "PASS default-flags-byte-identity" in capsys.readouterr().out
assert gates(different, "fail") == 1
assert "FAIL default-flags-byte-identity" in capsys.readouterr().out
def test_gates_scope_coverage_to_the_features_this_build_contains(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A dropped feature is not in the binary, so naming it as covered overstates
what the one trustworthy artifact proved -- and contradicts the drop list the
same job publishes.
"""
fixtures = tmp_path / "fixtures"
fixtures.mkdir()
for name in ("chrM-human.fa.gz", "chrM-read_1.fa.gz", "chrM-read_2.fa.gz"):
(fixtures / name).write_bytes(b"")
binary = _stub_aligner(tmp_path / "aligner", "@HD\tVN:1.6\n")
manifest = _write_manifest(tmp_path, _feature_toml("kept") + _feature_toml("dropped"))
assembly = tmp_path / "assembly.json"
assembly.write_text(json.dumps({"merged": ["kept"], "dropped": [{"name": "dropped"}]}))
rc = main(
[
"--manifest",
str(manifest),
"gates",
"--candidate",
str(binary),
"--stock",
str(binary),
"--fixtures",
str(fixtures),
"--workdir",
str(tmp_path / "work"),
"--assembly",
str(assembly),
"--repo",
str(tmp_path),
]
)
assert rc == 0
detail = capsys.readouterr().out
assert "1 feature(s) covered: kept" in detail
assert "1 not in this build: dropped" in detail
# --- a feature that merged in the previous build and does not now ---
def _record_previous_build(repo: Path, ref: str, merged: list[str]) -> None:
"""Commit a prior build's assembly JSON on `ref`, the way sync itself does."""
run(repo, "checkout", "-q", "-B", ref, "master")
commit_file(repo, "dist-manifest.json", json.dumps({"merged": merged, "dropped": []}), "record")
run(repo, "checkout", "-q", "master")
_ONE_CONFLICTING_FEATURE = """
[[feature]]
name = "x"
branch = "feat/x"
required = false
output = "identical"
summary = "x"
upstream = { status = "unsubmitted" }
"""
def test_sync_fails_when_a_feature_stops_merging(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""The drop is a regression: the previous build had it, this one does not.
Exiting zero here is what let a real drop pass unnoticed -- the build then
matched `dist`, the caller short-circuited on "nothing to ship", and the
open sync PR kept advertising the feature.
"""
conflicting_branch(repo, "feat/x")
_record_previous_build(repo, "dist-prev", ["x"])
manifest = _write_manifest(tmp_path, _ONE_CONFLICTING_FEATURE)
rc = main(_sync_argv(manifest, repo, "--regression-baseline", "dist-prev"))
assert rc == 1
captured = capsys.readouterr()
assert json.loads(captured.out)["regressed"] == ["x"]
assert "x" in captured.err
def test_sync_still_emits_the_assembly_json_when_it_fails(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""The caller redirects stdout to a file and reads it after the step fails."""
conflicting_branch(repo, "feat/x")
_record_previous_build(repo, "dist-prev", ["x"])
manifest = _write_manifest(tmp_path, _ONE_CONFLICTING_FEATURE)
main(_sync_argv(manifest, repo, "--regression-baseline", "dist-prev"))
out = json.loads(capsys.readouterr().out)
assert [d["name"] for d in out["dropped"]] == ["x"]
assert out["merged"] == []
def test_sync_succeeds_when_a_never_merged_feature_is_dropped_again(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A feature that did not merge last time either is not a regression."""
conflicting_branch(repo, "feat/x")
_record_previous_build(repo, "dist-prev", [])
manifest = _write_manifest(tmp_path, _ONE_CONFLICTING_FEATURE)
rc = main(_sync_argv(manifest, repo, "--regression-baseline", "dist-prev"))
assert rc == 0
assert json.loads(capsys.readouterr().out)["regressed"] == []
def test_sync_succeeds_with_no_baseline_ref(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""First run: nothing to compare against, so nothing can have regressed."""
conflicting_branch(repo, "feat/x")
manifest = _write_manifest(tmp_path, _ONE_CONFLICTING_FEATURE)
rc = main(_sync_argv(manifest, repo, "--regression-baseline", "no-such-branch"))
assert rc == 0
assert json.loads(capsys.readouterr().out)["regressed"] == []
def test_sync_without_the_flag_reports_no_regressions(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""The flag is opt-in; omitting it must not change the exit code."""
conflicting_branch(repo, "feat/x")
_record_previous_build(repo, "dist-prev", ["x"])
manifest = _write_manifest(tmp_path, _ONE_CONFLICTING_FEATURE)
rc = main(_sync_argv(manifest, repo))
assert rc == 0
assert json.loads(capsys.readouterr().out)["regressed"] == []
def test_gates_without_repo_reports_that_the_suites_did_not_run(
repo: Path, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""`--repo` must stay optional, and its absence must be visible.
The engine and the workflow file update at different times: every sync
checks out the freshly assembled `dist-next` and runs THAT `minibwa_dist`,
while the YAML executing comes from `dist` and only changes when a build is
shipped. So a new required argument is demanded by the engine one sync
before the workflow can supply it, and the gates step dies on argparse
before the ship that would fix it. Optional-and-loud is the only shape that
survives that gap.
"""
manifest = _write_manifest(
tmp_path,
"""
[[feature]]
name = "x"
branch = "feat/x"
required = false
output = "identical"
tests = "test/x"
summary = "x"
upstream = { status = "unsubmitted" }
""",
)
sam = "@SQ\tSN:chrM\nr1\t0\tchrM\t1\t60\t10M\n"
binary = stub_aligner(tmp_path / "mb", sam)
fixtures = tmp_path / "fx"
fixtures.mkdir()
for f in ("chrM-human.fa.gz", "chrM-read_1.fa.gz", "chrM-read_2.fa.gz"):
(fixtures / f).write_bytes(b"")
rc = main(
[
"--manifest",
str(manifest),
"gates",
"--candidate",
str(binary),
"--stock",
str(binary),
"--fixtures",
str(fixtures),
"--workdir",
str(tmp_path / "wd"),
]
)
assert rc == 0, "a missing --repo must not fail the build"
out = capsys.readouterr().out
assert "tests:x" not in out
assert "--repo not given" in out, "skipping the suites silently is the failure being avoided"