Skip to content

Commit b71b912

Browse files
committed
Add coverage for prepare_coverage_xml
1 parent 2be430e commit b71b912

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

exasol/toolbox/nox/_artifacts.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def _copy_artifacts(source: Path, dest: Path, files: Iterable[str]):
185185

186186

187187
def _prepare_coverage_xml(session: Session, source: Path) -> None:
188-
# we do not want to fail the coverage constraint for sonar, as sonar does this for us
188+
# we do not want to fail the coverage constraint for Sonar, as Sonar does this for us
189189
command = [
190190
"coverage",
191191
"xml",
@@ -195,10 +195,12 @@ def _prepare_coverage_xml(session: Session, source: Path) -> None:
195195
f"{source}/*",
196196
"--fail-under=0",
197197
]
198-
output = subprocess.run(command, capture_output=True, text=True, check=False)
198+
output = subprocess.run(
199+
command, capture_output=True, text=True, check=False
200+
) # nosec
199201

200202
if output.returncode != 0:
201-
if output.stderr.strip() == "No data to report.":
203+
if output.stdout.strip() == "No data to report.":
202204
# Assuming that previous steps passed in the CI, this indicates, as
203205
# is in the case for newly created projects that no coverage over the
204206
# `source` files was found. To allow Sonar to report, we create

test/unit/nox/_artifacts_test.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
ALL_LINT_FILES,
1919
COVERAGE_FILE,
2020
COVERAGE_TABLES,
21+
COVERAGE_XML,
2122
LINT_JSON,
2223
LINT_JSON_ATTRIBUTES,
2324
LINT_TXT,
@@ -27,9 +28,11 @@
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
5666
class EndsWith:
5767
"""
@@ -218,16 +228,9 @@ def test_missing_attributes(self, tmp_path, capsys, missing_attributes):
218228

219229

220230
class 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

Comments
 (0)