diff --git a/src/mutator/sources.py b/src/mutator/sources.py index a11edd7..217cf8d 100644 --- a/src/mutator/sources.py +++ b/src/mutator/sources.py @@ -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) diff --git a/tests/test_sources.py b/tests/test_sources.py index 9da1818..8a71763 100644 --- a/tests/test_sources.py +++ b/tests/test_sources.py @@ -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, } diff --git a/tests/test_swift.py b/tests/test_swift.py index 8020b23..42f7a11 100644 --- a/tests/test_swift.py +++ b/tests/test_swift.py @@ -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