Skip Swift test targets when choosing source files.

crapper treats Tests, AppTests, and *Tests.swift as tests, but mutator
only asked is_test_file about the filename. That kept a file such as
Tests/DemoTests/Board.swift, so a run would mutate the test suite.
.build, .swiftpm, and DerivedData were already SKIP_DIRS.

A Swift path is now a test path when a directory component ends with
Tests, or the file name ends with Tests.swift. A deeper directory named
test, the Java fixture layout, is still mutated.

Proved by the source walk and by selecting Sources/Demo/Box.swift next
to Tests/DemoTests/Board.swift and BoxTests.swift. The walk failed first
because those three Tests paths were still chosen. Both tests pass.

Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-25 23:58:57 +00:00
co-authored by Robert C. Martin
parent b52159454d
commit 23b7d47936
3 changed files with 51 additions and 1 deletions
+22
View File
@@ -10,6 +10,11 @@ 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.
Swift adds its own names. A directory component that ends with ``Tests``
(``Tests``, ``AppTests``) is a test target at any depth, and so is a file
named ``*Tests.swift``. ``.build``, ``.swiftpm``, and ``DerivedData`` are
crapper SKIP_DIRS.
"""
from __future__ import annotations
@@ -29,6 +34,19 @@ def is_test_path(relative: Path, test_dirs: set[str]) -> bool:
return len(parts) >= 2 and parts[0] == "src" and parts[1] in SRC_TEST_DIRS
def _swift_test_relative(relative: Path) -> bool:
"""Swift test targets are ``Tests``, ``*Tests``, or ``*Tests.swift``.
``test`` at any depth stays a production path. Swift's name is ``Tests``.
"""
if relative.suffix != ".swift":
return False
if relative.name.endswith("Tests.swift"):
return True
return any(part.endswith("Tests") for part in relative.parts[:-1])
def _relative_to(path: Path, root: Path) -> Path | None:
try:
return path.resolve().relative_to(root.resolve())
@@ -46,6 +64,8 @@ def is_mutable_source(path: Path, root: Path, crapper) -> bool:
return False
if is_test_path(relative, set(crapper.discover.TEST_DIRS)):
return False
if _swift_test_relative(relative):
return False
return not crapper.discover.is_test_file(path.name)
@@ -55,6 +75,8 @@ def is_skipped_test(path: Path, root: Path, crapper) -> bool:
relative = _relative_to(path, root)
if relative is not None and is_test_path(relative, set(crapper.discover.TEST_DIRS)):
return True
if relative is not None and _swift_test_relative(relative):
return True
return crapper.discover.is_test_file(path.name)
+9
View File
@@ -48,6 +48,15 @@ CASES = {
"src/pkg/foo.test.ts": False,
"src/pkg/foo.spec.ts": False,
"src/pkg/conftest.py": False,
"Tests/DemoTests/Board.swift": False,
"AppTests/Board.swift": False,
"Sources/Demo/BoxTests.swift": False,
"Sources/Demo/Box.swift": True,
".build/debug/Generated.swift": False,
".swiftpm/cache/Generated.swift": False,
"DerivedData/App.swift": False,
"src/pkg/Tests/Hidden.swift": False,
"src/pkg/test/Kept.swift": True,
}
+20 -1
View File
@@ -3,7 +3,7 @@
import sys
from pathlib import Path
from mutator.cli import run
from mutator.cli import parse_args, run, select_files
from mutator.functions import sites_in_file
SOURCE = """func place(x: Int, ready: Bool, name: String?) -> Int {
@@ -137,6 +137,25 @@ def test_private_and_fileprivate_join_the_type_namespace(tmp_path):
assert (by_name["open"].original, by_name["open"].mutant) == ("1", "0")
def test_swift_test_files_are_skipped_and_production_sources_are_kept(tmp_path, capsys):
source = tmp_path / "Sources" / "Demo" / "Box.swift"
source.parent.mkdir(parents=True)
source.write_text("func open() -> Int { 1 }\n", encoding="utf-8")
test_file = tmp_path / "Tests" / "DemoTests" / "Box.swift"
test_file.parent.mkdir(parents=True)
test_file.write_text("func check() {}\n", encoding="utf-8")
named = tmp_path / "Sources" / "Demo" / "BoxTests.swift"
named.write_text("func check() {}\n", encoding="utf-8")
chosen = select_files(
parse_args(["--root", str(tmp_path), str(source), str(test_file), str(named)])
)
captured = capsys.readouterr()
assert chosen == [source.resolve()]
assert "Skipping test file" in captured.err
assert "Tests/DemoTests/Box.swift" in captured.err
assert "BoxTests.swift" in captured.err
def test_a_swift_run_records_killed_and_survived_on_the_crapper_namespace(tmp_path, capsys):
relative = "Sources/Demo/Place.swift"
path = tmp_path / relative