Skip to content

Commit 02d7c4f

Browse files
committed
Add test for check_artifacts and switch prints to all bey stderr
1 parent 71f4070 commit 02d7c4f

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

exasol/toolbox/nox/_artifacts.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
LINT_TXT = ".lint.txt"
1919
SECURITY_JSON = ".security.json"
2020

21-
ALL_FILES = {COVERAGE_FILE, LINT_JSON, LINT_TXT, SECURITY_JSON}
21+
ALL_LINT_FILES = {COVERAGE_FILE, LINT_JSON, LINT_TXT, SECURITY_JSON}
2222
COVERAGE_TABLES = {"coverage_schema", "meta", "file", "line_bits"}
2323
LINT_JSON_ATTRIBUTES = {
2424
"type",
@@ -40,8 +40,9 @@
4040
@nox.session(name="artifacts:validate", python=False)
4141
def check_artifacts(session: Session) -> None:
4242
"""Validate that all project artifacts are available and consistent"""
43-
if not_available := _missing_files(ALL_FILES, PROJECT_CONFIG.root):
44-
print(f"not available: {not_available}", file=sys.stderr)
43+
all_files = {f.name for f in PROJECT_CONFIG.root.iterdir() if f.is_file()}
44+
if missing_files := (ALL_LINT_FILES - all_files):
45+
print(f"files not available: {missing_files}", file=sys.stderr)
4546
sys.exit(1)
4647

4748
all_is_valid_checks = [
@@ -50,18 +51,12 @@ def check_artifacts(session: Session) -> None:
5051
_is_valid_security_json(Path(PROJECT_CONFIG.root, SECURITY_JSON)),
5152
_is_valid_coverage(Path(PROJECT_CONFIG.root, COVERAGE_FILE)),
5253
]
53-
5454
if not all(all_is_valid_checks):
5555
sys.exit(1)
5656

5757

58-
def _missing_files(expected_files: set, directory: Path) -> set:
59-
files = {f.name for f in directory.iterdir() if f.is_file()}
60-
return expected_files - files
61-
62-
6358
def _handle_validation_error(file: Path, message: str) -> bool:
64-
print(f"error in [{file.name}]: {message}")
59+
print(f"error in [{file.name}]: {message}", file=sys.stderr)
6560
return False
6661

6762

test/unit/nox/_artifacts_test.py

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from dataclasses import dataclass
66
from inspect import cleandoc
77
from pathlib import Path
8+
from unittest import mock
89
from unittest.mock import (
910
Mock,
1011
call,
@@ -14,7 +15,7 @@
1415
import pytest
1516

1617
from exasol.toolbox.nox._artifacts import (
17-
ALL_FILES,
18+
ALL_LINT_FILES,
1819
COVERAGE_FILE,
1920
COVERAGE_TABLES,
2021
LINT_JSON,
@@ -26,11 +27,20 @@
2627
_is_valid_lint_json,
2728
_is_valid_lint_txt,
2829
_is_valid_security_json,
29-
_missing_files,
30+
check_artifacts,
3031
copy_artifacts,
3132
)
3233

3334

35+
@contextlib.contextmanager
36+
def mock_check_artifacts_session(
37+
path: Path,
38+
):
39+
with patch("exasol.toolbox.nox._artifacts.PROJECT_CONFIG") as config:
40+
config.root = path
41+
yield Mock()
42+
43+
3444
@contextlib.contextmanager
3545
def mock_session(path: Path, python_version: str, *files: str):
3646
with patch("exasol.toolbox.nox._artifacts.PROJECT_CONFIG") as config:
@@ -55,23 +65,47 @@ def __eq__(self, actual):
5565
return str(actual).endswith(self.suffix)
5666

5767

58-
@pytest.mark.parametrize(
59-
"missing_files",
60-
[
61-
(pytest.param(set(), id="all_files_present")),
62-
(pytest.param({LINT_JSON}, id="lint_json_missing")),
63-
(pytest.param({LINT_JSON, COVERAGE_FILE}, id="coverage_and_lint_json_missing")),
64-
(pytest.param(ALL_FILES, id="all_files_missing")),
65-
],
66-
)
67-
def test_missing_files(missing_files, tmp_path):
68-
existing_files = ALL_FILES - missing_files
69-
path = Path(tmp_path)
70-
for file in existing_files:
71-
Path(path, file).touch()
68+
class TestCheckArtifacts:
69+
@staticmethod
70+
def _create_artifact_files(path: Path, existing_files: set):
71+
for file in existing_files:
72+
Path(path, file).touch()
73+
74+
@mock.patch("exasol.toolbox.nox._artifacts._is_valid_lint_txt", return_value=True)
75+
@mock.patch("exasol.toolbox.nox._artifacts._is_valid_lint_json", return_value=True)
76+
@mock.patch(
77+
"exasol.toolbox.nox._artifacts._is_valid_security_json", return_value=True
78+
)
79+
@mock.patch("exasol.toolbox.nox._artifacts._is_valid_coverage", return_value=True)
80+
def test_passes_when_as_expected(
81+
self, mock_coverage, mock_security, mock_lint_json, mock_lint_txt, tmp_path
82+
):
83+
self._create_artifact_files(tmp_path, ALL_LINT_FILES)
84+
with mock_check_artifacts_session(tmp_path) as session:
85+
check_artifacts(session)
86+
87+
@pytest.mark.parametrize(
88+
"missing_files",
89+
[
90+
(pytest.param({LINT_JSON}, id="lint_json_missing")),
91+
(pytest.param(ALL_LINT_FILES, id="all_files_missing")),
92+
],
93+
)
94+
def test_fails_when_file_missing(self, tmp_path, missing_files, capsys):
95+
existing_files = ALL_LINT_FILES - missing_files
96+
self._create_artifact_files(tmp_path, existing_files)
97+
98+
with mock_check_artifacts_session(tmp_path) as session:
99+
with pytest.raises(SystemExit):
100+
check_artifacts(session)
101+
assert f"files not available: {missing_files}" in capsys.readouterr().err
72102

73-
actual = _missing_files(ALL_FILES, path)
74-
assert actual == missing_files
103+
def test_fails_when_check_fails(self, tmp_path, capsys):
104+
self._create_artifact_files(tmp_path, ALL_LINT_FILES)
105+
with mock_check_artifacts_session(tmp_path) as session:
106+
with pytest.raises(SystemExit):
107+
check_artifacts(session)
108+
assert f"error in [" in capsys.readouterr().err
75109

76110

77111
class TestIsValidLintTxt:
@@ -95,7 +129,7 @@ def test_fails_when_rating_not_found(self, tmp_path, capsys):
95129
result = _is_valid_lint_txt(path)
96130

97131
assert not result
98-
assert "Could not find a rating" in capsys.readouterr().out
132+
assert "Could not find a rating" in capsys.readouterr().err
99133

100134

101135
class TestIsValidLintJson:
@@ -122,7 +156,7 @@ def test_is_not_a_json(tmp_path, capsys):
122156
result = _is_valid_lint_json(path)
123157

124158
assert not result
125-
assert "Invalid json file" in capsys.readouterr().out
159+
assert "Invalid json file" in capsys.readouterr().err
126160

127161
@pytest.mark.parametrize(
128162
"missing_attributes", [pytest.param({"message-id"}, id="missing_message-id")]
@@ -137,7 +171,7 @@ def test_missing_attributes(self, tmp_path, capsys, missing_attributes):
137171
assert not result
138172
assert (
139173
f"missing the following attributes {missing_attributes}"
140-
in capsys.readouterr().out
174+
in capsys.readouterr().err
141175
)
142176

143177

@@ -164,7 +198,7 @@ def test_is_not_a_json(tmp_path, capsys):
164198
result = _is_valid_security_json(path)
165199

166200
assert not result
167-
assert "Invalid json file" in capsys.readouterr().out
201+
assert "Invalid json file" in capsys.readouterr().err
168202

169203
@pytest.mark.parametrize(
170204
"missing_attributes", [pytest.param({"errors"}, id="missing_errors")]
@@ -179,7 +213,7 @@ def test_missing_attributes(self, tmp_path, capsys, missing_attributes):
179213
assert not result
180214
assert (
181215
f"missing the following attributes {missing_attributes}"
182-
in capsys.readouterr().out
216+
in capsys.readouterr().err
183217
)
184218

185219

@@ -214,7 +248,7 @@ def test_database_missing_tables(self, tmp_path, capsys, missing_table):
214248

215249
assert not result
216250
assert (
217-
f"missing the following tables {missing_table}" in capsys.readouterr().out
251+
f"missing the following tables {missing_table}" in capsys.readouterr().err
218252
)
219253

220254

0 commit comments

Comments
 (0)