Repository navigation
Reuse check receipts after a final checkout check #1784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4452eab
f858f58
bd3c9b9
9aaeb12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,8 +184,22 @@ def _checkpoint_success(checkpoint: Callable[[CheckReport], None] | None, | |
| checkpoint(replace(report, results=(*report.results, *unvisited, incomplete))) | ||
|
|
||
|
|
||
| def has_untracked_task_state(config: Config, worktree: Path) -> bool: | ||
| return not _git(config, worktree, "ls-files", "--", ".loopzero") | ||
|
|
||
|
|
||
| def receipt_checkout_matches(config: Config, worktree: Path, report: CheckReport) -> bool: | ||
| if not _stage_unchanged(config, worktree, report.head): | ||
| return False | ||
| base = _check_base(config, worktree) | ||
| return all( | ||
| result.input_key is not None and result.input_key == _input_key(config, worktree, result.command, base) | ||
| for result in report.results if result.command in config.inputs or "*" in config.inputs | ||
| ) and _stage_unchanged(config, worktree, report.head) | ||
|
|
||
|
|
||
| def _previous(config: Config, worktree: Path, settings_sha256: str) -> dict[str, CheckResult]: | ||
| if _git(config, worktree, "ls-files", "--", ".loopzero"): | ||
| if not has_untracked_task_state(config, worktree): | ||
| return {} | ||
| try: | ||
| data = json.loads((worktree / ".loopzero/checks.json").read_text()) | ||
|
|
@@ -275,22 +289,29 @@ def _input_blobs(config: Config, worktree: Path, paths: tuple[str, ...]) -> str | |
| raise | ||
|
|
||
|
|
||
| def _has_inputs(config: Config, command: str) -> bool: | ||
| return command in config.inputs or "." in config.inputs.get("*", ()) | ||
|
|
||
|
|
||
| def _input_key(config: Config, worktree: Path, command: str, base: str, *, | ||
| status: Callable[[str], None] | None = None, | ||
| workflow: str | Path = "workflow.toml") -> str | None: | ||
| if command not in config.inputs and "." not in config.inputs.get("*", ()): | ||
| if not _has_inputs(config, command): | ||
| return None | ||
| if not base and status is None: | ||
| return None | ||
| entries = {key: config.inputs[key] for key in (command, "*") if key in config.inputs} | ||
| paths = tuple(path for group in entries.values() for path in group) | ||
| try: | ||
| blobs = _input_blobs(config, worktree, tuple(path for paths in entries.values() for path in paths)) | ||
| blobs = _input_blobs(config, worktree, paths) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. important: Check exclusion pathspecs against the paths they exclude With |
||
| if blobs and len(paths) > 1 and not all(_input_blobs(config, worktree, (path,)) for path in paths): | ||
| blobs = "" | ||
| except (SandboxUnavailable, _proc.ProcTimeout, OSError, ValueError): | ||
| if not base: | ||
| return None | ||
| raise | ||
| if blobs == "" and status: | ||
| status(f"{workflow}: checks.inputs for {command!r} matches no files in HEAD. " | ||
| status(f"{workflow}: checks.inputs for {command!r} is empty or has a pathspec that matches no files in HEAD. " | ||
| f"Correct these entries: {entries!r}. Running the check fresh.") | ||
| if not blobs or not base: | ||
| return None | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ | |
| from loopzero.cli import _current_check_report, _save_report | ||
| from loopzero.types import Config, ResourceLimits | ||
|
|
||
| from .conftest import git | ||
| from .conftest import commit_file, git | ||
|
|
||
| FAKE_BWRAP = '''#!/usr/bin/python3 | ||
| import os | ||
|
|
@@ -744,6 +744,7 @@ def cache(config, *, clean=False, verify_reuse=False): | |
| 'failed', 'boolean_exit_code', 'dirty_receipt', 'keyless', | ||
| 'dirty_tree', 'legacy', 'grant']) | ||
| def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, change, cache_check, receipt_path): | ||
| commit_file(git_repo, "shared", "initial", "add shared input") | ||
| cfg = make_config(checks=('echo one', 'echo two'), | ||
| inputs={'echo one': ('*.md',), 'echo two': ('*.md',), '*': ('shared',)}) | ||
| if change == 'grant': | ||
|
|
@@ -754,7 +755,7 @@ def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, ch | |
| whole = replace(cfg, inputs={'*': ('.',)}) | ||
| key = sandbox._input_key(whole, git_repo, 'unlisted', fetched_base) | ||
| assert key is not None | ||
| cache_check(cfg, clean=True) | ||
| initial_head = cache_check(cfg, clean=True).head | ||
| if change in {'other', 'input', 'shared', 'dirty_tree'}: | ||
| path = 'README.md' if change in {'input', 'dirty_tree'} else 'src.py' if change == 'other' else change | ||
| (git_repo / path).write_text('new content') | ||
|
|
@@ -768,12 +769,12 @@ def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, ch | |
| cfg = replace(cfg, network=True) | ||
| elif change == 'environment': | ||
| monkeypatch.setenv('LZ_KEEP', 'changed') | ||
| assert _current_check_report(git_repo, cfg, fetched_base) is None | ||
| assert _current_check_report(git_repo, cfg, initial_head) is None | ||
| elif change == 'grant': | ||
| monkeypatch.setenv("HEAVY_SLOT_GRANTED_CORES", "4") | ||
| assert _current_check_report(git_repo, cfg, fetched_base) is not None | ||
| assert _current_check_report(git_repo, cfg, initial_head) is not None | ||
| monkeypatch.delenv("HEAVY_SLOT_GRANTED_CORES") | ||
| assert _current_check_report(git_repo, cfg, fetched_base) is not None | ||
| assert _current_check_report(git_repo, cfg, initial_head) is not None | ||
| elif change == 'version': | ||
| monkeypatch.setattr(sandbox, '__version__', 'new') | ||
| else: | ||
|
|
@@ -799,7 +800,7 @@ def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, ch | |
| @pytest.mark.parametrize("case", ["typo", "empty", "dirty", "no_base"]) | ||
| def test_unmatched_inputs_run_fresh(git_repo, bwrap_log, fetched_base, case, cache_check): | ||
| command = 'test "$(cat README.md)" = hello' | ||
| paths = () if case == "empty" else ("ipnut.txt",) | ||
| paths = {"empty": (), "typo": ("README.md", "ipnut.txt")}.get(case, ("ipnut.txt",)) | ||
| config = make_config(checks=(command,), inputs={command: paths}) | ||
| cache_check(config) | ||
| (git_repo / "README.md").write_text("fail\n") | ||
|
|
@@ -830,22 +831,22 @@ def timed_out(*args): | |
| assert report.ok and report.dirty and report.results[0].command == command | ||
|
|
||
|
|
||
| def test_local_check_reuse_tracks_checkout_bytes(git_repo, bwrap_log, fetched_base, cache_check): | ||
| @pytest.mark.parametrize("partial_wildcard", [False, True]) | ||
| def test_local_check_reuse_tracks_checkout_bytes(git_repo, fetched_base, cache_check, monkeypatch, partial_wildcard): | ||
| path = git_repo / "README.md" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. important: Restore the fake bwrap fixture for both checkout-byte cases
|
||
| command = "python3 -c 'from pathlib import Path; exit(13 in Path(\"README.md\").read_bytes())'" | ||
| config = make_config(checks=(command,), inputs={command: ("README.md",)}) | ||
| assert path.read_bytes() == b"hello\n" | ||
| assert cache_check(config, clean=True, verify_reuse=True).head == fetched_base | ||
|
|
||
| config = make_config(checks=(command,), inputs={"*" if partial_wildcard else command: ("README.md",)}) | ||
| assert cache_check(config, clean=True, verify_reuse=not partial_wildcard).head == fetched_base | ||
| monkeypatch.setattr(cli, "_context", lambda *args, **kwargs: (git_repo, config)) | ||
| git(git_repo, "config", "core.autocrlf", "true") | ||
| path.unlink() | ||
| git(git_repo, "checkout-index", "-u", "--", path.name) | ||
| assert path.read_bytes() == b"hello\r\n" | ||
| assert not git(git_repo, "status", "--porcelain") | ||
| report = sandbox.run_checks(config, git_repo) | ||
| assert cli.main(["check"]) == 1 | ||
| report = cli._load_report(git_repo) | ||
| assert not report.dirty and report.head == fetched_base | ||
| assert report.results[0].exit_code == 1 | ||
| assert report.results[0].reused_from is None | ||
| assert (report.results[0].exit_code, report.results[0].reused_from) == (1, None) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("base", ["tracked", "recorded", "absent"]) | ||
|
|
@@ -928,7 +929,7 @@ def fail_fingerprint(argv, **kwargs): | |
|
|
||
|
|
||
| @pytest.mark.parametrize('symlink', [False, True]) | ||
| def test_tracked_forged_receipt_runs_failing_stage(git_repo, bwrap_log, fetched_base, symlink): | ||
| def test_tracked_forged_receipt_runs_failing_stage(git_repo, bwrap_log, fetched_base, symlink, monkeypatch): | ||
| from loopzero.types import CheckReport, CheckResult | ||
|
|
||
| command = 'echo stage-ran; exit 7' | ||
|
|
@@ -946,12 +947,12 @@ def test_tracked_forged_receipt_runs_failing_stage(git_repo, bwrap_log, fetched_ | |
| git(git_repo, 'commit', '-m', 'forge passing receipt') | ||
| assert not git(git_repo, 'status', '--porcelain').strip() | ||
| assert key == sandbox._input_key(cfg, git_repo, command, fetched_base) | ||
|
|
||
| receipt_reader = Mock(side_effect=AssertionError("tracked receipt opened")) | ||
| monkeypatch.setattr(cli, "_current_check_report", receipt_reader) | ||
| assert cli._reusable_check_report(git_repo, cfg) is None | ||
| report = sandbox.run_checks(cfg, git_repo) | ||
| assert not report.dirty | ||
| assert report.results[0].exit_code == 7 | ||
| assert not report.dirty and (report.results[0].exit_code, report.results[0].reused_from) == (7, None) | ||
| assert 'stage-ran' in report.results[0].tail | ||
| assert not report.results[0].reused_from | ||
|
|
||
|
|
||
| def test_no_inputs_preserves_receipt(git_repo, bwrap_log): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.