"""Tests for ``muse sparse-checkout`` — partial working-tree materialization. Coverage tiers: - Unit: init creates config; set replaces patterns; add appends patterns; list shows patterns; disable removes config; cone matching; pattern (glob) matching; filter_manifest_sparse; auto-read from root - Integration: checkout respects sparse config; disable restores full tree; cone mode directory filtering; pattern mode glob filtering; JSON output for list; init --no-cone switches to pattern mode - Security: ANSI injection in pattern name rejected; path traversal in pattern rejected - Stress: 200-file manifest filtered to cone subdirectory (≤ 20 files) """ from __future__ import annotations from collections.abc import Mapping import datetime import json import pathlib import pytest from tests.cli_test_helper import CliRunner from muse.core.object_store import write_object from muse.core.paths import muse_dir, sparse_checkout_path from muse.core.commits import ( CommitRecord, write_commit, ) from muse.core.snapshots import ( SnapshotRecord, write_snapshot, ) from muse.core.types import Manifest, blob_id, load_json_file runner = CliRunner() _REPO_ID = "sparse-checkout-test" # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _init_repo(path: pathlib.Path) -> pathlib.Path: dot_muse = muse_dir(path) for d in ("commits", "snapshots", "objects", "refs/heads", "code"): (dot_muse / d).mkdir(parents=True, exist_ok=True) (dot_muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") (dot_muse / "repo.json").write_text( json.dumps({"repo_id": _REPO_ID, "domain": "code"}), encoding="utf-8" ) return path def _env(repo: pathlib.Path) -> Mapping[str, str]: return {"MUSE_REPO_ROOT": str(repo)} def _write_files(root: pathlib.Path, files: Mapping[str, bytes]) -> Manifest: manifest: Manifest = {} for rel_path, content in files.items(): obj_id = blob_id(content) write_object(root, obj_id, content) manifest[rel_path] = obj_id abs_path = root / rel_path abs_path.parent.mkdir(parents=True, exist_ok=True) abs_path.write_bytes(content) return manifest def _invoke(args: list[str], repo: pathlib.Path) -> tuple[int, str, str]: """Invoke muse with MUSE_REPO_ROOT set; return (exit_code, stdout, stderr).""" result = runner.invoke(None, args, env=_env(repo)) return result.exit_code, result.stdout, result.stderr def _sparse_config(repo: pathlib.Path) -> pathlib.Path: return sparse_checkout_path(repo) # --------------------------------------------------------------------------- # Unit — init # --------------------------------------------------------------------------- class TestInit: def test_init_creates_config_file(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") rc, out, err = _invoke(["sparse-checkout", "init"], repo) assert rc == 0 assert _sparse_config(repo).exists() def test_init_default_mode_is_cone(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") _invoke(["sparse-checkout", "init"], repo) cfg = load_json_file(_sparse_config(repo)) assert cfg["mode"] == "cone" def test_init_no_cone_sets_pattern_mode(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") rc, out, err = _invoke(["sparse-checkout", "init", "--no-cone"], repo) assert rc == 0 cfg = load_json_file(_sparse_config(repo)) assert cfg["mode"] == "pattern" def test_init_idempotent(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/"], repo) rc, out, err = _invoke(["sparse-checkout", "init"], repo) assert rc == 0 assert _sparse_config(repo).exists() # --------------------------------------------------------------------------- # Unit — set # --------------------------------------------------------------------------- class TestSet: def test_set_writes_patterns(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") _invoke(["sparse-checkout", "init"], repo) rc, out, err = _invoke(["sparse-checkout", "set", "src/", "tests/"], repo) assert rc == 0 cfg = load_json_file(_sparse_config(repo)) assert cfg["patterns"] == ["src/", "tests/"] def test_set_replaces_existing(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "old/"], repo) _invoke(["sparse-checkout", "set", "new/"], repo) cfg = load_json_file(_sparse_config(repo)) assert cfg["patterns"] == ["new/"] def test_set_without_init_fails(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") rc, out, err = _invoke(["sparse-checkout", "set", "src/"], repo) assert rc != 0 def test_set_ansi_injection_rejected(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") _invoke(["sparse-checkout", "init"], repo) rc, out, err = _invoke(["sparse-checkout", "set", "\x1b[31mbad/\x1b[0m"], repo) assert rc != 0 # --------------------------------------------------------------------------- # Unit — add # --------------------------------------------------------------------------- class TestAdd: def test_add_appends_patterns(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/"], repo) rc, out, err = _invoke(["sparse-checkout", "add", "tests/"], repo) assert rc == 0 cfg = load_json_file(_sparse_config(repo)) assert "src/" in cfg["patterns"] assert "tests/" in cfg["patterns"] def test_add_deduplicates(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/"], repo) _invoke(["sparse-checkout", "add", "src/"], repo) cfg = load_json_file(_sparse_config(repo)) assert cfg["patterns"].count("src/") == 1 def test_add_without_init_fails(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") rc, out, err = _invoke(["sparse-checkout", "add", "src/"], repo) assert rc != 0 # --------------------------------------------------------------------------- # Unit — list # --------------------------------------------------------------------------- class TestList: def test_list_shows_patterns_text(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/", "docs/"], repo) rc, out, err = _invoke(["sparse-checkout", "list"], repo) assert rc == 0 assert "src/" in out assert "docs/" in out def test_list_json(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/"], repo) rc, out, err = _invoke(["sparse-checkout", "list", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["mode"] == "cone" assert "src/" in data["patterns"] def test_list_when_disabled(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") rc, out, err = _invoke(["sparse-checkout", "list"], repo) assert rc == 0 assert "disabled" in out.lower() # --------------------------------------------------------------------------- # Unit — disable # --------------------------------------------------------------------------- class TestDisable: def test_disable_removes_config(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/"], repo) rc, out, err = _invoke(["sparse-checkout", "disable"], repo) assert rc == 0 assert not _sparse_config(repo).exists() def test_disable_when_not_active_is_noop(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path / "repo") rc, out, err = _invoke(["sparse-checkout", "disable"], repo) assert rc == 0 # --------------------------------------------------------------------------- # Unit — core filter logic # --------------------------------------------------------------------------- class TestFilterLogic: def test_cone_includes_root_files(self) -> None: from muse.core.sparse import matches_sparse assert matches_sparse("README.md", ["src/"], mode="cone") assert matches_sparse("Makefile", ["src/"], mode="cone") def test_cone_includes_files_in_pattern_dir(self) -> None: from muse.core.sparse import matches_sparse assert matches_sparse("src/foo.py", ["src/"], mode="cone") assert matches_sparse("src/bar/baz.py", ["src/"], mode="cone") def test_cone_excludes_other_dirs(self) -> None: from muse.core.sparse import matches_sparse assert not matches_sparse("tests/test_foo.py", ["src/"], mode="cone") assert not matches_sparse("docs/readme.md", ["src/"], mode="cone") def test_cone_multiple_dirs(self) -> None: from muse.core.sparse import matches_sparse assert matches_sparse("src/foo.py", ["src/", "tests/"], mode="cone") assert matches_sparse("tests/test_foo.py", ["src/", "tests/"], mode="cone") assert not matches_sparse("docs/guide.md", ["src/", "tests/"], mode="cone") def test_pattern_mode_glob(self) -> None: from muse.core.sparse import matches_sparse assert matches_sparse("src/foo.py", ["src/**"], mode="pattern") assert not matches_sparse("tests/foo.py", ["src/**"], mode="pattern") def test_pattern_mode_extension(self) -> None: from muse.core.sparse import matches_sparse assert matches_sparse("foo.py", ["*.py"], mode="pattern") assert not matches_sparse("foo.txt", ["*.py"], mode="pattern") def test_filter_manifest_sparse_cone(self) -> None: from muse.core.sparse import filter_manifest_sparse manifest = { "README.md": "aaa", "src/foo.py": "bbb", "tests/test_foo.py": "ccc", "docs/guide.md": "ddd", } result = filter_manifest_sparse(manifest, ["src/"], mode="cone") assert "README.md" in result assert "src/foo.py" in result assert "tests/test_foo.py" not in result assert "docs/guide.md" not in result def test_filter_manifest_sparse_pattern(self) -> None: from muse.core.sparse import filter_manifest_sparse manifest = { "src/foo.py": "aaa", "src/bar.txt": "bbb", "tests/test_foo.py": "ccc", } result = filter_manifest_sparse(manifest, ["**/*.py"], mode="pattern") assert "src/foo.py" in result assert "tests/test_foo.py" in result assert "src/bar.txt" not in result # --------------------------------------------------------------------------- # Integration — apply_manifest auto-reads sparse config # --------------------------------------------------------------------------- class TestApplyManifestSparse: def test_apply_manifest_respects_sparse_config(self, tmp_path: pathlib.Path) -> None: """apply_manifest should only materialize files matching sparse patterns.""" from muse.core.workdir import apply_manifest repo = _init_repo(tmp_path / "repo") _sparse_config(repo).write_text( json.dumps({"mode": "cone", "patterns": ["src/"]}), encoding="utf-8" ) manifest = { "README.md": blob_id(b"readme"), "src/foo.py": blob_id(b"foo"), "tests/test_foo.py": blob_id(b"test"), } write_object(repo, blob_id(b"readme"), b"readme") write_object(repo, blob_id(b"foo"), b"foo") write_object(repo, blob_id(b"test"), b"test") apply_manifest(repo, {}, manifest) assert (repo / "README.md").exists() assert (repo / "src" / "foo.py").exists() assert not (repo / "tests" / "test_foo.py").exists() def test_apply_manifest_no_sparse_config_writes_all(self, tmp_path: pathlib.Path) -> None: """Without sparse config, apply_manifest writes everything.""" from muse.core.workdir import apply_manifest repo = _init_repo(tmp_path / "repo") manifest = { "README.md": blob_id(b"readme"), "src/foo.py": blob_id(b"foo"), "tests/test_foo.py": blob_id(b"test"), } write_object(repo, blob_id(b"readme"), b"readme") write_object(repo, blob_id(b"foo"), b"foo") write_object(repo, blob_id(b"test"), b"test") apply_manifest(repo, {}, manifest) assert (repo / "README.md").exists() assert (repo / "src" / "foo.py").exists() assert (repo / "tests" / "test_foo.py").exists() # --------------------------------------------------------------------------- # Stress — 200-file manifest filtered to cone # --------------------------------------------------------------------------- class TestStress: def test_200_file_manifest_cone_filter(self) -> None: from muse.core.sparse import filter_manifest_sparse manifest: Manifest = {} for i in range(100): manifest[f"src/file_{i:03d}.py"] = blob_id(f"src-{i}".encode()) for i in range(100): manifest[f"other/file_{i:03d}.py"] = blob_id(f"other-{i}".encode()) manifest["README.md"] = blob_id(b"readme") result = filter_manifest_sparse(manifest, ["src/"], mode="cone") # src/ files + root files assert len(result) == 101 # 100 src + README assert all( k.startswith("src/") or "/" not in k for k in result ) # --------------------------------------------------------------------------- # Flag registration tests # --------------------------------------------------------------------------- class TestRegisterFlags: def _parser(self) -> "argparse.ArgumentParser": import argparse from muse.cli.commands.sparse_checkout import register p = argparse.ArgumentParser() subs = p.add_subparsers() register(subs) return p def test_default_json_out_is_false(self) -> None: args = self._parser().parse_args(["sparse-checkout", "init"]) assert args.json_out is False def test_json_flag_sets_json_out(self) -> None: args = self._parser().parse_args(["sparse-checkout", "init", "--json"]) assert args.json_out is True def test_j_shorthand_sets_json_out(self) -> None: args = self._parser().parse_args(["sparse-checkout", "init", "-j"]) assert args.json_out is True