"""Discovery and filtering. A wrongly excluded file is invisible to search with no error, which is a worse failure than indexing some noise. These tests pin both directions. """ from __future__ import annotations import hashlib import os import subprocess import pytest from code_index import files as F def make_repo(tmp_path, paths: dict[str, str]) -> str: repo = tmp_path / "repo" repo.mkdir() subprocess.run(["git", "-C", str(repo), "init", "-q"], check=True) for rel, content in paths.items(): target = repo / rel target.parent.mkdir(parents=True, exist_ok=True) target.write_text(content, encoding="utf-8") subprocess.run(["git", "-C", str(repo), "add", "-A"], check=True) return str(repo) def discovered(repo: str) -> set[str]: found, _ = F.discover(repo) return {sf.rel for sf in found} def test_dependency_and_build_trees_are_excluded(tmp_path): repo = make_repo(tmp_path, { "src/app.ts": "export const a = 1;\n" * 5, "node_modules/dep/index.js": "module.exports = 1;\n" * 5, "dist/bundle.js": "var x=1;\n" * 5, "target/debug/out.rs": "fn main() {}\n" * 5, ".venv/lib/thing.py": "x = 1\n" * 5, }) assert discovered(repo) == {"src/app.ts"} def test_directories_that_often_hold_generated_files_are_kept(tmp_path): """These were excluded once and it hid real code, so the choice is pinned.""" repo = make_repo(tmp_path, { "bin/deploy.sh": "#!/bin/sh\necho hi\n" * 5, "assets/theme.css": "body { color: red; }\n" * 5, "migrations/001_init.sql": "CREATE TABLE t (id int);\n" * 5, "fixtures/sample.json": '{"a": 1}\n' * 5, }) assert discovered(repo) == { "bin/deploy.sh", "assets/theme.css", "migrations/001_init.sql", "fixtures/sample.json", } def test_lockfiles_and_binaries_are_excluded(tmp_path): repo = make_repo(tmp_path, { "src/app.ts": "export const a = 1;\n" * 5, "package-lock.json": '{"lockfileVersion": 3}\n' * 5, "yarn.lock": "# yarn\n" * 5, "Cargo.lock": "[[package]]\n" * 5, "logo.png": "\x89PNG\r\n\x1a\n" + "x" * 200, "app.min.js": "var a=1;\n", }) assert discovered(repo) == {"src/app.ts"} def test_minified_content_is_excluded(tmp_path): repo = make_repo(tmp_path, { "src/app.ts": "export const a = 1;\n" * 20, "vendor-ish.js": "var a=1;" * 900, # one enormous line }) found = discovered(repo) assert "src/app.ts" in found assert "vendor-ish.js" not in found def test_untracked_files_are_ignored(tmp_path): """Discovery walks git-tracked files so .gitignore needs no reimplementation.""" repo = make_repo(tmp_path, {"src/app.ts": "export const a = 1;\n" * 5}) (tmp_path / "repo" / "src" / "scratch.ts").write_text("const tmp = 1;\n" * 5) assert discovered(repo) == {"src/app.ts"} def test_binary_content_is_detected_without_an_extension(tmp_path): repo = make_repo(tmp_path, {"src/app.ts": "export const a = 1;\n" * 5}) (tmp_path / "repo" / "blob").write_bytes(b"\x00\x01\x02" * 500) subprocess.run(["git", "-C", repo, "add", "-A"], check=True) assert "blob" not in discovered(repo) def test_oversized_files_are_excluded(tmp_path): repo = make_repo(tmp_path, { "src/app.ts": "export const a = 1;\n" * 5, "huge.txt": "line of text here\n" * 40_000, }) found = discovered(repo) assert "src/app.ts" in found assert "huge.txt" not in found def test_content_hash_changes_with_content(tmp_path): """Incremental reindexing keys off this hash, so it must track edits.""" repo = make_repo(tmp_path, {"src/app.ts": "export const a = 1;\n" * 5}) before = {sf.rel: sf.sha for sf in F.discover(repo)[0]} (tmp_path / "repo" / "src" / "app.ts").write_text("export const a = 2;\n" * 5) after = {sf.rel: sf.sha for sf in F.discover(repo)[0]} assert before["src/app.ts"] != after["src/app.ts"] def test_read_hashes_the_text_it_returns(tmp_path): """Discovery's hash is hours old by the time a full build reads the file. The hash stored alongside the text has to describe that text, or an edit made during the run is indexed under the hash of the version before it. """ repo = make_repo(tmp_path, {"src/app.ts": "export const a = 1;\n" * 5}) sf = next(f for f in F.discover(repo)[0] if f.rel == "src/app.ts") (tmp_path / "repo" / "src" / "app.ts").write_text("export const a = 2;\n" * 5) text, sha = sf.read() assert "a = 2" in text assert sha != sf.sha, "returned the discovery hash rather than the text's own" assert sha == hashlib.sha256(text.encode()).hexdigest() def test_find_repos_handles_a_repo_root_directly(tmp_path): repo = make_repo(tmp_path, {"src/app.ts": "x\n"}) assert F.find_repos(repo) == [os.path.abspath(repo)] def test_find_repos_finds_children(tmp_path): make_repo(tmp_path, {"src/app.ts": "x\n"}) found = F.find_repos(str(tmp_path)) assert found == [os.path.abspath(str(tmp_path / "repo"))] def test_non_repo_directory_yields_nothing(tmp_path): (tmp_path / "plain").mkdir() assert F.find_repos(str(tmp_path)) == []