From 8e84e9cef7833e0506b2a4b31cc6b3df4816a973 Mon Sep 17 00:00:00 2001 From: Eljees <3.14hell@gmail.com> Date: Sun, 2 Aug 2026 15:13:37 +0300 Subject: [PATCH] Fix diff error without source files --- CHANGES.md | 2 ++ README.md | 11 ++++++++--- pycobertura/cobertura.py | 10 ++++++++-- tests/test_cobertura_diff.py | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index bbe08cae..bbae2146 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +* Raise `Cobertura.MissingFileSystem` instead of `AttributeError` when diffing + reports without source files. * Add `--sort-by-uncovered-lines` to sort output, so the file with the most missing lines is at the top. Thanks @guettli * Fix: JSON and YAML output when using `--ignore-regex` in `show` command. Thanks @OidaTiftla diff --git a/README.md b/README.md index 70ed5c5c..b85edefc 100644 --- a/README.md +++ b/README.md @@ -403,9 +403,14 @@ pycobertura/utils.py 12 0 100.00% TOTAL 253 0 100.00%""" from pycobertura import TextReporterDelta - -coverage1 = Cobertura('coverage1.xml') -coverage2 = Cobertura('coverage2.xml') +from pycobertura.filesystem import filesystem_factory + +coverage1 = Cobertura( + 'coverage1.xml', filesystem=filesystem_factory('source1') +) +coverage2 = Cobertura( + 'coverage2.xml', filesystem=filesystem_factory('source2') +) delta = TextReporterDelta(coverage1, coverage2) delta.generate() == """\ Filename Stmts Miss Cover Missing diff --git a/pycobertura/cobertura.py b/pycobertura/cobertura.py index d328fce1..ee162d21 100644 --- a/pycobertura/cobertura.py +++ b/pycobertura/cobertura.py @@ -212,6 +212,12 @@ def _raise_MissingFileSystem(self, filename): f"content of the file." ) + def _filesystem_has_file(self, filename): + if self.filesystem is None: + self._raise_MissingFileSystem(filename) + + return self.filesystem.has_file(filename) + @memoize def file_source(self, filename): """ @@ -437,7 +443,7 @@ def file_source(self, filename: str): """ nonexistent = True - if self.cobertura1.has_file(filename) and self.cobertura1.filesystem.has_file( + if self.cobertura1.has_file(filename) and self.cobertura1._filesystem_has_file( filename ): lines1 = self.cobertura1.source_lines(filename) @@ -447,7 +453,7 @@ def file_source(self, filename: str): lines1 = [] line_statuses1: Dict[int, LineStatus] = {} - if self.cobertura2.has_file(filename) and self.cobertura2.filesystem.has_file( + if self.cobertura2.has_file(filename) and self.cobertura2._filesystem_has_file( filename ): lines2 = self.cobertura2.source_lines(filename) diff --git a/tests/test_cobertura_diff.py b/tests/test_cobertura_diff.py index 45311132..86146777 100644 --- a/tests/test_cobertura_diff.py +++ b/tests/test_cobertura_diff.py @@ -1,6 +1,20 @@ +import pytest + from .utils import make_cobertura +@pytest.mark.parametrize("filename", ["dummy/dummy.py", "dummy/dummy3.py"]) +def test_diff_file_source_without_filesystem(filename): + from pycobertura.cobertura import Cobertura, CoberturaDiff + + cobertura1 = Cobertura('tests/dummy.source1/coverage.xml') + cobertura2 = Cobertura('tests/dummy.source2/coverage.xml') + differ = CoberturaDiff(cobertura1, cobertura2) + + with pytest.raises(Cobertura.MissingFileSystem): + differ.file_source(filename) + + def test_diff_class_source(): from pycobertura.cobertura import CoberturaDiff from pycobertura.cobertura import Line