From 5631fe2ce230bb30d304164f8ad9381d225856f8 Mon Sep 17 00:00:00 2001 From: Evan Kepner Date: Tue, 7 Jan 2020 18:13:54 -0500 Subject: [PATCH] add resolve path flag to coverage filter --- mutatest/filters.py | 12 ++++++++++-- mutatest/tests/conftest.py | 11 ++++++----- mutatest/tests/test_filters.py | 7 ++++++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/mutatest/filters.py b/mutatest/filters.py index e7c5add..6cd3f15 100644 --- a/mutatest/filters.py +++ b/mutatest/filters.py @@ -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. @@ -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: diff --git a/mutatest/tests/conftest.py b/mutatest/tests/conftest.py index 60d0e22..bb96280 100644 --- a/mutatest/tests/conftest.py +++ b/mutatest/tests/conftest.py @@ -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" @@ -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"), diff --git a/mutatest/tests/test_filters.py b/mutatest/tests/test_filters.py index 970828e..be20da7 100644 --- a/mutatest/tests/test_filters.py +++ b/mutatest/tests/test_filters.py @@ -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) @@ -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