From b9184399023f02c4820350f783a0dc78225d8418 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 26 Nov 2026 00:09:27 +0000 Subject: [PATCH] Kill equivalent Swift mutants and prove a SwiftPM run. Stopping a guard at else was the same as stopping at the following brace, and the binding flag started false only to be cleared by the keyword. Those edits survived because no test could see a difference, so the duplicate paths are gone. *Tests.swift stays crapper's filename rule; this check only adds Tests directories. A tiny package run with swift test records killed and survived on Demo.place and restores the source. The test skips when swift is not installed. The full suite, including that run on Swift 6.4.3, passed (115 tests). Co-authored-by: Robert C. Martin --- src/mutator/sites.py | 7 +++-- src/mutator/sources.py | 5 ++-- tests/test_swift.py | 60 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/mutator/sites.py b/src/mutator/sites.py index 2fe715d..73161f0 100644 --- a/src/mutator/sites.py +++ b/src/mutator/sites.py @@ -65,7 +65,6 @@ _SWIFT_SKIP = { "repeat", "{", "}", - ",", "statements", "if_statement", "comment", @@ -368,8 +367,6 @@ def _visit_leaf(node, data: bytes, language: str, found: list[RawSite]) -> None: def _ends_swift_conditions(node, child) -> bool: - if child.type == "else": - return True if node.type == "repeat_while_statement": return False return child.type in {"{", "statements"} @@ -383,9 +380,11 @@ def _swift_conditions(node): for child in node.children: if _ends_swift_conditions(node, child): break - if child.type in _SWIFT_SKIP: + if child.type == ",": binding = False continue + if child.type in _SWIFT_SKIP: + continue if child.type == "value_binding_pattern": binding = True continue diff --git a/src/mutator/sources.py b/src/mutator/sources.py index 217cf8d..e066784 100644 --- a/src/mutator/sources.py +++ b/src/mutator/sources.py @@ -35,15 +35,14 @@ def is_test_path(relative: Path, test_dirs: set[str]) -> bool: def _swift_test_relative(relative: Path) -> bool: - """Swift test targets are ``Tests``, ``*Tests``, or ``*Tests.swift``. + """A Swift directory named ``Tests`` or ending in ``Tests`` is a test target. + ``*Tests.swift`` is crapper's filename rule, applied to the file name. ``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]) diff --git a/tests/test_swift.py b/tests/test_swift.py index d9b1da1..9c1e645 100644 --- a/tests/test_swift.py +++ b/tests/test_swift.py @@ -1,9 +1,12 @@ """Swift mutation sites join the functions crapper reports.""" import os +import shutil import sys from pathlib import Path +import pytest + from mutator.cli import parse_args, run, select_files from mutator.functions import sites_in_file from mutator.runner import test_plan as plan_command @@ -179,6 +182,14 @@ def test_xcode_options_require_a_value(): assert parse_args(["--xcode-scheme"]).exit_code == 1 +def test_a_swift_file_without_a_package_or_xcode_project_uses_swift_test(tmp_path): + root = tmp_path / "loose" + source = _write(root / "Place.swift", "func place() -> Int { 1 }\n") + command, directory = plan_command(root, source, "swift", None) + assert command == ["swift", "test"] + assert directory == root.resolve() + + def test_a_swift_package_runs_swift_test(tmp_path): root = tmp_path / "pkg" _write(root / "Package.swift", "// swift-tools-version:5.9\n") @@ -360,6 +371,55 @@ def test_scan_marks_a_swift_line_uncovered_when_llvm_cov_did_not_hit_it(tmp_path assert "x > 0 -> !(x > 0)" in captured.out or "> -> >=" in captured.out +def _swift_on_path(monkeypatch) -> None: + if shutil.which("swift"): + return + toolchains = Path.home() / ".local" / "share" / "swiftly" / "toolchains" + binaries = sorted(toolchains.glob("*/usr/bin/swift")) if toolchains.is_dir() else [] + if not binaries: + pytest.skip("swift is not installed") + monkeypatch.setenv("PATH", f"{binaries[-1].parent}{os.pathsep}{os.environ.get('PATH', '')}") + + +def test_swift_test_kills_and_survives_mutants_in_a_package(tmp_path, monkeypatch, capsys): + _swift_on_path(monkeypatch) + root = tmp_path / "pkg" + _write( + root / "Package.swift", + "// swift-tools-version:5.9\n" + "import PackageDescription\n" + "let package = Package(name: \"Demo\", targets: [\n" + " .target(name: \"Demo\"),\n" + " .testTarget(name: \"DemoTests\", dependencies: [\"Demo\"]),\n" + "])\n", + ) + source = _write( + root / "Sources" / "Demo" / "Place.swift", + "func place(x: Int) -> Int {\n if x > 0 {\n return x\n }\n return 0\n}\n", + ) + _write( + root / "Tests" / "DemoTests" / "PlaceTests.swift", + "import XCTest\n" + "@testable import Demo\n" + "final class PlaceTests: XCTestCase {\n" + " func testTwo() { XCTAssertEqual(place(x: 2), 2) }\n" + " func testOne() { XCTAssertEqual(place(x: 1), 1) }\n" + " func testZero() { XCTAssertEqual(place(x: 0), 0) }\n" + "}\n", + ) + code = run( + ["--no-coverage", "--mutate-all", "--max-workers", "1", "--root", str(root)] + ) + captured = capsys.readouterr() + assert code == 3 + assert "KILLED" in captured.out + assert "SURVIVED" in captured.out + text = (root / ".metrics" / "mutate" / "Demo.edn").read_text(encoding="utf-8") + assert ':namespace "Demo"' in text + assert ':id "defn/place"' in text + assert source.read_text(encoding="utf-8").startswith("func place") + + 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