mirror of
https://github.com/unclebob/mutator.git
synced 2026-11-26 18:58:50 +00:00
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 <[email protected]>
This commit is contained in:
co-authored by
Robert C. Martin
parent
883a611b68
commit
b918439902
@@ -65,7 +65,6 @@ _SWIFT_SKIP = {
|
|||||||
"repeat",
|
"repeat",
|
||||||
"{",
|
"{",
|
||||||
"}",
|
"}",
|
||||||
",",
|
|
||||||
"statements",
|
"statements",
|
||||||
"if_statement",
|
"if_statement",
|
||||||
"comment",
|
"comment",
|
||||||
@@ -368,8 +367,6 @@ def _visit_leaf(node, data: bytes, language: str, found: list[RawSite]) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def _ends_swift_conditions(node, child) -> bool:
|
def _ends_swift_conditions(node, child) -> bool:
|
||||||
if child.type == "else":
|
|
||||||
return True
|
|
||||||
if node.type == "repeat_while_statement":
|
if node.type == "repeat_while_statement":
|
||||||
return False
|
return False
|
||||||
return child.type in {"{", "statements"}
|
return child.type in {"{", "statements"}
|
||||||
@@ -383,9 +380,11 @@ def _swift_conditions(node):
|
|||||||
for child in node.children:
|
for child in node.children:
|
||||||
if _ends_swift_conditions(node, child):
|
if _ends_swift_conditions(node, child):
|
||||||
break
|
break
|
||||||
if child.type in _SWIFT_SKIP:
|
if child.type == ",":
|
||||||
binding = False
|
binding = False
|
||||||
continue
|
continue
|
||||||
|
if child.type in _SWIFT_SKIP:
|
||||||
|
continue
|
||||||
if child.type == "value_binding_pattern":
|
if child.type == "value_binding_pattern":
|
||||||
binding = True
|
binding = True
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -35,15 +35,14 @@ def is_test_path(relative: Path, test_dirs: set[str]) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def _swift_test_relative(relative: Path) -> 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``.
|
``test`` at any depth stays a production path. Swift's name is ``Tests``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if relative.suffix != ".swift":
|
if relative.suffix != ".swift":
|
||||||
return False
|
return False
|
||||||
if relative.name.endswith("Tests.swift"):
|
|
||||||
return True
|
|
||||||
return any(part.endswith("Tests") for part in relative.parts[:-1])
|
return any(part.endswith("Tests") for part in relative.parts[:-1])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
"""Swift mutation sites join the functions crapper reports."""
|
"""Swift mutation sites join the functions crapper reports."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from mutator.cli import parse_args, run, select_files
|
from mutator.cli import parse_args, run, select_files
|
||||||
from mutator.functions import sites_in_file
|
from mutator.functions import sites_in_file
|
||||||
from mutator.runner import test_plan as plan_command
|
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
|
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):
|
def test_a_swift_package_runs_swift_test(tmp_path):
|
||||||
root = tmp_path / "pkg"
|
root = tmp_path / "pkg"
|
||||||
_write(root / "Package.swift", "// swift-tools-version:5.9\n")
|
_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
|
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):
|
def test_a_swift_run_records_killed_and_survived_on_the_crapper_namespace(tmp_path, capsys):
|
||||||
relative = "Sources/Demo/Place.swift"
|
relative = "Sources/Demo/Place.swift"
|
||||||
path = tmp_path / relative
|
path = tmp_path / relative
|
||||||
|
|||||||
Reference in New Issue
Block a user