Skip to content

Commit f71df38

Browse files
committed
Add factory to create PROJECT_CONFIG equivalent for tests
1 parent 0fdb113 commit f71df38

File tree

8 files changed

+122
-78
lines changed

8 files changed

+122
-78
lines changed

test/unit/config_test.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
class TestBaseConfig:
1212
@staticmethod
13-
def test_works_as_defined():
14-
BaseConfig()
13+
def test_works_as_defined(test_project_config_factory):
14+
test_project_config_factory()
1515

1616
@staticmethod
1717
@pytest.mark.parametrize(
@@ -58,14 +58,16 @@ def test_expansion_validation_fails_for_invalid_version():
5858
BaseConfigExpansion(python_versions=("1.f.0",))
5959

6060

61-
def test_minimum_python_version():
62-
conf = BaseConfig(python_versions=("5.5.5", "1.10", "9.9.9"))
61+
def test_minimum_python_version(test_project_config_factory):
62+
conf = test_project_config_factory(python_versions=("5.5.5", "1.10", "9.9.9"))
6363
assert conf.minimum_python_version == "1.10"
6464

6565

6666
@pytest.mark.parametrize("minimum_python_version", ["3.10", "3.10.5"])
67-
def test_pyupgrade_argument(minimum_python_version):
68-
conf = BaseConfig(python_versions=("3.11", minimum_python_version, "3.12"))
67+
def test_pyupgrade_argument(test_project_config_factory, minimum_python_version):
68+
conf = test_project_config_factory(
69+
python_versions=("3.11", minimum_python_version, "3.12")
70+
)
6971
assert conf.pyupgrade_argument == ("--py310-plus",)
7072

7173

@@ -83,6 +85,10 @@ def test_pyupgrade_argument(minimum_python_version):
8385
),
8486
],
8587
)
86-
def test_excluded_python_paths(add_to_excluded_python_paths, expected):
87-
conf = BaseConfig(add_to_excluded_python_paths=add_to_excluded_python_paths)
88+
def test_excluded_python_paths(
89+
test_project_config_factory, add_to_excluded_python_paths, expected
90+
):
91+
conf = test_project_config_factory(
92+
add_to_excluded_python_paths=add_to_excluded_python_paths
93+
)
8894
assert sorted(conf.excluded_python_paths) == sorted(expected)

test/unit/conftest.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
from pathlib import Path
2-
31
import pytest
42
from toolbox.config import BaseConfig
53

64

75
@pytest.fixture
8-
def test_project_config():
9-
return BaseConfig(root_path=Path("."), project_name="test")
6+
def test_project_config_factory(tmp_path):
7+
def _test_project_config(**kwargs) -> BaseConfig:
8+
defaults = {
9+
"root_path": tmp_path,
10+
"project_name": "test",
11+
}
12+
config = {**defaults, **kwargs}
13+
return BaseConfig(**config)
14+
15+
return _test_project_config

test/unit/nox/_artifacts_test.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
check_artifacts,
3737
copy_artifacts,
3838
)
39+
from noxconfig import Config
3940

4041

4142
@contextlib.contextmanager
4243
def mock_check_artifacts_session(
43-
path: Path,
44+
config: Config,
4445
):
45-
with patch("exasol.toolbox.nox._artifacts.PROJECT_CONFIG") as config:
46-
config.root = path
46+
with patch("exasol.toolbox.nox._artifacts.PROJECT_CONFIG", new=config):
4747
yield Mock()
4848

4949

@@ -92,10 +92,18 @@ def _create_artifact_files(path: Path, existing_files: set):
9292
)
9393
@mock.patch("exasol.toolbox.nox._artifacts._is_valid_coverage", return_value=True)
9494
def test_passes_when_as_expected(
95-
self, mock_coverage, mock_security, mock_lint_json, mock_lint_txt, tmp_path
95+
self,
96+
mock_coverage,
97+
mock_security,
98+
mock_lint_json,
99+
mock_lint_txt,
100+
test_project_config_factory,
101+
tmp_path,
96102
):
97103
self._create_artifact_files(tmp_path, ALL_LINT_FILES)
98-
with mock_check_artifacts_session(tmp_path) as session:
104+
with mock_check_artifacts_session(
105+
config=test_project_config_factory()
106+
) as session:
99107
check_artifacts(session)
100108

101109
@pytest.mark.parametrize(
@@ -105,18 +113,26 @@ def test_passes_when_as_expected(
105113
(pytest.param(ALL_LINT_FILES, id="all_files_missing")),
106114
],
107115
)
108-
def test_fails_when_file_missing(self, tmp_path, missing_files, capsys):
116+
def test_fails_when_file_missing(
117+
self, test_project_config_factory, tmp_path, missing_files, capsys
118+
):
109119
existing_files = ALL_LINT_FILES - missing_files
110120
self._create_artifact_files(tmp_path, existing_files)
111121

