|
| 1 | +"""Tests for handling Vitest coverage exclusions. |
| 2 | +
|
| 3 | +These tests verify that Codeflash correctly detects and handles files |
| 4 | +that are excluded from coverage by vitest.config.ts, preventing false |
| 5 | +0% coverage reports. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import json |
| 11 | +import tempfile |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from codeflash.models.models import CodeOptimizationContext, CoverageStatus |
| 17 | +from codeflash.verification.coverage_utils import JestCoverageUtils |
| 18 | + |
| 19 | + |
| 20 | +class TestVitestCoverageExclusions: |
| 21 | + """Tests for Vitest coverage exclusion handling.""" |
| 22 | + |
| 23 | + def test_missing_coverage_returns_not_found_status(self) -> None: |
| 24 | + """Should return NOT_FOUND status when file is not in coverage data. |
| 25 | +
|
| 26 | + When a file is excluded from Vitest coverage (via coverage.exclude), |
| 27 | + it won't appear in coverage-final.json. Codeflash should return |
| 28 | + NOT_FOUND status (not PARSED_SUCCESSFULLY). |
| 29 | +
|
| 30 | + This test verifies the current behavior is correct at the coverage |
| 31 | + parsing level. The issue is at a higher level (function_optimizer.py) |
| 32 | + where NOT_FOUND status needs better handling. |
| 33 | + """ |
| 34 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 35 | + tmp_path = Path(tmp_dir) |
| 36 | + |
| 37 | + # Create mock coverage-final.json that's missing the target file |
| 38 | + coverage_file = tmp_path / "coverage-final.json" |
| 39 | + coverage_data = { |
| 40 | + "/workspace/project/src/utils/helpers.ts": { |
| 41 | + "fnMap": {}, |
| 42 | + "s": {}, |
| 43 | + }, |
| 44 | + # src/agents/sandbox/fs-paths.ts is NOT here (excluded by Vitest) |
| 45 | + } |
| 46 | + with coverage_file.open("w") as f: |
| 47 | + json.dump(coverage_data, f) |
| 48 | + |
| 49 | + # Try to load coverage for a missing file |
| 50 | + missing_file = Path("/workspace/project/src/agents/sandbox/fs-paths.ts") |
| 51 | + from codeflash.models.models import CodeStringsMarkdown |
| 52 | + |
| 53 | + mock_context = CodeOptimizationContext( |
| 54 | + testgen_context=CodeStringsMarkdown(language="typescript"), |
| 55 | + read_writable_code=CodeStringsMarkdown(language="typescript"), |
| 56 | + helper_functions=[], |
| 57 | + preexisting_objects=set(), |
| 58 | + ) |
| 59 | + |
| 60 | + result = JestCoverageUtils.load_from_jest_json( |
| 61 | + coverage_json_path=coverage_file, |
| 62 | + function_name="parseSandboxBindMount", |
| 63 | + code_context=mock_context, |
| 64 | + source_code_path=missing_file, |
| 65 | + ) |
| 66 | + |
| 67 | + # Should return NOT_FOUND when file not in coverage |
| 68 | + assert result.status == CoverageStatus.NOT_FOUND, ( |
| 69 | + f"Expected NOT_FOUND for missing file, got {result.status}" |
| 70 | + ) |
| 71 | + assert result.coverage == 0.0 |
| 72 | + |
| 73 | + def test_handles_included_file_normally(self) -> None: |
| 74 | + """Should handle files that ARE included in coverage normally. |
| 75 | +
|
| 76 | + This test verifies that the fix doesn't break normal coverage parsing |
| 77 | + for files that are NOT excluded. |
| 78 | + """ |
| 79 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 80 | + tmp_path = Path(tmp_dir) |
| 81 | + |
| 82 | + # Create mock coverage-final.json with a valid file |
| 83 | + coverage_file = tmp_path / "coverage-final.json" |
| 84 | + test_file = "/workspace/project/src/utils/helpers.ts" |
| 85 | + coverage_data = { |
| 86 | + test_file: { |
| 87 | + "fnMap": { |
| 88 | + "0": {"name": "someHelper", "loc": {"start": {"line": 1}, "end": {"line": 5}}} |
| 89 | + }, |
| 90 | + "statementMap": { |
| 91 | + "0": {"start": {"line": 2}, "end": {"line": 2}}, |
| 92 | + "1": {"start": {"line": 3}, "end": {"line": 3}}, |
| 93 | + }, |
| 94 | + "s": {"0": 5, "1": 5}, # Both statements executed |
| 95 | + "branchMap": {}, |
| 96 | + "b": {}, |
| 97 | + } |
| 98 | + } |
| 99 | + with coverage_file.open("w") as f: |
| 100 | + json.dump(coverage_data, f) |
| 101 | + |
| 102 | + source_file = Path(test_file) |
| 103 | + from codeflash.models.models import CodeStringsMarkdown |
| 104 | + |
| 105 | + mock_context = CodeOptimizationContext( |
| 106 | + testgen_context=CodeStringsMarkdown(language="typescript"), |
| 107 | + read_writable_code=CodeStringsMarkdown(language="typescript"), |
| 108 | + helper_functions=[], |
| 109 | + preexisting_objects=set(), |
| 110 | + ) |
| 111 | + |
| 112 | + result = JestCoverageUtils.load_from_jest_json( |
| 113 | + coverage_json_path=coverage_file, |
| 114 | + function_name="someHelper", |
| 115 | + code_context=mock_context, |
| 116 | + source_code_path=source_file, |
| 117 | + ) |
| 118 | + |
| 119 | + # Should parse successfully for non-excluded files |
| 120 | + assert result.status == CoverageStatus.PARSED_SUCCESSFULLY |
| 121 | + assert result.coverage > 0.0 # Should have actual coverage |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + pytest.main([__file__, "-v"]) |
0 commit comments