mirror of
https://github.com/unclebob/mutator.git
synced 2026-11-26 18:58:50 +00:00
Skip test directories only at the project root or under src/test.
Why: crapper.discover treats any test-named folder at any depth as a test path. That silently skipped production fixtures such as src/agnesse/slim/test/SymbolManagingTableTable.java, even when the file was named directly, because "test" appeared in the path. What: Mutator now walks the tree itself. A path relative to the project root is a test path when its first component is a TEST_DIRS name (test, tests, spec, specs, or __tests__), or when it sits under src/test or src/tests, the Maven and Gradle layout. SKIP_DIRS are still skipped at any depth. Filename-based test files are still detected by crapper.discover.is_test_file, applied to the filename so the any-depth directory check is not reused. crapper itself is unchanged. README and --help describe the new rule. Proof: 12 new tests in test_sources.py plus help assertions. 84 tests were green before; 96 pass now. crapper on the changed files: every new function is under 10 (iter_source_files 9.0, is_test_path 5.0, is_mutable_source 5.0); parse_args remains 30.2, the existing option-parser exception. dryer: no candidates. mutator on sources.py: 25 killed, 0 survived, 0 uncovered. Dry run against /tmp/agn at 58fd81c37: SymbolManagingTableTable.java is discovered along with 54 other files under src/agnesse/slim/test/; no files under test/ are.
This commit is contained in:
+58
-57
@@ -13,42 +13,56 @@ from mutator.coverage import covered_lines
|
||||
from mutator.engine import mutate_file, restore_backups, scan_file
|
||||
from mutator.report import format_results, format_site_log
|
||||
from mutator.runner import CommandRunner
|
||||
from mutator.sources import is_mutable_source, is_skipped_test, iter_source_files
|
||||
|
||||
|
||||
def _skipped_directory_text() -> str:
|
||||
"""The directories the walker skips, named the same way in --help."""
|
||||
def _comma_and(names: set[str]) -> str:
|
||||
ordered = sorted(names)
|
||||
if len(ordered) == 1:
|
||||
return ordered[0]
|
||||
return ", ".join(ordered[:-1]) + ", and " + ordered[-1]
|
||||
|
||||
names = {
|
||||
".clj-kondo",
|
||||
".git",
|
||||
".hg",
|
||||
".idea",
|
||||
".metrics",
|
||||
".svn",
|
||||
".venv",
|
||||
"__pycache__",
|
||||
"__tests__",
|
||||
"build",
|
||||
"coverage",
|
||||
"dist",
|
||||
"node_modules",
|
||||
"out",
|
||||
"spec",
|
||||
"specs",
|
||||
"target",
|
||||
"test",
|
||||
"testdata",
|
||||
"tests",
|
||||
"venv",
|
||||
"vendor",
|
||||
}
|
||||
|
||||
def _directory_text(attr: str, fallback: set[str]) -> str:
|
||||
names = set(fallback)
|
||||
try:
|
||||
crapper = ensure_crapper()
|
||||
names = set(crapper.discover.SKIP_DIRS) | set(crapper.discover.TEST_DIRS)
|
||||
names = set(getattr(ensure_crapper().discover, attr))
|
||||
except ImportError:
|
||||
pass
|
||||
ordered = sorted(names)
|
||||
return ", ".join(ordered[:-1]) + ", and " + ordered[-1]
|
||||
return _comma_and(names)
|
||||
|
||||
|
||||
def _skip_directory_text() -> str:
|
||||
"""SKIP_DIRS, named the same way in --help."""
|
||||
|
||||
return _directory_text(
|
||||
"SKIP_DIRS",
|
||||
{
|
||||
".clj-kondo",
|
||||
".git",
|
||||
".hg",
|
||||
".idea",
|
||||
".metrics",
|
||||
".svn",
|
||||
".venv",
|
||||
"__pycache__",
|
||||
"build",
|
||||
"coverage",
|
||||
"dist",
|
||||
"node_modules",
|
||||
"out",
|
||||
"target",
|
||||
"testdata",
|
||||
"venv",
|
||||
"vendor",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _test_directory_text() -> str:
|
||||
"""TEST_DIRS, named the same way in --help."""
|
||||
|
||||
return _directory_text("TEST_DIRS", {"test", "tests", "spec", "specs", "__tests__"})
|
||||
|
||||
|
||||
HELP = f"""\
|
||||
@@ -105,7 +119,11 @@ Arguments:
|
||||
path contains this text are mutated.
|
||||
|
||||
With no paths, source files under the project root are mutated. Directories
|
||||
named {_skipped_directory_text()} are skipped.
|
||||
named {_skip_directory_text()} are skipped at any depth. A test directory
|
||||
({_test_directory_text()}) is skipped when it is the first component of the
|
||||
path, and so is src/test and src/tests. A deeper directory with one of those
|
||||
names, such as src/pkg/test, is mutated. Files whose names mark them as tests
|
||||
are also skipped.
|
||||
|
||||
The default, once a snapshot exists, reruns survivors and sites in functions
|
||||
whose text changed. Killed mutants in unchanged functions are kept.
|
||||
@@ -359,37 +377,19 @@ def _positionals(root: Path, args: list[str]) -> tuple[list[Path], list[str]]:
|
||||
|
||||
|
||||
def _changed_sources(root: Path, crapper) -> list[Path]:
|
||||
skip = set(crapper.discover.SKIP_DIRS) | set(crapper.discover.TEST_DIRS)
|
||||
root_resolved = root.resolve()
|
||||
files = []
|
||||
for path in _changed_files(root):
|
||||
relative = path.relative_to(root_resolved)
|
||||
if not skip.isdisjoint(relative.parts):
|
||||
continue
|
||||
if crapper.discover.language_of(path) is None:
|
||||
continue
|
||||
if crapper.discover.is_test_file(path):
|
||||
continue
|
||||
files.append(path)
|
||||
return files
|
||||
return [path for path in _changed_files(root) if is_mutable_source(path, root, crapper)]
|
||||
|
||||
|
||||
def _is_source(path: Path, crapper) -> bool:
|
||||
if crapper.discover.language_of(path) is None:
|
||||
return False
|
||||
return not crapper.discover.is_test_file(path)
|
||||
|
||||
|
||||
def _explicit_sources(existing: list[Path], crapper) -> list[Path]:
|
||||
def _explicit_sources(existing: list[Path], root: Path, crapper) -> list[Path]:
|
||||
files = []
|
||||
for path in existing:
|
||||
if path.is_dir():
|
||||
files.extend(crapper.discover.iter_source_files([path]))
|
||||
files.extend(iter_source_files([path], root, crapper))
|
||||
continue
|
||||
if _is_source(path, crapper):
|
||||
if is_mutable_source(path, root, crapper):
|
||||
files.append(path)
|
||||
continue
|
||||
if crapper.discover.is_test_file(path):
|
||||
if is_skipped_test(path, root, crapper):
|
||||
print(f"Skipping test file {path}", file=sys.stderr)
|
||||
return files
|
||||
|
||||
@@ -398,10 +398,11 @@ def _choose_files(options: Options, root: Path, existing: list[Path], crapper) -
|
||||
if options.changed:
|
||||
return _changed_sources(root, crapper)
|
||||
if options.source_roots:
|
||||
return crapper.discover.iter_source_files([(root / path).resolve() for path in options.source_roots])
|
||||
starts = [(root / path).resolve() for path in options.source_roots]
|
||||
return iter_source_files(starts, root, crapper)
|
||||
if existing:
|
||||
return _explicit_sources(existing, crapper)
|
||||
return crapper.discover.iter_source_files([root])
|
||||
return _explicit_sources(existing, root, crapper)
|
||||
return iter_source_files([root], root, crapper)
|
||||
|
||||
|
||||
def _matching(files: list[Path], filters: list[str]) -> list[Path]:
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
"""Choose the source files a mutation run will consider.
|
||||
|
||||
crapper.discover treats any TEST_DIRS name at any depth as a test path, which
|
||||
skips production fixtures such as src/agnesse/slim/test/. Mutator therefore
|
||||
walks the tree itself.
|
||||
|
||||
A path relative to the project root is a test path when its first component is
|
||||
a TEST_DIRS name (test, tests, spec, specs, or __tests__), or when it sits
|
||||
under src/test or src/tests, the Maven and Gradle test layout. SKIP_DIRS are
|
||||
skipped at any depth. Filename-based test files are still detected by
|
||||
crapper.discover.is_test_file, applied to the filename so the any-depth
|
||||
directory check is not reused.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
SRC_TEST_DIRS = frozenset({"test", "tests"})
|
||||
|
||||
|
||||
def is_test_path(relative: Path, test_dirs: set[str]) -> bool:
|
||||
parts = relative.parts
|
||||
if not parts:
|
||||
return False
|
||||
if parts[0] in test_dirs:
|
||||
return True
|
||||
return len(parts) >= 2 and parts[0] == "src" and parts[1] in SRC_TEST_DIRS
|
||||
|
||||
|
||||
def _relative_to(path: Path, root: Path) -> Path | None:
|
||||
try:
|
||||
return path.resolve().relative_to(root.resolve())
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def is_mutable_source(path: Path, root: Path, crapper) -> bool:
|
||||
if crapper.discover.language_of(path) is None:
|
||||
return False
|
||||
relative = _relative_to(path, root)
|
||||
if relative is None:
|
||||
return False
|
||||
if not set(crapper.discover.SKIP_DIRS).isdisjoint(relative.parts):
|
||||
return False
|
||||
if is_test_path(relative, set(crapper.discover.TEST_DIRS)):
|
||||
return False
|
||||
return not crapper.discover.is_test_file(path.name)
|
||||
|
||||
|
||||
def is_skipped_test(path: Path, root: Path, crapper) -> bool:
|
||||
if crapper.discover.language_of(path) is None:
|
||||
return False
|
||||
relative = _relative_to(path, root)
|
||||
if relative is not None and is_test_path(relative, set(crapper.discover.TEST_DIRS)):
|
||||
return True
|
||||
return crapper.discover.is_test_file(path.name)
|
||||
|
||||
|
||||
def _enter(current: Path, name: str, root: Path, skip_dirs: set[str], test_dirs: set[str]) -> bool:
|
||||
if name in skip_dirs:
|
||||
return False
|
||||
relative = _relative_to(current / name, root)
|
||||
return relative is None or not is_test_path(relative, test_dirs)
|
||||
|
||||
|
||||
def iter_source_files(roots: list[Path], project_root: Path, crapper) -> list[Path]:
|
||||
skip_dirs = set(crapper.discover.SKIP_DIRS)
|
||||
test_dirs = set(crapper.discover.TEST_DIRS)
|
||||
project_root = project_root.resolve()
|
||||
files: set[Path] = set()
|
||||
for start in roots:
|
||||
start = start.resolve()
|
||||
if start.is_file():
|
||||
if is_mutable_source(start, project_root, crapper):
|
||||
files.add(start)
|
||||
continue
|
||||
if not start.exists():
|
||||
continue
|
||||
for dirpath, dirnames, filenames in os.walk(start):
|
||||
current = Path(dirpath)
|
||||
dirnames[:] = [
|
||||
name for name in dirnames if _enter(current, name, project_root, skip_dirs, test_dirs)
|
||||
]
|
||||
for name in filenames:
|
||||
path = (current / name).resolve()
|
||||
if is_mutable_source(path, project_root, crapper):
|
||||
files.add(path)
|
||||
return sorted(files, key=lambda path: path.as_posix())
|
||||
Reference in New Issue
Block a user