8OI-510 / FS-516 regression coverage — :func:`build_test_index` must
9walk ``tests_dir`` exactly once per linter run and produce two lookup
10sets on :class:`LinterContext`. :func:`check_tests_exist` then becomes
11O(1) per call, replacing the previous O(N·M) rglob-and-read loop that
12dominated ``oct lint`` runtime on medium-to-large projects.
16- Verify first call builds and caches ``ctx.test_index``; subsequent
17 calls return the cached dict without re-walking.
18- Verify both naming-convention and import-statement paths populate the
20- Verify ``check_tests_exist`` returns True for either lookup path.
27 L3 — assertion details
32- First call builds and caches ``ctx.test_index``; subsequent calls
33 return the cached dict without re-walking.
34- Both naming-convention and import-statement paths populate the index.
35- ``check_tests_exist`` returns True for either path.
38from __future__
import annotations
41from pathlib
import Path
54 diagnostics_dir=project /
"oc_diagnostics",
55 tests_dir=project /
"tests",
56 docs_dir=project /
"docs",
57 log_lock=threading.Lock(),
62 """OI-510: name-matched and import-matched stems both land in the index."""
63 (tmp_path /
"tests").mkdir()
64 (tmp_path /
"tests" /
"test_foo.py").write_text(
"assert True\n", encoding=
"utf-8")
65 (tmp_path /
"tests" /
"test_explicit.py").write_text(
66 "from bar import baz\nimport qux\n", encoding=
"utf-8",
69 index = build_test_index(ctx)
70 assert "foo" in index[
"name_stems"]
71 assert "explicit" in index[
"name_stems"]
72 assert "bar" in index[
"imported_stems"]
73 assert "qux" in index[
"imported_stems"]
77 """OI-510: the index is built on first call and reused thereafter."""
78 (tmp_path /
"tests").mkdir()
79 (tmp_path /
"tests" /
"test_a.py").write_text(
"\n", encoding=
"utf-8")
81 first = build_test_index(ctx)
83 (tmp_path /
"tests" /
"test_b.py").write_text(
"\n", encoding=
"utf-8")
84 second = build_test_index(ctx)
85 assert first
is second
86 assert "b" not in second[
"name_stems"]
90 """OI-510: check_tests_exist returns True via either path, False otherwise."""
91 (tmp_path /
"tests").mkdir()
92 (tmp_path /
"tests" /
"test_named_hit.py").write_text(
"\n", encoding=
"utf-8")
93 (tmp_path /
"tests" /
"test_imports.py").write_text(
94 "import import_hit\n", encoding=
"utf-8",
97 assert check_tests_exist(Path(
"src/named_hit.py"), ctx)
is True
98 assert check_tests_exist(Path(
"src/import_hit.py"), ctx)
is True
99 assert check_tests_exist(Path(
"src/no_hit.py"), ctx)
is False
103 """OI-510: absent tests/ folder → empty sets, no exception."""
105 index = build_test_index(ctx)
106 assert index[
"name_stems"] == set()
107 assert index[
"imported_stems"] == set()
108 assert check_tests_exist(Path(
"anything.py"), ctx)
is False
test_check_tests_exist_uses_index(Path tmp_path)
test_index_is_cached(Path tmp_path)
test_index_populates_both_sets(Path tmp_path)
test_missing_tests_dir_yields_empty_index(Path tmp_path)
LinterContext _make_ctx(Path project)