Repository navigation
fix(repo): refuse a --cache-dir that is not a usable directory with exit 2 #828
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
dokimi/changelog.d/20261116_150000_antonwolfy_repo-cache-dir-usable.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ### Fixed | ||
|
|
||
| - **`repo` commands refuse a `--cache-dir` that is not a usable directory with exit 2 and a message naming the flag and the path** — a regular file, or a path below one, used to end in `✗ [Errno 17] File exists` and exit 1 (a failed run in `--json`) after the command had started; the value is now checked before any clone, as `skill shorten` and `skill novelty analyze` do. A bad `--cache-dir` next to a local-directory source is refused too, where it used to be ignored. An existing read-only directory still passes, so a populated cache replays; an empty read-only one fails at the clone with exit 1. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Copyright (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """The up-front check of a ``--cache-dir`` value, shared by the novelty and repo commands (#701, #765).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from pathlib import Path | ||
|
|
||
| RETENTION_USE = "the retention verdicts can be stored in" | ||
| CLONE_USE = "the repository clones can be cached in" | ||
|
|
||
|
|
||
| def usable_cache_dir(raw: str | None, use: str = RETENTION_USE) -> Path | None: | ||
| """``--cache-dir`` as a path a cache can store into, or ``None`` when unset. | ||
|
|
||
| A directory that does not exist yet is fine (created on the first store) when an existing, | ||
| writable directory is above it; one that exists must be a readable directory, writable or | ||
| not (a fully cached run replays from it). Anything else raises ``InputError`` before a paid | ||
| call or a clone (#701, #765). ``use`` closes the message: "pass a directory <use>". | ||
| """ | ||
| from dokimi.utils import InputError | ||
|
|
||
| if not raw: | ||
| return None | ||
| path = Path(raw) | ||
| probe = path | ||
| try: | ||
| while not probe.exists() and not probe.is_symlink() and probe != probe.parent: | ||
| probe = probe.parent | ||
| is_dir = probe.is_dir() | ||
| except OSError as exc: | ||
| # An ancestor without search permission: exists()/is_dir() raise PermissionError (Python < 3.14). | ||
| raise InputError(f"--cache-dir {path}: cannot inspect {probe} ({exc.strerror or type(exc).__name__}); " | ||
| f"pass a directory {use}") from exc | ||
| if not is_dir: | ||
| where = "is not a directory" if probe == path else f"has {probe} above it, which is not a directory" | ||
| raise InputError(f"--cache-dir {path} {where}; pass a directory {use}") | ||
| # An existing directory only has to be readable: a fully cached run replays from a read-only | ||
| # directory and writes nothing. A missing one is created on the first store, so it needs to be writable. | ||
| needed, word = (os.R_OK | os.X_OK, "readable") if probe == path else (os.W_OK | os.X_OK, "writable") | ||
| if not os.access(probe, needed): | ||
|
antonwolfy marked this conversation as resolved.
Resolved
|
||
| raise InputError(f"--cache-dir {path}: {probe} is not {word}; pass a directory {use}") | ||
| return path | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # Copyright (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """`--cache-dir` of the `repo` commands is checked before any clone (#765): a bad value exits 2 naming the flag.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| import subprocess | ||
|
|
||
| import pytest | ||
|
|
||
| from dokimi import cli | ||
| from dokimi.commands.repo import _cache_dir | ||
| from tests.cli_refusal import refusal | ||
|
|
||
| PIN = "HEAD" | ||
|
|
||
|
|
||
| def _argv(monkeypatch, *argv): | ||
| monkeypatch.setattr("sys.argv", ["dokimi", *argv]) | ||
|
|
||
|
|
||
| def _source(tmp_path) -> str: | ||
| return f"file://{tmp_path}/nothere@{PIN}" | ||
|
|
||
|
|
||
| def _commands(tmp_path): | ||
| return { | ||
| "fetch": ["repo", "fetch", _source(tmp_path)], | ||
| "scan": ["repo", "scan", _source(tmp_path)], | ||
| "reach": ["repo", "reach", _source(tmp_path), "--model", "m"], | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("command", ["fetch", "scan", "reach"]) | ||
| def test_repo_command_refuses_a_cache_dir_that_is_a_regular_file(command, tmp_path, monkeypatch, capsys): | ||
| cache = tmp_path / "cachefile" | ||
| cache.write_text("x", encoding="utf-8") | ||
| _argv(monkeypatch, *_commands(tmp_path)[command], "--cache-dir", str(cache)) | ||
| with refusal() as exc: | ||
| cli.main() | ||
| err = capsys.readouterr().err | ||
| assert exc.code == 2 | ||
| assert f"--cache-dir {cache} is not a directory; pass a directory the repository clones can be cached in" in err | ||
| assert "File exists" not in err and cache.read_text(encoding="utf-8") == "x" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("kind", ["check", "replay"]) | ||
| def test_eval_commands_that_clone_refuse_a_cache_dir_that_is_a_regular_file(kind, tmp_path, monkeypatch, capsys): | ||
| threads = tmp_path / "threads.json" | ||
| threads.write_text("[]", encoding="utf-8") | ||
| cache = tmp_path / "cachefile" | ||
| cache.write_text("x", encoding="utf-8") | ||
| argv = {"check": ["eval", "check", _source(tmp_path)], | ||
| "replay": ["eval", "tasks", "generate", "--from", "replay", _source(tmp_path), "--threads", str(threads), | ||
| "--out-dir", str(tmp_path / "out")]}[kind] | ||
| _argv(monkeypatch, *argv, "--cache-dir", str(cache)) | ||
| with refusal() as exc: | ||
| cli.main() | ||
| assert exc.code == 2 | ||
| assert f"--cache-dir {cache} is not a directory" in capsys.readouterr().err | ||
|
|
||
|
|
||
| def test_repo_fetch_refuses_a_child_of_a_regular_file_and_makes_nothing(tmp_path, monkeypatch, capsys): | ||
| blocker = tmp_path / "file" | ||
| blocker.write_text("x", encoding="utf-8") | ||
| _argv(monkeypatch, *_commands(tmp_path)["fetch"], "--cache-dir", str(blocker / "cache")) | ||
| with refusal() as exc: | ||
| cli.main() | ||
| err = capsys.readouterr().err | ||
| assert exc.code == 2 | ||
| assert f"--cache-dir {blocker / 'cache'} has {blocker} above it, which is not a directory" in err | ||
| assert sorted(p.name for p in tmp_path.iterdir()) == ["file"] | ||
|
|
||
|
|
||
| def test_repo_fetch_json_reports_the_bad_flag_as_input_not_a_failed_run(tmp_path, monkeypatch, capsys): | ||
| cache = tmp_path / "cachefile" | ||
| cache.write_text("x", encoding="utf-8") | ||
| _argv(monkeypatch, "repo", "fetch", _source(tmp_path), "--cache-dir", str(cache), "--json") | ||
| with refusal() as exc: | ||
| cli.main() | ||
| document = json.loads(capsys.readouterr().out) | ||
| assert exc.code == 2 | ||
| assert document["status"] == "failed" | ||
| assert f"--cache-dir {cache} is not a directory" in json.dumps(document) | ||
| assert "exit 1: see stderr" not in json.dumps(document) | ||
|
|
||
|
|
||
| def test_repo_scan_of_a_local_directory_also_refuses_a_bad_cache_dir(tmp_path, monkeypatch, capsys): | ||
| repo = tmp_path / "repo" | ||
| repo.mkdir() | ||
| cache = tmp_path / "cachefile" | ||
| cache.write_text("x", encoding="utf-8") | ||
| _argv(monkeypatch, "repo", "scan", str(repo), "--cache-dir", str(cache)) | ||
| with refusal() as exc: | ||
| cli.main() | ||
| assert exc.code == 2 | ||
| assert f"--cache-dir {cache} is not a directory" in capsys.readouterr().err | ||
|
|
||
|
|
||
| def test_cache_dir_passes_unset_an_existing_directory_and_a_missing_child_unchanged(tmp_path): | ||
| def args(value): | ||
| return argparse.Namespace(cache_dir=value) | ||
|
|
||
| assert _cache_dir(args(None)) is None | ||
| assert _cache_dir(args("")) is None | ||
| assert _cache_dir(args(str(tmp_path))) == tmp_path | ||
| assert _cache_dir(args(str(tmp_path / "new" / "deeper"))) == tmp_path / "new" / "deeper" | ||
| assert not (tmp_path / "new").exists() | ||
|
|
||
|
|
||
| def test_repo_fetch_prints_the_sha_of_a_pinned_source_in_a_writable_cache_dir(tmp_path, monkeypatch, capsys): | ||
| src = tmp_path / "src" | ||
| src.mkdir() | ||
| (src / "a.txt").write_text("a", encoding="utf-8") | ||
| git = ["git", "-C", str(src), "-c", "user.name=t", "-c", "user.email=t@example.com"] | ||
| subprocess.run([*git, "init", "-q"], check=True) | ||
| subprocess.run([*git, "add", "-A"], check=True) | ||
| subprocess.run([*git, "commit", "-qm", "init"], check=True) | ||
| sha = subprocess.run([*git, "rev-parse", "HEAD"], check=True, capture_output=True, text=True).stdout.strip() | ||
| cache = tmp_path / "cache" | ||
| cache.mkdir() | ||
| _argv(monkeypatch, "repo", "fetch", f"file://{src}@{sha}", "--cache-dir", str(cache)) | ||
| cli.main() | ||
| assert capsys.readouterr().out.startswith(f"{sha}\t") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.