claude-dev-env 8.109.5 → 8.109.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,15 +18,25 @@ number, and records the head it reviewed::
18
18
 
19
19
  python3 merge_review_receipt.py owner/name open def5678 0 -> CLEAN owner/name open def5678
20
20
 
21
+ No pull request exists to hold a rulings comment, so an ``open`` pass rules
22
+ out its findings with a different ``path:line`` for each, run in the checkout
23
+ that holds the head. Each line must exist in the file at that head::
24
+
25
+ python3 merge_review_receipt.py owner/name open def5678 2 --ruled-out 2 --rulings a.py:1,b.py:20
26
+ -> CLEAN owner/name open def5678 (2 findings ruled out: a.py:1,b.py:20)
27
+
21
28
  ``hooks/blocking/merge_review_gate.py`` allows a merge pinned to the head of
22
29
  the newest pass only when that pass is clean. It allows a pull request open
23
- whose head has a clean ``open`` pass, and a merge pinned to that same head.
30
+ whose head has a clean ``open`` pass. It allows a merge pinned to that same
31
+ head only when the ``open`` pass found zero findings. Ruled-out findings need
32
+ a numbered pass that links the rulings comment on the pull request.
24
33
  """
25
34
 
26
35
  from __future__ import annotations
27
36
 
28
37
  import argparse
29
38
  import json
39
+ import subprocess
30
40
  import sys
31
41
  from collections.abc import Sequence
32
42
  from pathlib import Path
@@ -37,6 +47,7 @@ if hooks_directory not in sys.path:
37
47
  sys.path.insert(0, hooks_directory)
38
48
 
39
49
  from blocking.merge_review_gate_parts.receipts import (
50
+ is_clean_open_pass,
40
51
  is_clean_receipt,
41
52
  open_receipt_path,
42
53
  receipt_path,
@@ -47,8 +58,12 @@ from hooks_constants.merge_review_gate_constants import (
47
58
  RECEIPT_FINDINGS_KEY,
48
59
  RECEIPT_HEAD_KEY,
49
60
  RECEIPT_RULED_OUT_KEY,
61
+ RECEIPT_RULING_LINES_KEY,
50
62
  RECEIPT_RULINGS_KEY,
51
63
  REPOSITORY_SLUG_PATTERN,
64
+ RULING_LOOKUP_TIMEOUT_SECONDS,
65
+ RULING_REFERENCE_PATTERN,
66
+ RULING_SEPARATOR,
52
67
  )
53
68
 
54
69
 
@@ -73,25 +88,39 @@ def record_review(
73
88
  Returns:
74
89
  The receipt path written.
75
90
  """
76
- receipt = _receipt_fields(head, finding_count, ruled_out_count, rulings)
91
+ receipt = _receipt_fields(
92
+ head, finding_count, ruled_out_count, RECEIPT_RULINGS_KEY, rulings
93
+ )
77
94
  return _write_receipt(slug, number, receipt)
78
95
 
79
96
 
80
- def record_open_review(slug: str, head: str, finding_count: int) -> Path:
97
+ def record_open_review(
98
+ slug: str, head: str, finding_count: int, all_rulings: Sequence[str] = ()
99
+ ) -> Path:
81
100
  """Write the receipt for one review pass run before the pull request opens.
82
101
 
83
102
  Args:
84
103
  slug: Repository as ``owner/name``.
85
104
  head: The commit the pass reviewed.
86
105
  finding_count: How many findings the pass reported.
106
+ all_rulings: One ``path:line`` of evidence for each ruled out finding.
107
+ Run in the checkout that holds ``head``.
87
108
 
88
109
  Returns:
89
110
  The receipt path written.
111
+
112
+ Raises:
113
+ ValueError: A ruling is malformed, repeated, or names a line ``head`` lacks.
90
114
  """
115
+ problem = _ruling_problem(head, all_rulings)
116
+ if problem:
117
+ raise ValueError(problem)
118
+ return _write_open_receipt(slug, head, _open_receipt(head, finding_count, all_rulings))
119
+
120
+
121
+ def _write_open_receipt(slug: str, head: str, all_receipt_fields: dict[str, object]) -> Path:
91
122
  owner, repo = slug.split("/", 1)
