55from dataclasses import dataclass
66from inspect import cleandoc
77from pathlib import Path
8+ from unittest import mock
89from unittest .mock import (
910 Mock ,
1011 call ,
1415import pytest
1516
1617from exasol .toolbox .nox ._artifacts import (
17- ALL_FILES ,
18+ ALL_LINT_FILES ,
1819 COVERAGE_FILE ,
1920 COVERAGE_TABLES ,
2021 LINT_JSON ,
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
3545def 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
77111class 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
101135class 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