claude-dev-env 8.117.2 → 8.117.4
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.
- package/_shared/pr-loop/scripts/pyproject.toml +1 -1
- package/_shared/process-tree/scripts/pyproject.toml +1 -1
- package/bin/cde.mjs +4 -3
- package/bin/cde.test.mjs +87 -1
- package/bin/codex-compat.mjs +4 -3
- package/bin/codex-compat.test.mjs +91 -1
- package/bin/install-plan.mjs +2 -1
- package/bin/install.mjs +24 -7
- package/bin/install.plan.test.mjs +15 -0
- package/bin/install.test.mjs +61 -0
- package/bin/python-probe.mjs +42 -0
- package/bin/python-probe.test.mjs +19 -2
- package/docs/rule-guides/features-start-with-an-eval.md +3 -3
- package/docs/rule-guides/proof-before-pull-request.md +2 -2
- package/hooks/blocking/bash_pre_tool_use_dispatcher.py +11 -4
- package/hooks/blocking/build_eval_before_eval_write_gate.py +10 -4
- package/hooks/blocking/followup_pr_dedupe.py +381 -64
- package/hooks/blocking/gate_child_process.py +187 -0
- package/hooks/blocking/gate_run_reads.py +116 -35
- package/hooks/blocking/merge_review_gate.py +93 -10
- package/hooks/blocking/merge_review_gate_parts/create_folders.py +346 -0
- package/hooks/blocking/merge_review_gate_parts/create_options.py +86 -0
- package/hooks/blocking/merge_review_gate_parts/folder_state.py +107 -0
- package/hooks/blocking/merge_review_gate_parts/open_request.py +199 -64
- package/hooks/blocking/merge_review_gate_parts/quoted_lines.py +283 -0
- package/hooks/blocking/merge_review_gate_parts/wrapped_creates.py +126 -0
- package/hooks/blocking/pr_lifecycle_skill_gate.py +61 -18
- package/hooks/blocking/pre_tool_use_dispatcher.py +25 -3
- package/hooks/blocking/pull_request_proof.py +5 -51
- package/hooks/blocking/pull_request_proof_edit_gate.py +19 -5
- package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +54 -0
- package/hooks/blocking/test_build_eval_before_eval_write_gate.py +64 -0
- package/hooks/blocking/test_followup_pr_dedupe.py +464 -1
- package/hooks/blocking/test_gate_child_process.py +64 -0
- package/hooks/blocking/test_gate_run_reads.py +364 -4
- package/hooks/blocking/test_merge_review_gate.py +832 -39
- package/hooks/blocking/test_pr_lifecycle_skill_gate.py +130 -4
- package/hooks/blocking/test_pre_tool_use_dispatcher.py +64 -0
- package/hooks/blocking/test_pull_request_proof.py +16 -0
- package/hooks/blocking/test_pull_request_proof_edit_gate.py +60 -0
- package/hooks/blocking/tests/test_create_folders.py +290 -0
- package/hooks/blocking/tests/test_create_options.py +34 -0
- package/hooks/blocking/tests/test_folder_state.py +40 -0
- package/hooks/blocking/tests/test_open_request.py +350 -44
- package/hooks/blocking/tests/test_quoted_lines.py +86 -0
- package/hooks/blocking/tests/test_wrapped_creates.py +42 -0
- package/hooks/features/bash-dispatchers.md +1 -1
- package/hooks/features/conduct-gates.md +7 -7
- package/hooks/features/write-edit-blocking.md +3 -3
- package/hooks/hooks_constants/build_eval_before_eval_write_gate_constants.py +5 -0
- package/hooks/hooks_constants/followup_pr_dedupe_constants.py +34 -1
- package/hooks/hooks_constants/gate_fault.py +52 -0
- package/hooks/hooks_constants/gate_fault_constants.py +13 -0
- package/hooks/hooks_constants/gate_run_reads_constants.py +9 -1
- package/hooks/hooks_constants/merge_review_gate_constants.py +94 -1
- package/hooks/hooks_constants/piped_pytest_blocker_constants.py +2 -0
- package/hooks/hooks_constants/pr_lifecycle_skill_gate_constants.py +6 -1
- package/hooks/hooks_constants/pre_tool_use_stdin.py +23 -3
- package/hooks/hooks_constants/shell_command_pipeline.py +68 -8
- package/hooks/hooks_constants/shell_command_segments.py +2 -2
- package/hooks/hooks_constants/shell_command_wrappers.py +3 -2
- package/hooks/hooks_constants/test_followup_pr_dedupe_constants.py +7 -0
- package/hooks/hooks_constants/test_gate_fault.py +41 -0
- package/hooks/hooks_constants/test_piped_pytest_blocker_constants.py +5 -0
- package/hooks/hooks_constants/test_pr_lifecycle_skill_gate_constants.py +10 -0
- package/hooks/hooks_constants/test_pre_tool_use_stdin.py +97 -0
- package/hooks/hooks_constants/test_shell_command_pipeline.py +55 -0
- package/hooks/hooks_constants/test_shell_command_segments.py +15 -0
- package/hooks/hooks_constants/test_shell_command_wrappers.py +6 -0
- package/hooks/hooks_constants/transcript_skill_scan_constants.py +6 -0
- package/hooks/pyproject.toml +2 -3
- package/hooks/test_python_floor.py +197 -0
- package/hooks/test_transcript_skill_scan.py +324 -0
- package/hooks/transcript_skill_scan.py +233 -2
- package/hooks/validators/code_quality_checks.py +119 -17
- package/hooks/validators/test_code_quality_checks.py +110 -4
- package/package.json +1 -1
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
4
|
import os
|
|
5
|
+
import subprocess
|
|
5
6
|
import sys
|
|
6
7
|
import threading
|
|
7
8
|
import time
|
|
9
|
+
import urllib.error
|
|
8
10
|
from collections.abc import Mapping
|
|
11
|
+
from email.message import Message
|
|
9
12
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
10
13
|
from pathlib import Path
|
|
11
14
|
|
|
@@ -17,7 +20,13 @@ if str(HOOKS_DIRECTORY) not in sys.path:
|
|
|
17
20
|
|
|
18
21
|
from blocking import followup_pr_dedupe as dedupe
|
|
19
22
|
from blocking.gate_run_reads import gate_run
|
|
20
|
-
from hooks_constants.followup_pr_dedupe_constants import
|
|
23
|
+
from hooks_constants.followup_pr_dedupe_constants import (
|
|
24
|
+
DUPLICATE_FOLLOWUP_REASON_TEMPLATE,
|
|
25
|
+
UNANSWERED_FOLLOWUP_REASON_TEMPLATE,
|
|
26
|
+
GH_REPO_SET_FOLLOWUP_REASON_TEMPLATE,
|
|
27
|
+
UNREAD_ORIGIN_FOLLOWUP_REASON_TEMPLATE,
|
|
28
|
+
)
|
|
29
|
+
from hooks_constants.pytest_invocation import unquoted_token
|
|
21
30
|
|
|
22
31
|
OPEN_FOLLOWUP = {
|
|
23
32
|
"number": 1769,
|
|
@@ -262,3 +271,457 @@ def test_followup_read_failure_should_skip_only_that_create() -> None:
|
|
|
262
271
|
reason = dedupe.duplicate_followup_reason(payload, _keyed_reader(all_open_by_repository, all_asked_repositories))
|
|
263
272
|
assert reason == _duplicate_reason(open_followup, 7)
|
|
264
273
|
assert all_asked_repositories == [("a", "r"), ("b", "r")]
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def _unanswered_reason(owner: str, repo: str, parent_number: int) -> str:
|
|
277
|
+
return UNANSWERED_FOLLOWUP_REASON_TEMPLATE.format(owner=owner, repo=repo, parent_number=parent_number)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def _raising_reader(error: Exception) -> dedupe.OpenPullRequestReader:
|
|
281
|
+
def read(owner: str, repo: str) -> list[Mapping[str, object]]:
|
|
282
|
+
raise error
|
|
283
|
+
|
|
284
|
+
return read
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
@pytest.mark.parametrize(
|
|
288
|
+
"unanswered_error",
|
|
289
|
+
[
|
|
290
|
+
TimeoutError("read timed out"),
|
|
291
|
+
urllib.error.URLError(TimeoutError("connect timed out")),
|
|
292
|
+
urllib.error.HTTPError("https://api.github.com/x", 500, "Internal Server Error", Message(), None),
|
|
293
|
+
urllib.error.HTTPError("https://api.github.com/x", 503, "Service Unavailable", Message(), None),
|
|
294
|
+
],
|
|
295
|
+
)
|
|
296
|
+
def test_unanswered_read_should_refuse_the_create(unanswered_error: Exception) -> None:
|
|
297
|
+
reason = dedupe.duplicate_followup_reason(_mcp_payload("Follow-up to #1731"), _raising_reader(unanswered_error))
|
|
298
|
+
assert reason == _unanswered_reason("jl-cmd", "claude-dev-env", 1731)
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
@pytest.mark.parametrize(
|
|
302
|
+
"answered_error",
|
|
303
|
+
[
|
|
304
|
+
urllib.error.HTTPError("https://api.github.com/x", 404, "Not Found", Message(), None),
|
|
305
|
+
urllib.error.HTTPError("https://api.github.com/x", 403, "rate limit exceeded", Message(), None),
|
|
306
|
+
urllib.error.HTTPError("https://api.github.com/x", 429, "Too Many Requests", Message(), None),
|
|
307
|
+
urllib.error.URLError(ConnectionRefusedError("refused")),
|
|
308
|
+
urllib.error.URLError(OSError("Name or service not known")),
|
|
309
|
+
json.JSONDecodeError("Expecting value", "<html>", 0),
|
|
310
|
+
],
|
|
311
|
+
)
|
|
312
|
+
def test_answered_read_failure_should_skip_the_create(answered_error: Exception) -> None:
|
|
313
|
+
assert dedupe.duplicate_followup_reason(_mcp_payload("Follow-up to #1731"), _raising_reader(answered_error)) is None
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def test_read_with_no_time_left_should_refuse_the_create(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
317
|
+
monkeypatch.setattr(dedupe, "answer_within_allowance", lambda *all_arguments: None)
|
|
318
|
+
reason = dedupe.duplicate_followup_reason(_mcp_payload("Follow-up to #1731"))
|
|
319
|
+
assert reason == _unanswered_reason("jl-cmd", "claude-dev-env", 1731)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def _silent_server() -> ThreadingHTTPServer:
|
|
323
|
+
class SilentHandler(BaseHTTPRequestHandler):
|
|
324
|
+
def log_message(self, *all_arguments: object) -> None:
|
|
325
|
+
return
|
|
326
|
+
|
|
327
|
+
def do_GET(self) -> None:
|
|
328
|
+
time.sleep(5)
|
|
329
|
+
|
|
330
|
+
server = ThreadingHTTPServer(("127.0.0.1", 0), SilentHandler)
|
|
331
|
+
server.daemon_threads = True
|
|
332
|
+
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
333
|
+
return server
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def test_hung_web_read_should_refuse_the_create(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
337
|
+
server = _silent_server()
|
|
338
|
+
monkeypatch.setenv("NO_PROXY", "127.0.0.1")
|
|
339
|
+
monkeypatch.setenv("no_proxy", "127.0.0.1")
|
|
340
|
+
monkeypatch.setattr(dedupe, "GITHUB_API_ROOT", f"http://127.0.0.1:{server.server_address[1]}")
|
|
341
|
+
monkeypatch.setattr(dedupe, "READ_TIMEOUT_SECONDS", 1)
|
|
342
|
+
reason = dedupe.duplicate_followup_reason(_mcp_payload("Follow-up to #1731"))
|
|
343
|
+
server.shutdown()
|
|
344
|
+
assert reason == _unanswered_reason("jl-cmd", "claude-dev-env", 1731)
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def test_web_read_answering_404_should_skip_the_create(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
348
|
+
class NotFoundHandler(BaseHTTPRequestHandler):
|
|
349
|
+
def log_message(self, *all_arguments: object) -> None:
|
|
350
|
+
return
|
|
351
|
+
|
|
352
|
+
def do_GET(self) -> None:
|
|
353
|
+
self.send_error(404)
|
|
354
|
+
|
|
355
|
+
server = ThreadingHTTPServer(("127.0.0.1", 0), NotFoundHandler)
|
|
356
|
+
server.daemon_threads = True
|
|
357
|
+
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
358
|
+
monkeypatch.setenv("NO_PROXY", "127.0.0.1")
|
|
359
|
+
monkeypatch.setenv("no_proxy", "127.0.0.1")
|
|
360
|
+
monkeypatch.setattr(dedupe, "GITHUB_API_ROOT", f"http://127.0.0.1:{server.server_address[1]}")
|
|
361
|
+
reason = dedupe.duplicate_followup_reason(_mcp_payload("Follow-up to #1731"))
|
|
362
|
+
server.shutdown()
|
|
363
|
+
assert reason is None
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
def test_unanswered_read_should_refuse_every_draft_on_that_repository_after_one_read() -> None:
|
|
367
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
368
|
+
reader = _keyed_reader({("o", "r"): TimeoutError("read timed out")}, all_asked_repositories)
|
|
369
|
+
with gate_run():
|
|
370
|
+
first_reason = dedupe.duplicate_followup_reason(_chained_inline_creates([("o/r", "Follow-up to #7")]), reader)
|
|
371
|
+
second_reason = dedupe.duplicate_followup_reason(_chained_inline_creates([("o/r", "Follow-up to #8")]), reader)
|
|
372
|
+
assert first_reason == _unanswered_reason("o", "r", 7)
|
|
373
|
+
assert second_reason == _unanswered_reason("o", "r", 8)
|
|
374
|
+
assert all_asked_repositories == [("o", "r")]
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
def test_answered_failure_should_stay_remembered_as_a_skip() -> None:
|
|
378
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
379
|
+
reader = _keyed_reader({("o", "r"): OSError("network down")}, all_asked_repositories)
|
|
380
|
+
with gate_run():
|
|
381
|
+
first_reason = dedupe.duplicate_followup_reason(_chained_inline_creates([("o/r", "Follow-up to #7")]), reader)
|
|
382
|
+
second_reason = dedupe.duplicate_followup_reason(_chained_inline_creates([("o/r", "Follow-up to #8")]), reader)
|
|
383
|
+
assert (first_reason, second_reason) == (None, None)
|
|
384
|
+
assert all_asked_repositories == [("o", "r")]
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def test_first_unanswered_or_duplicate_draft_in_command_order_should_refuse() -> None:
|
|
388
|
+
open_followup = _open_followup(9, 7)
|
|
389
|
+
all_open_by_repository: dict[tuple[str, str], list[Mapping[str, object]] | Exception] = {
|
|
390
|
+
("a", "r"): TimeoutError("read timed out"),
|
|
391
|
+
("b", "r"): [open_followup],
|
|
392
|
+
}
|
|
393
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
394
|
+
unanswered_first = _chained_inline_creates([("a/r", "Follow-up to #5"), ("b/r", "Follow-up to #7")])
|
|
395
|
+
reader = _keyed_reader(all_open_by_repository, all_asked_repositories)
|
|
396
|
+
assert dedupe.duplicate_followup_reason(unanswered_first, reader) == _unanswered_reason("a", "r", 5)
|
|
397
|
+
assert all_asked_repositories == [("a", "r")]
|
|
398
|
+
duplicate_first = _chained_inline_creates([("b/r", "Follow-up to #7"), ("a/r", "Follow-up to #5")])
|
|
399
|
+
assert dedupe.duplicate_followup_reason(duplicate_first, reader) == _duplicate_reason(open_followup, 7)
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def _script_create(command: str, tmp_path: Path, body: str) -> dict[str, object]:
|
|
403
|
+
(tmp_path / "body.md").write_text(body, encoding="utf-8")
|
|
404
|
+
return {"tool_name": "Bash", "cwd": str(tmp_path), "tool_input": {"command": command}}
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def test_script_create_whose_body_file_names_an_open_parent_should_be_denied(tmp_path: Path) -> None:
|
|
408
|
+
payload = _script_create(
|
|
409
|
+
"python3 .agents/skills/pull-request/scripts/pull_request.py create --repo jl-cmd/claude-dev-env"
|
|
410
|
+
' --base main --head fix-b --title "fix: b" --body-file body.md',
|
|
411
|
+
tmp_path,
|
|
412
|
+
"Follow-up to #1731\n",
|
|
413
|
+
)
|
|
414
|
+
reason = dedupe.duplicate_followup_reason(payload, _reader([OPEN_FOLLOWUP]))
|
|
415
|
+
assert reason == _duplicate_reason(OPEN_FOLLOWUP, 1731)
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def test_followup_drafts_should_read_a_script_create_once_with_its_assigned_options(tmp_path: Path) -> None:
|
|
419
|
+
payload = _script_create(
|
|
420
|
+
'python pull_request.py create --repo=o/r --base=main --head=b --title="fix: b" --body-file=body.md',
|
|
421
|
+
tmp_path,
|
|
422
|
+
"Follow-up to #7\n",
|
|
423
|
+
)
|
|
424
|
+
assert dedupe.followup_drafts(payload) == [dedupe.PullRequestDraft("o", "r", "Follow-up to #7\n")]
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def test_all_create_argument_lists_should_list_gh_and_script_creates_once_each() -> None:
|
|
428
|
+
command = (
|
|
429
|
+
'gh pr create --title "fix: a" --body x'
|
|
430
|
+
' && python3 pull_request.py create --repo o/r --title "fix: b" --body-file b.md'
|
|
431
|
+
" && python3 pull_request.py edit --repo o/r --number 7 && gh pr view 7"
|
|
432
|
+
)
|
|
433
|
+
all_argument_lists = [
|
|
434
|
+
[unquoted_token(each_argument) for each_argument in each_list]
|
|
435
|
+
for each_list in dedupe.all_create_argument_lists(command)
|
|
436
|
+
]
|
|
437
|
+
assert all_argument_lists == [
|
|
438
|
+
["--title", "fix: a", "--body", "x"],
|
|
439
|
+
["--repo", "o/r", "--title", "fix: b", "--body-file", "b.md"],
|
|
440
|
+
]
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
@pytest.mark.parametrize(
|
|
444
|
+
("command", "expected_arguments"),
|
|
445
|
+
[
|
|
446
|
+
("gh pr new --title t", ["--title", "t"]),
|
|
447
|
+
("gh -R o/r pr create --title t", ["--title", "t", "-R", "o/r"]),
|
|
448
|
+
("gh --repo o/r pr create", ["--repo", "o/r"]),
|
|
449
|
+
("gh --repo=o/r pr new", ["--repo=o/r"]),
|
|
450
|
+
("GH_REPO=o/r gh pr create --title t", ["--title", "t", "--repo", "o/r"]),
|
|
451
|
+
("env GH_REPO=o/r gh pr create", ["--repo", "o/r"]),
|
|
452
|
+
("GH_REPO=x/y gh pr create --repo o/r", ["--repo", "o/r"]),
|
|
453
|
+
("python3.11 pull_request.py create --title t", ["--title", "t"]),
|
|
454
|
+
("python3.13 pull_request.py create", []),
|
|
455
|
+
("python3 -u pull_request.py create", []),
|
|
456
|
+
("python -X dev pull_request.py create --head h", ["--head", "h"]),
|
|
457
|
+
("py -3 pull_request.py create", []),
|
|
458
|
+
("uv run python pull_request.py create", []),
|
|
459
|
+
("uv run pull_request.py create", []),
|
|
460
|
+
("python3 ~/.agents/skills/pull-request/scripts/pull_request.py create", []),
|
|
461
|
+
],
|
|
462
|
+
)
|
|
463
|
+
def test_all_create_argument_lists_should_read_every_create_spelling(
|
|
464
|
+
command: str, expected_arguments: list[str]
|
|
465
|
+
) -> None:
|
|
466
|
+
all_argument_lists = [
|
|
467
|
+
[unquoted_token(each_argument) for each_argument in each_list]
|
|
468
|
+
for each_list in dedupe.all_create_argument_lists(command)
|
|
469
|
+
]
|
|
470
|
+
assert all_argument_lists == [expected_arguments]
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
@pytest.mark.parametrize(
|
|
474
|
+
"command",
|
|
475
|
+
[
|
|
476
|
+
"python -m pull_request.py create",
|
|
477
|
+
'python -c "print(1)" pull_request.py create',
|
|
478
|
+
'git commit -m "see gh pr create --head two"',
|
|
479
|
+
"cat <<'EOF'\ngh pr create --head two\nEOF",
|
|
480
|
+
"gh pr view --repo o/r",
|
|
481
|
+
],
|
|
482
|
+
)
|
|
483
|
+
def test_all_create_argument_lists_should_skip_text_that_runs_no_create(command: str) -> None:
|
|
484
|
+
assert list(dedupe.all_create_argument_lists(command)) == []
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
def test_create_arguments_should_read_one_segment() -> None:
|
|
488
|
+
assert dedupe.create_arguments(["GH_REPO=o/r", "gh", "pr", "new", "--head", "h"]) == [
|
|
489
|
+
"--head",
|
|
490
|
+
"h",
|
|
491
|
+
"--repo",
|
|
492
|
+
"o/r",
|
|
493
|
+
]
|
|
494
|
+
assert dedupe.create_arguments(["gh", "pr", "view"]) is None
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
def test_option_value_should_read_spaced_and_assigned_values() -> None:
|
|
498
|
+
assert dedupe.option_value(["--head", "a", "--head", "b"], frozenset({"--head"})) == "a"
|
|
499
|
+
assert dedupe.option_value(["-H='c'"], frozenset({"-H", "--head"})) == "c"
|
|
500
|
+
assert dedupe.option_value(["--fill"], frozenset({"--head"})) is None
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
@pytest.mark.parametrize(
|
|
504
|
+
("all_tokens", "expected"),
|
|
505
|
+
[
|
|
506
|
+
(["GH_REPO=o/r", "gh", "pr", "view"], "o/r"),
|
|
507
|
+
(["X=1", "GH_REPO=o/r", "gh.exe", "pr", "view"], "o/r"),
|
|
508
|
+
(["gh", "pr", "view", "GH_REPO=o/r"], None),
|
|
509
|
+
(["gh", "pr", "view"], None),
|
|
510
|
+
(["GH_REPO=a/b", "GH_REPO=o/r", "gh", "pr", "view"], "o/r"),
|
|
511
|
+
(["GH_REPO+=o/r", "gh", "pr", "view"], None),
|
|
512
|
+
(["GH_REPO=o/r", "GH_REPO+=x", "gh", "pr", "view"], None),
|
|
513
|
+
(["GH_REPO+=x", "GH_REPO=o/r", "gh", "pr", "view"], "o/r"),
|
|
514
|
+
],
|
|
515
|
+
)
|
|
516
|
+
def test_prefix_repository_should_read_only_a_prefix_before_gh(all_tokens: list[str], expected: str | None) -> None:
|
|
517
|
+
assert dedupe.prefix_repository(all_tokens) == expected
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
def test_named_repository_should_read_owner_and_name_past_a_host() -> None:
|
|
521
|
+
assert dedupe.named_repository("github.com/o/r") == ("o", "r")
|
|
522
|
+
assert dedupe.named_repository("o/r") == ("o", "r")
|
|
523
|
+
assert dedupe.named_repository("notaslug") is None
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
def test_gh_repo_environment_should_name_the_repository_when_the_origin_is_unread(tmp_path: Path) -> None:
|
|
527
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
528
|
+
open_followup = _open_followup(9, 7)
|
|
529
|
+
payload = {
|
|
530
|
+
"tool_name": "Bash",
|
|
531
|
+
"cwd": str(tmp_path),
|
|
532
|
+
"tool_input": {"command": 'GH_REPO=o/r gh pr create --title "fix: a" --body "Follow-up to #7"'},
|
|
533
|
+
}
|
|
534
|
+
reader = _keyed_reader({("o", "r"): [open_followup]}, all_asked_repositories)
|
|
535
|
+
assert dedupe.duplicate_followup_reason(payload, reader) == _duplicate_reason(open_followup, 7)
|
|
536
|
+
assert all_asked_repositories == [("o", "r")]
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
def test_all_pull_request_argument_lists_should_list_the_named_action_only() -> None:
|
|
540
|
+
command = "gh pr edit 7 --body x && python pull_request.py edit --number 8 && gh pr create --body y"
|
|
541
|
+
all_argument_lists = list(dedupe.all_pull_request_argument_lists(command, ["pr", "edit"], "edit"))
|
|
542
|
+
assert all_argument_lists == [["7", "--body", "x"], ["--number", "8"]]
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
def _unread_origin_reason(parent_number: int) -> str:
|
|
546
|
+
return UNREAD_ORIGIN_FOLLOWUP_REASON_TEMPLATE.format(parent_number=parent_number)
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
def _create_without_repository(working_directory: Path, body: str) -> dict[str, object]:
|
|
550
|
+
return {
|
|
551
|
+
"tool_name": "Bash",
|
|
552
|
+
"cwd": str(working_directory),
|
|
553
|
+
"tool_input": {"command": f'gh pr create --title "fix: a" --body "{body}"'},
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
def _checkout_with_origin(tmp_path: Path, origin_url: str) -> Path:
|
|
558
|
+
checkout_path = tmp_path / "checkout"
|
|
559
|
+
subprocess.run(["git", "init", "-q", str(checkout_path)], check=True)
|
|
560
|
+
subprocess.run(["git", "-C", str(checkout_path), "remote", "add", "origin", origin_url], check=True)
|
|
561
|
+
return checkout_path
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
def _unasked_reader(all_asked_repositories: list[tuple[str, str]]) -> dedupe.OpenPullRequestReader:
|
|
565
|
+
return _keyed_reader({}, all_asked_repositories)
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
@pytest.mark.parametrize(
|
|
569
|
+
("command", "expected"),
|
|
570
|
+
[
|
|
571
|
+
("export GH_REPO=o/r ; gh pr create", True),
|
|
572
|
+
("GH_REPO=o/r && gh pr create", True),
|
|
573
|
+
("declare -x GH_REPO", True),
|
|
574
|
+
("typeset -x GH_REPO=o/r", True),
|
|
575
|
+
("bash -c 'export GH_REPO=o/r'", True),
|
|
576
|
+
("env GH_REPO=o/r bash -c 'gh pr create'", True),
|
|
577
|
+
("export GH_REPO+=o/r ; gh pr create", True),
|
|
578
|
+
("GH_REPO+=o/r ; gh pr create", True),
|
|
579
|
+
("declare -x GH_REPO+=o/r", True),
|
|
580
|
+
("GH_REPO+=o/r gh pr create", True),
|
|
581
|
+
("GH_REPO+=o/r bash -c 'gh pr create'", True),
|
|
582
|
+
("GH_REPO=o/r gh pr create", False),
|
|
583
|
+
("echo GH_REPO=o/r ; gh pr create", False),
|
|
584
|
+
("echo GH_REPO+=o/r ; gh pr create", False),
|
|
585
|
+
("gh pr create --body 'export GH_REPO=o/r'", False),
|
|
586
|
+
("export PATH=/bin ; gh pr create", False),
|
|
587
|
+
],
|
|
588
|
+
)
|
|
589
|
+
def test_sets_gh_repository_should_find_a_gh_repo_set_outside_a_create_prefix(command: str, expected: bool) -> None:
|
|
590
|
+
assert dedupe.sets_gh_repository(command) is expected
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
@pytest.mark.parametrize("operator", [";", "&&", "\n"])
|
|
594
|
+
def test_create_with_no_repository_should_be_refused_when_the_command_sets_gh_repo(
|
|
595
|
+
tmp_path: Path, operator: str
|
|
596
|
+
) -> None:
|
|
597
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
598
|
+
checkout = _checkout_with_origin(tmp_path, "https://github.com/acme/tools.git")
|
|
599
|
+
create = 'gh pr create --title "fix: a" --body "Follow-up to #7"'
|
|
600
|
+
payload = {
|
|
601
|
+
"tool_name": "Bash",
|
|
602
|
+
"cwd": str(checkout),
|
|
603
|
+
"tool_input": {"command": f"export GH_REPO=o/r {operator} {create}"},
|
|
604
|
+
}
|
|
605
|
+
reason = dedupe.duplicate_followup_reason(payload, _unasked_reader(all_asked_repositories))
|
|
606
|
+
assert reason == GH_REPO_SET_FOLLOWUP_REASON_TEMPLATE.format(parent_number=7)
|
|
607
|
+
assert all_asked_repositories == []
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
@pytest.mark.parametrize(
|
|
611
|
+
"prefix",
|
|
612
|
+
["GH_REPO+=o/r ", "export GH_REPO+=o/r ; ", "GH_REPO+=o/r ; ", "GH_REPO=acme/tools GH_REPO+=x "],
|
|
613
|
+
)
|
|
614
|
+
def test_create_with_no_repository_should_be_refused_when_the_command_appends_to_gh_repo(
|
|
615
|
+
tmp_path: Path, prefix: str
|
|
616
|
+
) -> None:
|
|
617
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
618
|
+
checkout = _checkout_with_origin(tmp_path, "https://github.com/acme/tools.git")
|
|
619
|
+
create = 'gh pr create --title "fix: a" --body "Follow-up to #7"'
|
|
620
|
+
payload = {"tool_name": "Bash", "cwd": str(checkout), "tool_input": {"command": prefix + create}}
|
|
621
|
+
reason = dedupe.duplicate_followup_reason(payload, _unasked_reader(all_asked_repositories))
|
|
622
|
+
assert reason == GH_REPO_SET_FOLLOWUP_REASON_TEMPLATE.format(parent_number=7)
|
|
623
|
+
assert all_asked_repositories == []
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
def test_create_with_its_own_repository_should_be_read_when_the_command_appends_to_gh_repo(tmp_path: Path) -> None:
|
|
627
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
628
|
+
open_followup = _open_followup(9, 7)
|
|
629
|
+
command = 'GH_REPO+=x/y gh pr create --repo o/r --title "fix: a" --body "Follow-up to #7"'
|
|
630
|
+
payload = {"tool_name": "Bash", "cwd": str(tmp_path), "tool_input": {"command": command}}
|
|
631
|
+
reader = _keyed_reader({("o", "r"): [open_followup]}, all_asked_repositories)
|
|
632
|
+
assert dedupe.duplicate_followup_reason(payload, reader) == _duplicate_reason(open_followup, 7)
|
|
633
|
+
assert all_asked_repositories == [("o", "r")]
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
def test_create_with_its_own_repository_should_be_read_when_the_command_sets_gh_repo(tmp_path: Path) -> None:
|
|
637
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
638
|
+
open_followup = _open_followup(9, 7)
|
|
639
|
+
command = 'export GH_REPO=x/y ; gh pr create --repo o/r --title "fix: a" --body "Follow-up to #7"'
|
|
640
|
+
payload = {"tool_name": "Bash", "cwd": str(tmp_path), "tool_input": {"command": command}}
|
|
641
|
+
reader = _keyed_reader({("o", "r"): [open_followup]}, all_asked_repositories)
|
|
642
|
+
assert dedupe.duplicate_followup_reason(payload, reader) == _duplicate_reason(open_followup, 7)
|
|
643
|
+
assert all_asked_repositories == [("o", "r")]
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
def test_origin_read_that_outlives_its_allowance_should_refuse_the_create(
|
|
647
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
648
|
+
) -> None:
|
|
649
|
+
monkeypatch.setattr(dedupe, "ALL_GIT_REMOTE_COMMAND_WORDS", (sys.executable, "-c", "import time; time.sleep(5)"))
|
|
650
|
+
monkeypatch.setattr(dedupe, "READ_TIMEOUT_SECONDS", 1)
|
|
651
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
652
|
+
payload = _create_without_repository(tmp_path, "Follow-up to #1731")
|
|
653
|
+
reason = dedupe.duplicate_followup_reason(payload, _unasked_reader(all_asked_repositories))
|
|
654
|
+
assert reason == _unread_origin_reason(1731)
|
|
655
|
+
assert all_asked_repositories == []
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
def test_origin_read_that_exits_non_zero_should_refuse_the_create(tmp_path: Path) -> None:
|
|
659
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
660
|
+
payload = _create_without_repository(tmp_path, "Follow-up to #1731")
|
|
661
|
+
reason = dedupe.duplicate_followup_reason(payload, _unasked_reader(all_asked_repositories))
|
|
662
|
+
assert reason == _unread_origin_reason(1731)
|
|
663
|
+
assert all_asked_repositories == []
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
def test_origin_that_prints_a_url_off_github_should_skip_the_create(tmp_path: Path) -> None:
|
|
667
|
+
checkout_path = _checkout_with_origin(tmp_path, "https://gitlab.com/o/r.git")
|
|
668
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
669
|
+
payload = _create_without_repository(checkout_path, "Follow-up to #1731")
|
|
670
|
+
assert dedupe.duplicate_followup_reason(payload, _unasked_reader(all_asked_repositories)) is None
|
|
671
|
+
assert all_asked_repositories == []
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
def test_origin_on_github_should_name_the_repository_to_read(tmp_path: Path) -> None:
|
|
675
|
+
checkout_path = _checkout_with_origin(tmp_path, "git@github.com:o/r.git")
|
|
676
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
677
|
+
payload = _create_without_repository(checkout_path, "Follow-up to #7")
|
|
678
|
+
reader = _keyed_reader({("o", "r"): [_open_followup(9, 7)]}, all_asked_repositories)
|
|
679
|
+
assert dedupe.duplicate_followup_reason(payload, reader) == _duplicate_reason(_open_followup(9, 7), 7)
|
|
680
|
+
assert all_asked_repositories == [("o", "r")]
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
def test_unread_origin_should_be_read_once_per_run_and_refuse_every_draft_without_a_repository(
|
|
684
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
685
|
+
) -> None:
|
|
686
|
+
all_origin_reads: list[tuple[object, ...]] = []
|
|
687
|
+
|
|
688
|
+
def unanswered_child(*all_arguments: object) -> None:
|
|
689
|
+
all_origin_reads.append(all_arguments)
|
|
690
|
+
|
|
691
|
+
monkeypatch.setattr(dedupe, "child_stdout", unanswered_child)
|
|
692
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
693
|
+
with gate_run():
|
|
694
|
+
first_reason = dedupe.duplicate_followup_reason(
|
|
695
|
+
_create_without_repository(tmp_path, "Follow-up to #7"), _unasked_reader(all_asked_repositories)
|
|
696
|
+
)
|
|
697
|
+
second_reason = dedupe.duplicate_followup_reason(
|
|
698
|
+
_create_without_repository(tmp_path, "Follow-up to #8"), _unasked_reader(all_asked_repositories)
|
|
699
|
+
)
|
|
700
|
+
assert (first_reason, second_reason) == (_unread_origin_reason(7), _unread_origin_reason(8))
|
|
701
|
+
assert len(all_origin_reads) == 1
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
def test_command_order_should_decide_between_an_unread_origin_and_a_duplicate(tmp_path: Path) -> None:
|
|
705
|
+
open_followup = _open_followup(9, 7)
|
|
706
|
+
with_repository = 'gh pr create --repo b/r --title "fix: b" --body "Follow-up to #7"'
|
|
707
|
+
without_repository = 'gh pr create --title "fix: a" --body "Follow-up to #5"'
|
|
708
|
+
reader = _keyed_reader({("b", "r"): [open_followup]}, [])
|
|
709
|
+
|
|
710
|
+
def payload(command: str) -> dict[str, object]:
|
|
711
|
+
return {"tool_name": "Bash", "cwd": str(tmp_path), "tool_input": {"command": command}}
|
|
712
|
+
|
|
713
|
+
unread_first = payload(without_repository + " && " + with_repository)
|
|
714
|
+
duplicate_first = payload(with_repository + " && " + without_repository)
|
|
715
|
+
assert dedupe.duplicate_followup_reason(unread_first, reader) == _unread_origin_reason(5)
|
|
716
|
+
assert dedupe.duplicate_followup_reason(duplicate_first, reader) == _duplicate_reason(open_followup, 7)
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
def test_repository_value_gh_rejects_should_skip_the_create(tmp_path: Path) -> None:
|
|
720
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
721
|
+
payload = {
|
|
722
|
+
"tool_name": "Bash",
|
|
723
|
+
"cwd": str(tmp_path),
|
|
724
|
+
"tool_input": {"command": 'gh pr create --repo notaslug --title "fix: a" --body "Follow-up to #7"'},
|
|
725
|
+
}
|
|
726
|
+
assert dedupe.duplicate_followup_reason(payload, _unasked_reader(all_asked_repositories)) is None
|
|
727
|
+
assert all_asked_repositories == []
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""Behavioral tests for starting, holding and stopping the gate's child processes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import signal
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
HOOKS_DIRECTORY = Path(__file__).resolve().parent.parent
|
|
12
|
+
if str(HOOKS_DIRECTORY) not in sys.path:
|
|
13
|
+
sys.path.insert(0, str(HOOKS_DIRECTORY))
|
|
14
|
+
|
|
15
|
+
from blocking import gate_child_process
|
|
16
|
+
from blocking.gate_child_process import close_job, job_holding, started_child, stop_child
|
|
17
|
+
|
|
18
|
+
SLEEPING_CHILD = [sys.executable, "-c", "import time; time.sleep(30)"]
|
|
19
|
+
PRINTING_CHILD = [sys.executable, "-c", "print('answer')"]
|
|
20
|
+
FAKE_JOB_HANDLE = 7
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_started_child_should_pipe_the_output_of_a_child_that_starts() -> None:
|
|
24
|
+
child = started_child(PRINTING_CHILD, None, None)
|
|
25
|
+
assert child is not None
|
|
26
|
+
assert child.communicate(timeout=10)[0] == "answer\n"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_started_child_should_return_none_for_a_program_that_cannot_start(tmp_path: Path) -> None:
|
|
30
|
+
assert started_child([str(tmp_path / "no-such-program")], None, None) is None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_job_holding_should_give_no_job_outside_windows(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
34
|
+
monkeypatch.setattr(gate_child_process.sys, "platform", "linux")
|
|
35
|
+
child = started_child(PRINTING_CHILD, None, None)
|
|
36
|
+
assert child is not None
|
|
37
|
+
child.communicate(timeout=10)
|
|
38
|
+
assert job_holding(child) is None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_stop_child_should_end_a_child_that_still_runs() -> None:
|
|
42
|
+
if sys.platform == "win32":
|
|
43
|
+
pytest.skip("POSIX process groups")
|
|
44
|
+
child = started_child(SLEEPING_CHILD, None, None)
|
|
45
|
+
assert child is not None
|
|
46
|
+
stop_child(child, None)
|
|
47
|
+
assert child.poll() == -signal.SIGKILL
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_close_job_should_close_nothing_without_a_job() -> None:
|
|
51
|
+
assert close_job(None) is False
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_close_job_should_close_the_job_handle_it_is_given(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
55
|
+
all_closed: list[int] = []
|
|
56
|
+
|
|
57
|
+
class FakeKernel32:
|
|
58
|
+
def CloseHandle(self, job: int) -> int:
|
|
59
|
+
all_closed.append(job)
|
|
60
|
+
return 1
|
|
61
|
+
|
|
62
|
+
monkeypatch.setattr(gate_child_process, "_kernel32", FakeKernel32)
|
|
63
|
+
assert close_job(FAKE_JOB_HANDLE) is True
|
|
64
|
+
assert all_closed == [FAKE_JOB_HANDLE]
|