92
- return _write_receipt_at(
93
- open_receipt_path(owner, repo, head), _receipt_fields(head, finding_count, 0, "")
94
- )
123
+ return _write_receipt_at(open_receipt_path(owner, repo, head), all_receipt_fields)
95
124
 
96
125
 
97
126
  def _write_receipt(slug: str, number: int, all_receipt_fields: dict[str, object]) -> Path:
@@ -106,15 +135,119 @@ def _write_receipt_at(path: Path, all_receipt_fields: dict[str, object]) -> Path
106
135
 
107
136
 
108
137
  def _receipt_fields(
109
- head: str, finding_count: int, ruled_out_count: int, rulings: str
138
+ head: str, finding_count: int, ruled_out_count: int, ruling_key: str, ruling_entry: object
110
139
  ) -> dict[str, object]:
111
140
  receipt: dict[str, object] = {RECEIPT_HEAD_KEY: head, RECEIPT_FINDINGS_KEY: finding_count}
112
141
  if ruled_out_count:
113
142
  receipt[RECEIPT_RULED_OUT_KEY] = ruled_out_count
114
- receipt[RECEIPT_RULINGS_KEY] = rulings
143
+ receipt[ruling_key] = ruling_entry
115
144
  return receipt
116
145
 
117
146
 
147
+ def _blob_line_count(head: str, file_path: str) -> int:
148
+ try:
149
+ completed = subprocess.run(
150
+ ["git", "cat-file", "blob", f"{head}:{file_path}"],
151
+ capture_output=True,
152
+ timeout=RULING_LOOKUP_TIMEOUT_SECONDS,
153
+ check=False,
154
+ )
155
+ except (OSError, subprocess.SubprocessError):
156
+ return 0
157
+ return _line_count(completed.stdout) if completed.returncode == 0 else 0
158
+
159
+
160
+ def _unproven_rulings(head: str, all_rulings: Sequence[str]) -> list[str]:
161
+ all_line_counts: dict[str, int] = {}
162
+ all_unproven: list[str] = []
163
+ for each_ruling in all_rulings:
164
+ file_path, _, line_number = each_ruling.rpartition(":")
165
+ if file_path not in all_line_counts:
166
+ all_line_counts[file_path] = _blob_line_count(head, file_path)
167
+ if int(line_number) > all_line_counts[file_path]:
168
+ all_unproven.append(each_ruling)
169
+ return all_unproven
170
+
171
+
172
+ def _line_count(content: bytes) -> int:
173
+ if not content:
174
+ return 0
175
+ return content.count(b"\n") + (0 if content.endswith(b"\n") else 1)
176
+
177
+
178
+ def _split_rulings(rulings: str) -> list[str]:
179
+ all_entries = (each_entry.strip() for each_entry in rulings.split(RULING_SEPARATOR))
180
+ return [each_entry for each_entry in all_entries if each_entry]
181
+
182
+
183
+ def _open_receipt(
184
+ head: str, finding_count: int, all_rulings: Sequence[str]
185
+ ) -> dict[str, object]:
186
+ return _receipt_fields(
187
+ head, finding_count, len(all_rulings), RECEIPT_RULING_LINES_KEY, list(all_rulings)
188
+ )
189
+
190
+
191
+ def _ruling_problem(head: str, all_rulings: Sequence[str]) -> str | None:
192
+ all_malformed = [
193
+ each_ruling
194
+ for each_ruling in all_rulings
195
+ if RULING_REFERENCE_PATTERN.fullmatch(each_ruling) is None
196
+ ]
197
+ if all_malformed:
198
+ return (
199
+ "--rulings needs path:line, with / between folders, no backslash, colon, comma "
200
+ "or . and .. segment in the path, and a line from 1, not "
201
+ f"{RULING_SEPARATOR.join(all_malformed)}"
202
+ )
203
+ if len(set(all_rulings)) != len(all_rulings):
204
+ return "--rulings needs a different path:line for each finding"
205
+ all_unproven = _unproven_rulings(head, all_rulings)
206
+ if all_unproven:
207
+ return (
208
+ f"run this in the checkout that holds {head}; it has no file and line at "
209
+ f"{RULING_SEPARATOR.join(all_unproven)}"
210
+ )
211
+ return None
212
+
213
+
214
+ def _checked_open_rulings(
215
+ parser: argparse.ArgumentParser, parsed: argparse.Namespace
216
+ ) -> list[str]:
217
+ if parsed.ruled_out < 0 or parsed.ruled_out > parsed.findings:
218
+ parser.error("--ruled-out must be between 0 and the finding count")
219
+ all_rulings = _split_rulings(parsed.rulings)
220
+ if len(all_rulings) != parsed.ruled_out:
221
+ parser.error("--rulings needs one path:line for each finding --ruled-out counts")
222
+ return all_rulings
223
+
224
+
225
+ def _record_open_pass(
226
+ parser: argparse.ArgumentParser, parsed: argparse.Namespace, stdout: TextIO
227
+ ) -> int:
228
+ if parsed.findings < 0:
229
+ parser.error("findings must be zero or more")
230
+ if not OPEN_RECEIPT_HEAD_PATTERN.fullmatch(parsed.head):
231
+ parser.error(f"an {OPEN_TARGET_WORD} pass needs the full 40 character commit")
232
+ all_rulings = _checked_open_rulings(parser, parsed)
233
+ try:
234
+ record_open_review(parsed.slug, parsed.head, parsed.findings, all_rulings)
235
+ except ValueError as problem:
236
+ parser.error(str(problem))
237
+ if is_clean_open_pass(_open_receipt(parsed.head, parsed.findings, all_rulings)):
238
+ ruled_out_note = (
239
+ f" ({parsed.ruled_out} findings ruled out: {RULING_SEPARATOR.join(all_rulings)})"
240
+ if parsed.ruled_out
241
+ else ""
242
+ )
243
+ stdout.write(f"CLEAN {parsed.slug} {OPEN_TARGET_WORD} {parsed.head}{ruled_out_note}\n")
244
+ return 0
245
+ stdout.write(
246
+ f"REVIEWED {parsed.slug} {OPEN_TARGET_WORD} {parsed.head} {parsed.findings} findings\n"
247
+ )
248
+ return 1
249
+
250
+
118
251
  def main(all_arguments: Sequence[str], stdout: TextIO = sys.stdout) -> int:
