Skip to content

Commit

Permalink
add resolve path flag to coverage filter
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanKepner committed Jan 7, 2020
1 parent 43d1477 commit 5631fe2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
12 changes: 10 additions & 2 deletions mutatest/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ def coverage_data(self) -> CoverageData:
return self._coverage_data

def filter( # type: ignore
self, loc_idxs: Set[LocIndex], source_file: Union[str, Path], invert: bool = False
self,
loc_idxs: Set[LocIndex],
source_file: Union[str, Path],
invert: bool = False,
resolve_source: bool = True,
) -> Set[LocIndex]:
"""Filter based on coverage measured file.
Expand All @@ -138,11 +142,15 @@ class instance, and any valid measured file can be used in the filter.
loc_idxs: location index set of targets
source_file: source file that is measured by the coverage file
invert: flag for inverted filter using NOT
resolve_source: flag for using resolved source_file vs. direct str, default True.
This exists mostly for testing purposes to access mocked entries in the
fake coverage files.
Returns:
Filtered set of location index set
"""
measured_file = str(Path(source_file).resolve())
measured_file = str(Path(source_file).resolve()) if resolve_source else str(source_file)

covered_lines = self.coverage_data.lines(measured_file) or list()

if invert:
Expand Down
11 changes: 6 additions & 5 deletions mutatest/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ def mock_coverage_file(tmp_path_factory):

folder = tmp_path_factory.mktemp("cov")

# aligned to fixture mock_source_and_targets
# aligned to fixture mock_source_and_targets for file3.py used in positive filter.
mock_contents = {
"/simple_isnot/isnot/__init__.py": [1],
"/simple_isnot/isnot/test_isnot.py": [1, 3, 4],
"/simple_isnot/isnot/run.py": [1, 4, 2],
"file1.py": [1],
"file2.py": [1, 3, 4],
"file3.py": [1, 2, 4],
}

mock_cov_file = folder / ".coverage"
Expand All @@ -147,7 +147,8 @@ def mock_source_and_targets():
Covered lines include: 1, 2, 4
"""
source_file = Path("/simple_isnot/isnot/run.py")
# see mock_coverage_file fixture
source_file = Path("file3.py")
targets = {
LocIndex(ast_class="AugAssign", lineno=1, col_offset=1, op_type="o"),
LocIndex(ast_class="AugAssign", lineno=2, col_offset=1, op_type="o"),
Expand Down
7 changes: 6 additions & 1 deletion mutatest/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@pytest.fixture(scope="module")
def mock_CoverageFilter(mock_coverage_file):
"""Mock CoverageFilter on the mock_coverage_file defined in conftest."""
print(mock_coverage_file)
return CoverageFilter(coverage_file=mock_coverage_file)


Expand All @@ -42,8 +43,12 @@ def test_unset_coverage_file(fn):
)
def test_filter(mock_CoverageFilter, mock_source_and_targets, invert, expected):
"""Coverage filter inverted and not inverted set filtering."""
# use resolve_source=False for the mocks of relative file names.
results = mock_CoverageFilter.filter(
mock_source_and_targets.targets, mock_source_and_targets.source_file, invert=invert
mock_source_and_targets.targets,
mock_source_and_targets.source_file,
invert=invert,
resolve_source=False,
)
result_ln = sorted([r.lineno for r in results])
assert result_ln == expected
Expand Down

0 comments on commit 5631fe2

Please sign in to comment.