testscope.py¶
clew/testscope.py
Which indexed files are TEST code, so a bare name prefers the library over a helper.
_matcher
re.Pattern[str] clew.testscope._matcher(tuple[str,...] patterns)
One compiled regex equivalent to "any of these globs matches".
Source · 10 lines
def _matcher(patterns: tuple[str, ...]) -> re.Pattern[str]:
"""`fnmatch.fnmatch` IS `normcase` + the translated regex; on POSIX `normcase` is the identity,
so an alternation of the translated patterns is the same predicate, compiled once.
@brief Compile the globs.
@return The regex.
"""
return re.compile("|".join(f"(?:{fnmatch.translate(p)})" for p in patterns) or "(?!)")
Activity · 1 steps
Flows
is_test_path
bool clew.testscope.is_test_path(str rel, tuple[str,...]|list[str] patterns)
Whether one repo-relative path is test code under the given patterns.
Called by _is_test_file, _test_files, covering_tests, import_req_test_edges, is_test, mark_test_scope, test_a_declaration_displaces_the_default_patterns, test_an_e2e_directory_is_test_code, test_the_default_patterns_match_the_common_layouts, traceability
Calls _matcher
Source · 18 lines
def is_test_path(rel: str, patterns: tuple[str, ...] | list[str]) -> bool:
"""Matched against BOTH the full relative path and the basename, because the two pattern
families in `TEST_PATH_FACTS` mean different things: `tests/*` is about location and
`*_test.*` is about the file itself. Requiring one form to serve both would silently drop
whichever family lost.
A leading `./` is stripped so a path spelled either way matches the same patterns.
@brief Test whether a path is test code.
@return True when it matches any pattern.
@version 1
"""
candidate = rel.lstrip("./")
name = PurePosixPath(candidate).name
matcher = _matcher(tuple(patterns))
return bool(matcher.match(candidate) or matcher.match(name))
Activity · 4 steps
Flows
mark_test_scope
int clew.testscope.mark_test_scope(sqlite3.Connection conn, tuple[str,...]|list[str] patterns)
Stamp the indexed files that are test code, for the resolver to join against.
Source · 32 lines
def mark_test_scope(conn: sqlite3.Connection, patterns: tuple[str, ...] | list[str]) -> int:
"""WRITES THE TABLE EVEN WHEN NOTHING MATCHES, and that is the load-bearing detail. An
ABSENT table means "this index predates the stage" and the resolver keeps its old ordering;
an EMPTY table means "this stage ran and found no test code", which is a real answer about
a repo that has none. Collapsing the two would make an old index and a test-free repo
indistinguishable, and the resolver would silently behave differently on each.
Rebuilt from scratch on every build rather than merged, because the pattern set can shrink
as well as grow and a stale mark is worse than no mark: it would demote a library
definition on the strength of a rule the operator has since withdrawn.
@brief Record which path rows are test code.
@return The count marked.
@version 1
"""
conn.executescript(_DDL)
conn.execute("DELETE FROM test_scope")
rows = [
(rowid,)
for rowid, name in conn.execute("SELECT rowid, name FROM path WHERE type = 1")
if is_test_path(str(name), patterns)
]
conn.executemany("INSERT OR IGNORE INTO test_scope(path_rowid) VALUES (?)", rows)
total = conn.execute("SELECT COUNT(*) FROM path WHERE type = 1").fetchone()[0]
logger.info(
"test scope: %d of %d indexed file(s) are test code under %d pattern(s); a bare "
"ambiguous name now prefers a definition outside them",
len(rows),
total,
len(patterns),
)
return len(rows)1 more lines, not shown