119
252
  """Record one pass and print its verdict line.
120
253
 
@@ -135,23 +268,20 @@ def main(all_arguments: Sequence[str], stdout: TextIO = sys.stdout) -> int:
135
268
  parser.add_argument(
136
269
  "--ruled-out", type=int, default=0, help="Findings ruled out with file:line evidence."
137
270
  )
138
- parser.add_argument("--rulings", default="", help="Link to the posted rulings.")
271
+ parser.add_argument(
272
+ "--rulings",
273
+ default="",
274
+ help=(
275
+ "Link to the posted rulings. For an open pass, a different path:line for each "
276
+ "ruled out finding, comma separated."
277
+ ),
278
+ )
139
279
  parsed = parser.parse_args(all_arguments)
140
280
  rulings = parsed.rulings.strip()
141
281
  if not REPOSITORY_SLUG_PATTERN.fullmatch(parsed.slug) or ".." in parsed.slug:
142
282
  parser.error("slug must be owner/name")
143
283
  if parsed.number == OPEN_TARGET_WORD:
144
- if parsed.ruled_out or rulings:
145
- parser.error(f"an {OPEN_TARGET_WORD} pass is clean only at zero findings")
146
- if parsed.findings < 0:
147
- parser.error("findings must be zero or more")
148
- if not OPEN_RECEIPT_HEAD_PATTERN.fullmatch(parsed.head):
149
- parser.error(f"an {OPEN_TARGET_WORD} pass needs the full 40 character commit")
150
- record_open_review(parsed.slug, parsed.head, parsed.findings)
151
- verdict = "CLEAN" if parsed.findings == 0 else "REVIEWED"
152
- count_note = "" if parsed.findings == 0 else f" {parsed.findings} findings"
153
- stdout.write(f"{verdict} {parsed.slug} {OPEN_TARGET_WORD} {parsed.head}{count_note}\n")
154
- return 0 if parsed.findings == 0 else 1
284
+ return _record_open_pass(parser, parsed, stdout)
155
285
  if not parsed.number.isdigit():
156
286
  parser.error(f"number must be a pull request number or {OPEN_TARGET_WORD}")
157
287
  parsed.number = int(parsed.number)
@@ -161,7 +291,9 @@ def main(all_arguments: Sequence[str], stdout: TextIO = sys.stdout) -> int:
161
291
  parser.error("--ruled-out needs --rulings, a link to the posted rulings")
162
292
  if rulings and not parsed.ruled_out:
163
293
  parser.error("--rulings needs --ruled-out, the count of findings it rules out")
164
- receipt = _receipt_fields(parsed.head, parsed.findings, parsed.ruled_out, rulings)
294
+ receipt = _receipt_fields(
295
+ parsed.head, parsed.findings, parsed.ruled_out, RECEIPT_RULINGS_KEY, rulings
296
+ )
165
297
  _write_receipt(parsed.slug, parsed.number, receipt)
166
298
  owner, repo = parsed.slug.split("/", 1)
167
299
  if is_clean_receipt(receipt, owner, repo, parsed.number):
@@ -769,3 +769,138 @@ def test_cli_accepts_a_repository(tmp_path: Path) -> None:
769
769
  )
770
770
  assert completed_process.returncode == 0
771
771
  assert completed_process.stderr == ""
772
+
773
+
774
+ def _repository_adding_workflow(tmp_path: Path, workflow_name: str) -> Path:
775
+ git_identity = ["-c", "user.name=t", "-c", "user.email=t@example.com"]
776
+ repository_path = tmp_path / "repository"
777
+ subprocess.run(
778
+ ["git", "init", "-q", "-b", "main", str(repository_path)], check=True
779
+ )
780
+ (repository_path / "readme.md").write_text("base\n", encoding="utf-8")
781
+ subprocess.run(["git", "-C", str(repository_path), "add", "."], check=True)
782
+ subprocess.run(
783
+ [
784
+ "git",
785
+ "-C",
786
+ str(repository_path),
787
+ *git_identity,
788
+ "commit",
789
+ "-q",
790
+ "-m",
791
+ "base",
792
+ ],
793
+ check=True,
794
+ )
795
+ subprocess.run(
796
+ ["git", "-C", str(repository_path), "checkout", "-q", "-b", "feature"],
797
+ check=True,
798
+ )
799
+ subprocess.run(
800
+ ["git", "-C", str(repository_path), "remote", "add", "origin", "https://github.com/o/r.git"],
801
+ check=True,
802
+ )
803
+ workflow_directory = repository_path / ".github" / "workflows"
804
+ workflow_directory.mkdir(parents=True)
805
+ (workflow_directory / workflow_name).write_text("name: added\n", encoding="utf-8")
806
+ subprocess.run(["git", "-C", str(repository_path), "add", "."], check=True)
807
+ subprocess.run(
808
+ [
809
+ "git",
810
+ "-C",
811
+ str(repository_path),
812
+ *git_identity,
813
+ "commit",
814
+ "-q",
815
+ "-m",
816
+ "change",
817
+ ],
818
+ check=True,
819
+ )
820
+ return repository_path
821
+
822
+
823
+ def _unproven_workflow_body(after_merge_section: str) -> str:
824
+ return (
825
+ VALID_PR_BODY.replace(
826
+ "on the new body.",
827
+ "on the new body.\n\nNot proven here: the live GitHub Actions run.",
828
+ )
829
+ + after_merge_section
830
+ )
831
+
832
+
833
+ def should_accept_an_unproven_run_when_the_branch_adds_the_workflow_its_after_merge_section_names(
834
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
835
+ ) -> None:
836
+ monkeypatch.chdir(_repository_adding_workflow(tmp_path, "release-hold.yml"))
837
+ after_merge_section = (
838
+ "\n## After merge\n\nThe first owner approval runs `release-hold.yml`."
839
+ " The agent records that run link here.\n"
840
+ )
841
+ all_findings = durable_post_lint.lint_durable_post(
842
+ action="pr-create",
843
+ title="feat: release the hold label",
844
+ body_text=_unproven_workflow_body(after_merge_section),
845
+ head_branch="feature",
846
+ repository="o/r",
847
+ )
848
+ assert all_findings == ()
849
+
850
+
851
+ def should_report_an_unproven_run_when_the_checkout_belongs_to_another_repository(
852
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
853
+ ) -> None:
854
+ monkeypatch.chdir(_repository_adding_workflow(tmp_path, "release-hold.yml"))
855
+ after_merge_section = (
856
+ "\n## After merge\n\nThe first owner approval runs `release-hold.yml`."
857
+ " The agent records that run link here.\n"
858
+ )
859
+ all_findings = durable_post_lint.lint_durable_post(
860
+ action="pr-create",
861
+ title="feat: release the hold label",
862
+ body_text=_unproven_workflow_body(after_merge_section),
863
+ head_branch="feature",
864
+ repository="x/y",
865
+ )
866
+ assert [each_finding.code for each_finding in all_findings] == [
867
+ durable_post_lint.PULL_REQUEST_PROOF_FINDING_CODE
868
+ ]
869
+
870
+
871
+ def should_report_an_unproven_run_when_no_head_branch_is_named(
872
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
873
+ ) -> None:
874
+ monkeypatch.chdir(_repository_adding_workflow(tmp_path, "release-hold.yml"))
875
+ after_merge_section = (
876
+ "\n## After merge\n\nThe first owner approval runs `release-hold.yml`."
877
+ " The agent records that run link here.\n"
878
+ )
879
+ all_findings = durable_post_lint.lint_durable_post(
880
+ action="pr-create",
881
+ title="feat: release the hold label",
882
+ body_text=_unproven_workflow_body(after_merge_section),
883
+ head_branch=None,
884
+ repository="o/r",
885
+ )
886
+ assert [each_finding.code for each_finding in all_findings] == [
887
+ durable_post_lint.PULL_REQUEST_PROOF_FINDING_CODE
888
+ ]
889
+
890
+
891
+ def should_report_an_unproven_run_when_the_after_merge_section_names_another_workflow(
892
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
893
+ ) -> None:
894
+ monkeypatch.chdir(_repository_adding_workflow(tmp_path, "release-hold.yml"))
895
+ after_merge_section = "\n## After merge\n\nThe first owner approval runs `other.yml`. The agent records that run link here.\n"
896
+ all_findings = durable_post_lint.lint_durable_post(
897
+ action="pr-create",
898
+ title="feat: release the hold label",
899
+ body_text=_unproven_workflow_body(after_merge_section),
900
+ head_branch="feature",
901
+ repository="o/r",
902
+ )
903
+ assert [each_finding.code for each_finding in all_findings] == [
904
+ durable_post_lint.PULL_REQUEST_PROOF_FINDING_CODE
905
+ ]
906
+ assert "Not proven here: the live GitHub Actions run." in all_findings[0].message
@@ -3,6 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import json
6
+ import subprocess
6
7
  from io import StringIO
7
8
  from pathlib import Path
8
9
 
@@ -138,6 +139,20 @@ def test_record_open_review_writes_the_head_receipt_in_lowercase(receipt_folder:
138
139
  assert json.loads(path.read_text(encoding="utf-8")) == {"head": HEAD.upper(), "findings": 2}
139
140
 
140
141
 
142
+ def test_should_leave_ruling_keys_out_of_an_open_pass_that_rules_nothing_out(
143
+ receipt_folder: Path,
144
+ ) -> None:
145
+ merge_review_receipt.main(["acme/tools", "open", HEAD, "2"], StringIO())
146
+ receipt = json.loads((receipt_folder / "heads" / f"acme__tools__{HEAD}.json").read_text())
147
+ assert receipt == {"head": HEAD, "findings": 2}
148
+
149
+
150
+ def test_record_open_review_should_refuse_a_ruling_the_head_lacks(receipt_folder: Path) -> None:
151
+ with pytest.raises(ValueError, match="no file and line at nowhere.py:3"):
152
+ merge_review_receipt.record_open_review("acme/tools", HEAD, 1, ["nowhere.py:3"])
153
+ assert not (receipt_folder / "heads").exists()
154
+
155
+
141
156
  def test_should_exit_one_for_an_open_pass_with_findings() -> None:
142
157
  verdict_stream = StringIO()
143
158
  assert merge_review_receipt.main(["acme/tools", "open", HEAD, "2"], verdict_stream) == 1
@@ -160,6 +175,159 @@ def test_should_refuse_an_open_pass_it_cannot_record(
160
175
  assert list(receipt_folder.iterdir()) == []
161
176
 
162
177
 
178
+ def _checkout_with_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> str:
179
+ checkout = tmp_path / "checkout"
180
+ checkout.mkdir()
181
+ subprocess.run(["git", "init", "--quiet", str(checkout)], check=True)
182
+ (checkout / "a.py").write_text("one\ntwo\nthree\n", encoding="utf-8")
183
+ (checkout / "docs").mkdir()
184
+ (checkout / "docs" / "notes.txt").write_text("note\n", encoding="utf-8")
185
+ (checkout / "docs" / "my notes.txt").write_text("spaced\nname\n", encoding="utf-8")
186
+ (checkout / "odd.txt").write_bytes(b"a\x0cb\x1cc\xc2\x85d\n")
187
+ (checkout / "unterminated.txt").write_bytes(b"first\nlast")
188
+ subprocess.run(
189
+ ["git", "-C", str(checkout), "add", "a.py", "docs", "odd.txt", "unterminated.txt"],
190
+ check=True,
191
+ )
192
+ subprocess.run(
193
+ [
194
+ "git",
195
+ "-C",
196
+ str(checkout),
197
+ "-c",
198
+ "user.name=t",
199
+ "-c",
200
+ "user.email=t@example.com",
201
+ "commit",
202
+ "--quiet",
203
+ "-m",
204
+ "first",
205
+ ],
206
+ check=True,
207
+ )
208
+ completed = subprocess.run(
209
+ ["git", "-C", str(checkout), "rev-parse", "HEAD"],
210
+ capture_output=True,
211
+ text=True,
212
+ check=True,
213
+ )
214
+ monkeypatch.chdir(checkout)
215
+ return completed.stdout.strip()
216
+
217
+
218
+ def test_should_print_clean_for_an_open_pass_ruled_out_at_file_and_line(
219
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, receipt_folder: Path
220
+ ) -> None:
221
+ head = _checkout_with_file(tmp_path, monkeypatch)
222
+ verdict_stream = StringIO()
223
+ exit_code = merge_review_receipt.main(
224
+ ["acme/tools", "open", head, "2", "--ruled-out", "2", "--rulings", "a.py:1,a.py:3"],
225
+ verdict_stream,
226
+ )
227
+ assert exit_code == 0
228
+ assert verdict_stream.getvalue() == (
229
+ f"CLEAN acme/tools open {head} (2 findings ruled out: a.py:1,a.py:3)\n"
230
+ )
231
+ receipt = json.loads((receipt_folder / "heads" / f"acme__tools__{head}.json").read_text())
232
+ assert receipt == {
233
+ "head": head,
234
+ "findings": 2,
235
+ "ruled_out": 2,
236
+ "ruling_lines": ["a.py:1", "a.py:3"],
237
+ }
238
+
239
+
240
+ def test_should_count_a_line_by_newline_only_and_keep_an_unterminated_last_line(
241
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, receipt_folder: Path
242
+ ) -> None:
243
+ head = _checkout_with_file(tmp_path, monkeypatch)
244
+ exit_code = merge_review_receipt.main(
245
+ ["acme/tools", "open", head, "2", "--ruled-out", "2", "--rulings", "odd.txt:1,unterminated.txt:2"],
246
+ StringIO(),
247
+ )
248
+ assert exit_code == 0
249
+
250
+
251
+ def test_should_report_reviewed_and_keep_the_rulings_when_some_open_findings_stay_unruled(
252
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, receipt_folder: Path
253
+ ) -> None:
254
+ head = _checkout_with_file(tmp_path, monkeypatch)
255
+ verdict_stream = StringIO()
256
+ exit_code = merge_review_receipt.main(
257
+ ["acme/tools", "open", head, "3", "--ruled-out", "2", "--rulings", "a.py:1,a.py:2"],
258
+ verdict_stream,
259
+ )
260
+ assert exit_code == 1
261
+ assert verdict_stream.getvalue() == f"REVIEWED acme/tools open {head} 3 findings\n"
262
+ receipt = json.loads((receipt_folder / "heads" / f"acme__tools__{head}.json").read_text())
263
+ assert receipt == {
264
+ "head": head,
265
+ "findings": 3,
266
+ "ruled_out": 2,
267
+ "ruling_lines": ["a.py:1", "a.py:2"],
268
+ }
269
+
270
+
271
+ def test_should_accept_an_open_pass_ruled_out_at_a_path_with_a_space(
272
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, receipt_folder: Path
273
+ ) -> None:
274
+ head = _checkout_with_file(tmp_path, monkeypatch)
275
+ exit_code = merge_review_receipt.main(
276
+ ["acme/tools", "open", head, "2", "--ruled-out", "2", "--rulings", "docs/my notes.txt:2,a.py:3"],
277
+ StringIO(),
278
+ )
279
+ assert exit_code == 0
280
+ receipt = json.loads((receipt_folder / "heads" / f"acme__tools__{head}.json").read_text())
281
+ assert receipt["ruling_lines"] == ["docs/my notes.txt:2", "a.py:3"]
282
+
283
+
284
+ @pytest.mark.parametrize(
285
+ "rulings",
286
+ [
287
+ "a.py:1",
288
+ "a.py:1,a.py:9",
289
+ "a.py:1,missing.py:1",
290
+ "a.py:1,docs:1",
291
+ "a.py:1,a.py:0",
292
+ "a.py:1,a.py:1",
293
+ "a.py:1,./a.py:1",
294
+ "a.py:1,docs/../a.py:1",
295
+ "a.py:1,docs\\notes.txt:1",
296
+ "a.py:1,odd.txt:2",
297
+ "a.py:1,unterminated.txt:3",
298
+ "see the thread,a.py:3",
299
+ ],
300
+ )
301
+ def test_should_refuse_an_open_pass_whose_rulings_do_not_prove_each_finding(
302
+ rulings: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, receipt_folder: Path
303
+ ) -> None:
304
+ head = _checkout_with_file(tmp_path, monkeypatch)
305
+ with pytest.raises(SystemExit):
306
+ merge_review_receipt.main(
307
+ ["acme/tools", "open", head, "2", "--ruled-out", "2", "--rulings", rulings],
308
+ StringIO(),
309
+ )
310
+ assert not (receipt_folder / "heads").exists()
311
+
312
+
313
+ def test_should_name_the_path_form_when_a_ruling_is_malformed_not_missing(
314
+ tmp_path: Path,
315
+ monkeypatch: pytest.MonkeyPatch,
316
+ receipt_folder: Path,
317
+ capsys: pytest.CaptureFixture[str],
318
+ ) -> None:
319
+ head = _checkout_with_file(tmp_path, monkeypatch)
320
+ with pytest.raises(SystemExit):
321
+ merge_review_receipt.main(
322
+ ["acme/tools", "open", head, "2", "--ruled-out", "2", "--rulings", "a.py:1,docs\\notes.txt:1"],
323
+ StringIO(),
324
+ )
325
+ error_text = capsys.readouterr().err
326
+ assert "docs\\notes.txt:1" in error_text
327
+ assert "segment" in error_text
328
+ assert "run this in the checkout" not in error_text
329
+
330
+
163
331
  @pytest.mark.parametrize("head", ["abc", "abc1234", "../../escape", "zzzzzzz"])
164
332
  def test_should_refuse_an_open_pass_whose_head_is_not_a_commit(head: str, receipt_folder: Path) -> None:
165
333
  with pytest.raises(SystemExit):