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
+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