diff --git a/tests/test_coverage.py b/tests/test_coverage.py index c3360d1..7a59eb1 100644 --- a/tests/test_coverage.py +++ b/tests/test_coverage.py @@ -165,6 +165,8 @@ def test_load_bundle_merges_each_report(tmp_path): assert bundle.percent_for(function(start_line=3, end_line=4)) == 50.0 assert bundle.go_profile is not None assert bundle.jacoco is not None + assert "src/demo/core.clj" in bundle.form_html + assert "index" not in bundle.form_html def _go_module(module_dir, module_line, profile_text): diff --git a/tests/test_edges.py b/tests/test_edges.py index 8ecb396..578d036 100644 --- a/tests/test_edges.py +++ b/tests/test_edges.py @@ -54,6 +54,8 @@ def test_suffix_match_rejects_a_longer_suffix(): assert suffix_match("src/demo/core.clj", "demo/core.clj") is True assert suffix_match("core.clj", "src/demo/core.clj") is False assert suffix_match("src/demo/core.clj", "") is False + assert suffix_match("", "") is False + assert suffix_match("src/a.go", "src/a.go") is True def test_coverage_percent_is_zero_when_there_is_nothing_to_count(): @@ -74,13 +76,17 @@ def test_lcov_keeps_a_file_that_has_no_end_record(): def test_go_profile_rejects_a_broken_line(): - with pytest.raises(ValueError, match="invalid coverage segment"): + with pytest.raises(ValueError, match="invalid coverage segment on line 2"): parse_go_profile("mode: set\nnot a segment\n") + with pytest.raises(ValueError, match="invalid coverage segment on line 1"): + parse_go_profile("not a segment\n") def test_go_percent_is_zero_when_no_segment_overlaps_the_function(): profile = parse_go_profile("mode: set\nboard.go:1.1,2.2 1 1\n") assert go_percent(profile, "board.go", 10, 12) == 0.0 + touching = parse_go_profile("mode: set\nboard.go:3.1,4.2 2 2\n") + assert go_percent(touching, "board.go", 1, 3) == 100.0 def test_go_percent_does_not_accept_the_first_unrelated_file(): @@ -158,6 +164,34 @@ def test_a_shorter_file_does_not_take_a_longer_files_coverage(): assert bundle.percent_for(long) == 100.0 +def test_an_equal_report_key_blocks_a_longer_file(): + bundle = CoverageBundle( + go_profile=parse_go_profile("mode: set\n/data/board.go:1.1,2.2 1 1\n") + ) + bundle.bind_sources(["/proj/data/board.go", "/data/board.go"]) + covered = function(path="/proj/data/board.go", start_line=1, end_line=2) + assert bundle.percent_for(covered) is None + + +def test_the_smaller_added_prefix_is_not_the_shorter_extra_prefix(): + profile = parse_go_profile( + "mode: set\n" + "X/proj/src/demo/file.go:1.1,2.2 1 1\n" + "Y/Z/proj/demo/file.go:1.1,2.2 1 0\n" + ) + assert go_percent(profile, "/proj/src/demo/file.go", 1, 2) == 100.0 + + +def test_a_candidate_path_is_not_blank(): + from crapper.coverage import _candidates, _key_has_suffix + + assert "" not in _candidates("") + assert _key_has_suffix(["src", "a.go"], ["src", "a.go"]) is True + bundle = CoverageBundle() + bundle.bind_sources(["a.go", "a.go"]) + assert bundle.source_parts.count(("a.go",)) == 1 + + def test_a_short_report_key_matches_one_longer_source(): bundle = CoverageBundle( go_profile=parse_go_profile("mode: set\nboard.go:4.1,6.2 2 1\n") diff --git a/tests/test_runners.py b/tests/test_runners.py index 3e3e0af..806c52b 100644 --- a/tests/test_runners.py +++ b/tests/test_runners.py @@ -17,8 +17,10 @@ from crapper.runners import ( _python_kind, _record_python_lcov, _restore_manifests, + _cover_python, _rust_kind, _vitest_version, + write_command_output, python_coverage_commands, python_roots, python_sources, @@ -567,3 +569,125 @@ def test_vitest_install_restores_manifests_when_install_changes_them(tmp_path, m def test_python_sources_name_a_top_level_module_without_its_suffix(tmp_path): module = tmp_path / "carddav.py" assert python_sources(tmp_path, [module, tmp_path / "test_carddav.py"]) == "carddav" + + +def test_an_empty_language_directory_is_kept(tmp_path): + kept = tmp_path / "target" / "coverage" / "go" + kept.mkdir(parents=True) + _clean_clojure(tmp_path) + assert kept.is_dir() + + +def test_command_output_can_be_written_twice_under_nested_parents(tmp_path, monkeypatch): + def fake_run(command, cwd, stdout): + stdout.write("ok") + + class Completed: + returncode = 0 + + return Completed() + + monkeypatch.setattr("crapper.runners.subprocess.run", fake_run) + destination = tmp_path / "a" / "b" / "out.json" + assert write_command_output(["llvm-cov"], tmp_path, destination) == 0 + assert write_command_output(["llvm-cov"], tmp_path, destination) == 0 + assert destination.read_text(encoding="utf-8") == "ok" + + +def test_vitest_coverage_is_ready_only_after_a_real_install(tmp_path, monkeypatch): + provider = tmp_path / "node_modules" / "@vitest" / "coverage-v8" + + def install(command, cwd): + provider.mkdir(parents=True) + return 0 + + monkeypatch.setattr("crapper.runners.run_shell", install) + assert _ensure_vitest_coverage(tmp_path) is True + + other = tmp_path / "other" + other.mkdir() + monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 0) + assert _ensure_vitest_coverage(other) is False + + +def test_rustup_failure_is_reported_and_success_is_quiet(monkeypatch, capsys): + monkeypatch.setattr( + "crapper.runners.shutil.which", + lambda name: "/bin/rustup" if name == "rustup" else None, + ) + monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 0) + assert _rust_kind() is None + assert "llvm-tools-preview failed" not in capsys.readouterr().err + + monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 1) + assert _rust_kind() is None + assert "llvm-tools-preview failed" in capsys.readouterr().err + + +def test_a_failed_install_does_not_count_as_llvm_cov(monkeypatch): + installed = {"done": False} + + def which(name): + if name == "cargo-llvm-cov" and installed["done"]: + return "/bin/cargo-llvm-cov" + return None + + def shell(command, cwd): + installed["done"] = True + return 1 + + monkeypatch.setattr("crapper.runners.shutil.which", which) + monkeypatch.setattr("crapper.runners.run_shell", shell) + assert _rust_kind() is None + + +def test_java_and_go_success_are_quiet(tmp_path, monkeypatch, capsys): + java = _write(tmp_path, "src/Board.java", "class Board {}\n") + _write(tmp_path, "pom.xml", "\n") + monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 0) + run_coverage(tmp_path, [java], None) + assert "exited" not in capsys.readouterr().err + monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 1) + run_coverage(tmp_path, [java], None) + assert "Java coverage exited 1" in capsys.readouterr().err + + go_root = tmp_path / "go" + go_source = _write(go_root, "board.go", "package demo\nfunc Place() int { return 1 }\n") + _write(go_root, "go.mod", "module example.com/demo\n") + monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 0) + run_coverage(go_root, [go_source], None) + assert "Go coverage exited" not in capsys.readouterr().err + monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 1) + run_coverage(go_root, [go_source], None) + assert "Go coverage exited 1" in capsys.readouterr().err + + +def test_python_and_rust_reports_are_created_again(tmp_path, monkeypatch, capsys): + python = _write(tmp_path, "src/app.py", "def run():\n return 1\n") + _write(tmp_path, "pyproject.toml", "") + monkeypatch.setattr("crapper.runners._ensure_python_module", lambda *args: True) + monkeypatch.setattr("crapper.runners.run_shell", lambda *args: 0) + monkeypatch.setattr("crapper.runners._record_python_lcov", lambda *args: None) + _cover_python(tmp_path, [python]) + _cover_python(tmp_path, [python]) + assert (tmp_path / "target" / "coverage" / "python").is_dir() + + rust_root = tmp_path / "rust" + rust = _write(rust_root, "src/lib.rs", "pub fn open() {}\n") + _write(rust_root, "Cargo.toml", '[package]\nname = "demo"\nversion = "0.1.0"\n') + monkeypatch.setattr( + "crapper.runners.shutil.which", + lambda name: "/bin/cargo-llvm-cov" if name == "cargo-llvm-cov" else None, + ) + run_coverage(rust_root, [rust], None) + run_coverage(rust_root, [rust], None) + assert (rust_root / "target" / "coverage" / "rust").is_dir() + assert "exited" not in capsys.readouterr().err + monkeypatch.setattr("crapper.runners.run_shell", lambda *args: 1) + run_coverage(rust_root, [rust], None) + assert "Rust coverage exited 1" in capsys.readouterr().err + + +def test_a_successful_custom_coverage_command_is_quiet(tmp_path, capsys): + assert run_coverage(tmp_path, [], "true") == 0 + assert "exited" not in capsys.readouterr().err diff --git a/tests/test_swift.py b/tests/test_swift.py index 0c96e53..445dde8 100644 --- a/tests/test_swift.py +++ b/tests/test_swift.py @@ -376,6 +376,23 @@ def test_the_innermost_region_decides_whether_a_line_is_covered(): def test_a_segment_region_is_a_single_line(): + text = json.dumps( + { + "data": [ + { + "files": [ + { + "filename": "Short.swift", + "segments": ["nope", [1, 2], [2, 5, 1, True]], + } + ], + "functions": [], + } + ] + } + ) + short = parse_swift_coverage(text)["Short.swift"] + assert short.regions == [(2, 2, 1)] text = (_FIXTURES / "segments.json").read_text(encoding="utf-8") record = parse_swift_coverage(text)["Sources/Demo/Box.swift"] assert (2, 2, 1) in record.regions @@ -1002,6 +1019,25 @@ def test_a_failed_xcode_run_forgets_the_previous_report(tmp_path, monkeypatch): assert ':name "choose", :namespace "Demo", :complexity 2, :coverage 0.0, :crap 6.0' in _snapshot(tmp_path) +def test_swift_export_needs_the_profile_the_binary_and_llvm_cov(tmp_path, monkeypatch, capsys): + _plant(tmp_path, "Package.swift", "") + source = _plant(tmp_path, "Sources/Demo/Box.swift", "func open() {}\n") + _plant_profdata(tmp_path) + exports = [] + monkeypatch.setattr( + "crapper.runners.shutil.which", + lambda name: f"/usr/bin/{name}" if name in {"swift", "llvm-cov"} else None, + ) + monkeypatch.setattr("crapper.runners.run_shell", lambda command, cwd: 0) + monkeypatch.setattr( + "crapper.runners.write_command_output", + lambda command, cwd, destination: exports.append(command) or 0, + ) + assert run_coverage(tmp_path, [source], None) == 0 + assert exports == [] + assert "Swift coverage data is missing" in capsys.readouterr().err + + def test_swift_without_a_toolchain_does_not_run_a_command(tmp_path, monkeypatch, capsys): _plant(tmp_path, "Package.swift", "") source = _plant(tmp_path, "Sources/Demo/Box.swift", "func open() {}\n")