Skip to content

Commit ff19187

Browse files
Handle case of deleted files (#533)
* Handle case of deleted file * Updating tests: - Adding test with pycodestyle driver - Adding test with pylint driver
1 parent f386052 commit ff19187

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

diff_cover/violationsreporters/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ def violations(self, src_path):
157157
self.violations_dict = self.driver.parse_reports(self.reports)
158158
return self.violations_dict[src_path]
159159

160+
if not os.path.exists(src_path):
161+
self.violations_dict[src_path] = []
162+
return self.violations_dict[src_path]
163+
160164
if self.driver_tool_installed is None:
161165
self.driver_tool_installed = self.driver.installed()
162166
if not self.driver_tool_installed:
@@ -166,8 +170,7 @@ def violations(self, src_path):
166170
if self.options:
167171
for arg in self.options.split():
168172
command.append(arg)
169-
if os.path.exists(src_path):
170-
command.append(src_path.encode(sys.getfilesystemencoding()))
173+
command.append(src_path.encode(sys.getfilesystemencoding()))
171174

172175
stdout, stderr = execute(command, self.driver.exit_codes)
173176
output = stderr if self.driver.output_stderr else stdout

tests/test_violations_reporter.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,12 +1586,15 @@ def test_quality(self, process_patcher):
15861586

15871587

15881588
class TestPylintQualityReporterTest:
1589+
@pytest.mark.disable_all_files_exist
15891590
def test_no_such_file(self):
1591+
file_paths = ["ajshdjlasdhajksdh.py"]
15901592
quality = QualityReporter(PylintDriver())
15911593

15921594
# Expect that we get no results
1593-
result = quality.violations("")
1594-
assert result == []
1595+
for path in file_paths:
1596+
result = quality.violations(path)
1597+
assert result == []
15951598

15961599
def test_no_python_file(self):
15971600
quality = QualityReporter(PylintDriver())
@@ -2181,6 +2184,15 @@ def test_quality_reporter(self, mocker):
21812184

21822185
assert mock_stderr.getvalue() == "pycodestyle path/to/file.py"
21832186

2187+
@pytest.mark.disable_all_files_exist
2188+
def test_file_does_not_exist(self):
2189+
quality = QualityReporter(pycodestyle_driver)
2190+
file_paths = ["ajshdjlasdhajksdh.py"]
2191+
# Expect that we get no results because that file does not exist
2192+
for path in file_paths:
2193+
result = quality.violations(path)
2194+
assert result == []
2195+
21842196

21852197
class TestCppcheckQualityDriverTest:
21862198
"""Tests for cppcheck quality driver."""

0 commit comments

Comments
 (0)