112-
with mock_check_artifacts_session(tmp_path) as session:
122+
with mock_check_artifacts_session(
123+
config=test_project_config_factory()
124+
) as session:
113125
with pytest.raises(SystemExit):
114126
check_artifacts(session)
115127
assert f"files not available: {missing_files}" in capsys.readouterr().err
116128

117-
def test_fails_when_check_fails(self, tmp_path, capsys):
129+
def test_fails_when_check_fails(
130+
self, test_project_config_factory, tmp_path, capsys
131+
):
118132
self._create_artifact_files(tmp_path, ALL_LINT_FILES)
119-
with mock_check_artifacts_session(tmp_path) as session:
133+
with mock_check_artifacts_session(
134+
config=test_project_config_factory()
135+
) as session:
120136
with pytest.raises(SystemExit):
121137
check_artifacts(session)
122138
assert "error in [" in capsys.readouterr().err

test/unit/nox/_format_test.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,33 @@
1212
fix_format,
1313
)
1414
from exasol.toolbox.nox._shared import Mode
15-
from noxconfig import Config
1615

1716

1817
@pytest.fixture
19-
def file_with_unneeded_import(tmp_path):
20-
file_path = tmp_path / "dummy_file.py"
18+
def config(test_project_config_factory):
19+
return test_project_config_factory()
20+
21+
22+
@pytest.fixture
23+
def file_with_unneeded_import(config):
24+
config.source_code_path.mkdir(parents=True, exist_ok=True)
25+
file_path = config.source_code_path / "dummy_file.py"
2126
file_path.write_text("import black")
2227
return file_path
2328

2429

2530
@pytest.fixture
26-
def file_with_not_ordered_import(tmp_path):
27-
file_path = tmp_path / "dummy_file.py"
31+
def file_with_not_ordered_import(config):
32+
config.source_code_path.mkdir(parents=True, exist_ok=True)
33+
file_path = config.source_code_path / "dummy_file.py"
2834
file_path.write_text("import isort\nimport black")
2935
return file_path
3036

3137

3238
@pytest.fixture
33-
def file_without_blank_line(tmp_path):
34-
file_path = tmp_path / "dummy_file.py"
39+
def file_without_blank_line(config):
40+
config.source_code_path.mkdir(parents=True, exist_ok=True)
41+
file_path = config.source_code_path / "dummy_file.py"
3542
file_path.write_text("import black\nimport isort")
3643
return file_path
3744

@@ -86,10 +93,11 @@ def test_black_mode_check(nox_session, file_without_blank_line, caplog):
8693
assert file_without_blank_line.read_text() == "import black\nimport isort"
8794

8895

89-
def test_pyupgrade(nox_session, tmp_path):
90-
file_path = tmp_path / "dummy_file.py"
96+
def test_pyupgrade(nox_session, config):
97+
config.source_code_path.mkdir(parents=True, exist_ok=True)
98+
file_path = config.source_code_path / "dummy_file.py"
9199
file_path.write_text("from typing import Union\nx:Union[int, str]=2")
92-
_pyupgrade(session=nox_session, config=Config(), files=[str(file_path)])
100+
_pyupgrade(session=nox_session, config=config, files=[str(file_path)])
93101
assert file_path.read_text() == "from typing import Union\nx:int | str=2"
94102

95103

@@ -148,11 +156,9 @@ def file_with_multiple_problems(tmp_path):
148156
return file_path
149157

150158

151-
def test_fix_format(nox_session, tmp_path, file_with_multiple_problems):
152-
with patch("exasol.toolbox.nox._format.PROJECT_CONFIG") as config:
159+
def test_fix_format(nox_session, config, file_with_multiple_problems):
160+
with patch("exasol.toolbox.nox._format.PROJECT_CONFIG", new=config):
153161
with patch("exasol.toolbox.nox._format._version") as version:
154-
config.root = tmp_path
155-
config.pyupgrade_argument = ("--py310-plus",)
156162
# Simulate version is up-to-date, as version check is out of the scope of the test case
157163
version.return_value = True
158164
fix_format(nox_session)
@@ -171,13 +177,10 @@ def test_fix_format(nox_session, tmp_path, file_with_multiple_problems):
171177
)
172178

173179

174-
def test_check_format(
175-
nox_session, tmp_path, file_with_multiple_problems, caplog, capsys
176-
):
180+
def test_check_format(nox_session, config, file_with_multiple_problems, caplog, capsys):
177181
expected_text = file_with_multiple_problems.read_text()
178182

179-
with patch("exasol.toolbox.nox._format.PROJECT_CONFIG") as config:
180-
config.root = tmp_path
183+
with patch("exasol.toolbox.nox._format.PROJECT_CONFIG", new=config):
181184
with pytest.raises(CommandFailed):
182185
check_format(nox_session)
183186

test/unit/nox/_lint_test.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
from inspect import cleandoc
3-
from pathlib import Path
43
from unittest.mock import patch
54

65
import pytest
@@ -14,7 +13,12 @@
1413

1514

