1818 ALL_LINT_FILES ,
1919 COVERAGE_FILE ,
2020 COVERAGE_TABLES ,
21+ COVERAGE_XML ,
2122 LINT_JSON ,
2223 LINT_JSON_ATTRIBUTES ,
2324 LINT_TXT ,
2728 _is_valid_lint_json ,
2829 _is_valid_lint_txt ,
2930 _is_valid_security_json ,
31+ _prepare_coverage_xml ,
3032 check_artifacts ,
3133 copy_artifacts ,
3234)
35+ from noxconfig import PROJECT_CONFIG
3336
3437
3538@contextlib .contextmanager
@@ -52,6 +55,13 @@ def mock_session(path: Path, python_version: str, *files: str):
5255 yield Mock (posargs = [str (path )])
5356
5457
58+ def _create_coverage_file (path : Path , tables : set ) -> None :
59+ connection = sqlite3 .connect (path )
60+ cursor = connection .cursor ()
61+ for table in tables :
62+ cursor .execute (f"CREATE TABLE IF NOT EXISTS { table } (test INTEGER)" )
63+
64+
5565@dataclass
5666class EndsWith :
5767 """
@@ -218,16 +228,9 @@ def test_missing_attributes(self, tmp_path, capsys, missing_attributes):
218228
219229
220230class TestIsValidCoverage :
221- @staticmethod
222- def _create_coverage_file (path : Path , tables : set ) -> None :
223- connection = sqlite3 .connect (path )
224- cursor = connection .cursor ()
225- for table in tables :
226- cursor .execute (f"CREATE TABLE IF NOT EXISTS { table } (test INTEGER)" )
227-
228231 def test_passes_when_as_expected (self , tmp_path ):
229232 path = Path (tmp_path , COVERAGE_FILE )
230- self . _create_coverage_file (path , COVERAGE_TABLES )
233+ _create_coverage_file (path , COVERAGE_TABLES )
231234
232235 result = _is_valid_coverage (path )
233236
@@ -242,7 +245,7 @@ def test_passes_when_as_expected(self, tmp_path):
242245 def test_database_missing_tables (self , tmp_path , capsys , missing_table ):
243246 tables = COVERAGE_TABLES - missing_table
244247 path = Path (tmp_path , COVERAGE_FILE )
245- self . _create_coverage_file (path , tables )
248+ _create_coverage_file (path , tables )
246249
247250 result = _is_valid_coverage (path )
248251
@@ -303,3 +306,34 @@ def test_all_files(tmp_path, capsys):
303306 )
304307 for f in [".lint.txt" , ".lint.json" , ".security.json" ]:
305308 assert (tmp_path / f ).exists ()
309+
310+
311+ class TestPrepareCoverageXml :
312+ def setup_method (self ):
313+ for path in [Path (COVERAGE_FILE ), Path (COVERAGE_XML )]:
314+ if path .exists ():
315+ path .unlink ()
316+
317+ def teardown_method (self ):
318+ for path in [Path (COVERAGE_FILE ), Path (COVERAGE_XML )]:
319+ if path .exists ():
320+ path .unlink ()
321+
322+ @staticmethod
323+ def test_no_coverage_file ():
324+ _prepare_coverage_xml (Mock (), PROJECT_CONFIG .source )
325+
326+ assert Path (COVERAGE_XML ).is_file ()
327+ assert Path (COVERAGE_XML ).read_text () == ""
328+ assert not Path (COVERAGE_FILE ).is_file ()
329+
330+ @staticmethod
331+ def test_with_bad_coverage_file_still_raises_error ():
332+ _create_coverage_file (Path (COVERAGE_FILE ), COVERAGE_TABLES )
333+ session_mock = Mock ()
334+
335+ _prepare_coverage_xml (session_mock , PROJECT_CONFIG .source )
336+
337+ assert Path (COVERAGE_FILE ).is_file ()
338+ assert not Path (COVERAGE_XML ).is_file ()
339+ assert session_mock .called_once ()
0 commit comments