1615
@pytest.fixture
17-
def file_with_multiple_problems(tmp_path):
16+
def config(test_project_config_factory):
17+
return test_project_config_factory()
18+
19+
20+
@pytest.fixture
21+
def file_with_multiple_problems(config):
1822
"""
1923
In this file with multiple problems, it is expected that the nox
2024
lint sessions would detect the following errors:
@@ -30,8 +34,8 @@ def file_with_multiple_problems(tmp_path):
3034
* [B607:start_process_with_partial_path] Starting a process with a partial executable path
3135
* [B603:subprocess_without_shell_equals_true] subprocess call - check for execution of untrusted input.
3236
"""
33-
34-
file_path = tmp_path / "dummy_file.py"
37+
config.source_code_path.mkdir(parents=True, exist_ok=True)
38+
file_path = config.source_code_path / "dummy_file.py"
3539
text = """
3640
import subprocess
3741
@@ -42,15 +46,13 @@ def file_with_multiple_problems(tmp_path):
4246
return file_path
4347

4448

45-
def test_lint(nox_session, tmp_path, file_with_multiple_problems):
46-
with patch("exasol.toolbox.nox._lint.PROJECT_CONFIG") as config:
47-
config.root = tmp_path
48-
config.source = Path("")
49+
def test_lint(nox_session, config, file_with_multiple_problems):
50+
with patch("exasol.toolbox.nox._lint.PROJECT_CONFIG", new=config):
4951
with pytest.raises(CommandFailed, match="Returned code 20"):
5052
lint(session=nox_session)
5153

52-
json_file = tmp_path / ".lint.json"
53-
txt_file = tmp_path / ".lint.txt"
54+
json_file = config.root_path / ".lint.json"
55+
txt_file = config.root_path / ".lint.txt"
5456

5557
assert json_file.exists()
5658
assert txt_file.exists()
@@ -60,10 +62,8 @@ def test_lint(nox_session, tmp_path, file_with_multiple_problems):
6062
assert {"C0114", "C0304", "W1510"}.issubset(errors)
6163

6264

63-
def test_type_check(nox_session, tmp_path, file_with_multiple_problems, caplog):
64-
with patch("exasol.toolbox.nox._lint.PROJECT_CONFIG") as config:
65-
config.root = tmp_path
66-
config.source = Path("")
65+
def test_type_check(nox_session, config, file_with_multiple_problems, caplog):
66+
with patch("exasol.toolbox.nox._lint.PROJECT_CONFIG", new=config):
6767
with pytest.raises(CommandFailed, match="Returned code 1"):
6868
type_check(session=nox_session)
6969

@@ -74,13 +74,11 @@ def test_type_check(nox_session, tmp_path, file_with_multiple_problems, caplog):
7474
)
7575

7676

77-
def test_security_lint(nox_session, tmp_path, file_with_multiple_problems):
78-
with patch("exasol.toolbox.nox._lint.PROJECT_CONFIG") as config:
79-
config.root = tmp_path
80-
config.source = Path("")
77+
def test_security_lint(nox_session, config, file_with_multiple_problems):
78+
with patch("exasol.toolbox.nox._lint.PROJECT_CONFIG", new=config):
8179
security_lint(session=nox_session)
8280

83-
output_file = tmp_path / ".security.json"
81+
output_file = config.root_path / ".security.json"
8482
assert output_file.exists()
8583

8684
contents = output_file.read_text()

test/unit/nox/_package_version_test.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515

1616

1717
@pytest.fixture
18-
def config(version_file) -> Config:
19-
return Config(version_file=version_file)
18+
def config(test_project_config_factory) -> Config:
19+
config = test_project_config_factory()
20+
# We need to set up the directory path so that version_file can execute
21+
config.source_code_path.mkdir(parents=True, exist_ok=True)
22+
return config
2023

2124

2225
@pytest.fixture
23-
def version_file(tmp_path):
24-
version_file = tmp_path / "version.py"
26+
def version_file(config):
27+
version_file = config.version_filepath
2528
write_version_module(version=DEFAULT_VERSION, version_file=version_file)
2629
return version_file
2730

@@ -38,7 +41,7 @@ def test_write_version_module(version_file) -> None:
3841
class TestVersionCheck:
3942
@staticmethod
4043
@mock.patch.object(Version, "from_poetry", return_value=DEFAULT_VERSION)
41-
def test_same_value_is_successful(from_poetry, config):
44+
def test_same_value_is_successful(from_poetry, config, version_file):
4245
Version(major=0, minor=1, patch=0)
4346
parser = _create_parser()
4447
args = parser.parse_args([])
@@ -48,7 +51,7 @@ def test_same_value_is_successful(from_poetry, config):
4851

4952
@staticmethod
5053
@mock.patch.object(Version, "from_poetry", return_value=ALTERNATE_VERSION)
51-
def test_different_value_is_failure(from_poetry, config):
54+
def test_different_value_is_failure(from_poetry, config, version_file):
5255
Version(major=0, minor=1, patch=0)
5356
parser = _create_parser()
5457
args = parser.parse_args([])

0 commit comments

Comments
 (0)