Repository navigation
Expand file tree
/
Copy pathtest_plugin.py
More file actions
246 lines (176 loc) · 9.16 KB
/
Copy pathtest_plugin.py
File metadata and controls
246 lines (176 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Copyright © LFV
from unittest.mock import MagicMock, patch
import pytest
import tomlkit
from cleo.io.io import IO
from poetry.poetry import Poetry
from reqstool_python_decorators.decorators.decorators import SVCs
from ruamel.yaml import YAML
from reqstool_python_poetry_plugin.plugin import ReqstoolPlugin
def _make_plugin(root_dir, reqstool_config: dict | None = None) -> ReqstoolPlugin:
plugin = ReqstoolPlugin()
poetry = MagicMock(spec=Poetry)
poetry.package.root_dir = root_dir
poetry.package.version = "1.0.0"
poetry.pyproject.data = {"tool": {"reqstool": reqstool_config or {}}}
plugin._poetry = poetry
plugin._cleo_io = MagicMock(spec=IO)
return plugin
@SVCs("SVC_POETRY_PLUGIN_003")
def test_update_sdist_include_adds_reqstool_entries(tmp_path):
pyproject_path = tmp_path / "pyproject.toml"
pyproject_path.write_text('[tool.poetry]\nname = "x"\n')
plugin = _make_plugin(tmp_path)
plugin._update_sdist_include()
data = tomlkit.loads(pyproject_path.read_text())
includes = list(data["tool"]["poetry"]["include"])
assert "reqstool_config.yml" in includes
assert "reqstool/**/*" in includes
assert "build/reqstool/**/*" in includes
@SVCs("SVC_POETRY_PLUGIN_003")
def test_update_sdist_include_is_idempotent(tmp_path):
pyproject_path = tmp_path / "pyproject.toml"
pyproject_path.write_text('[tool.poetry]\nname = "x"\ninclude = ["reqstool_config.yml"]\n')
plugin = _make_plugin(tmp_path)
plugin._update_sdist_include()
plugin._update_sdist_include()
data = tomlkit.loads(pyproject_path.read_text())
includes = list(data["tool"]["poetry"]["include"])
assert includes.count("reqstool_config.yml") == 1
assert includes.count("reqstool/**/*") == 1
assert includes.count("build/reqstool/**/*") == 1
@SVCs("SVC_POETRY_PLUGIN_003")
def test_update_sdist_include_honors_custom_directories(tmp_path):
pyproject_path = tmp_path / "pyproject.toml"
pyproject_path.write_text('[tool.poetry]\nname = "x"\n')
plugin = _make_plugin(
tmp_path, reqstool_config={"dataset_directory": "custom_ds", "output_directory": "custom_out"}
)
plugin._update_sdist_include()
data = tomlkit.loads(pyproject_path.read_text())
includes = list(data["tool"]["poetry"]["include"])
assert "custom_ds/**/*" in includes
assert "custom_out/**/*" in includes
@SVCs("SVC_POETRY_PLUGIN_004")
def test_cleanup_post_build_removes_reqstool_config(tmp_path):
config_file = tmp_path / "reqstool_config.yml"
config_file.write_text("language: python\n")
plugin = _make_plugin(tmp_path)
plugin._cleanup_post_build()
assert not config_file.exists()
@SVCs("SVC_POETRY_PLUGIN_004")
def test_cleanup_post_build_is_a_noop_when_no_config_file(tmp_path):
plugin = _make_plugin(tmp_path)
plugin._cleanup_post_build()
plugin._cleo_io.write_line.assert_not_called()
@SVCs("SVC_POETRY_PLUGIN_005")
def test_cleanup_pyproject_strips_excess_blank_lines(tmp_path):
pyproject_path = tmp_path / "pyproject.toml"
pyproject_path.write_text('[tool.poetry]\nname = "x"\n\n\n\ninclude = ["reqstool_config.yml"]\n')
plugin = _make_plugin(tmp_path)
plugin._cleanup_pyproject_install_after_install()
assert pyproject_path.read_text() == '[tool.poetry]\nname = "x"\n\ninclude = ["reqstool_config.yml"]\n'
@SVCs("SVC_POETRY_PLUGIN_005")
def test_cleanup_pyproject_is_a_noop_without_excess_blank_lines(tmp_path):
pyproject_path = tmp_path / "pyproject.toml"
original = '[tool.poetry]\nname = "x"\n\ninclude = ["reqstool_config.yml"]\n'
pyproject_path.write_text(original)
plugin = _make_plugin(tmp_path)
plugin._cleanup_pyproject_install_after_install()
assert pyproject_path.read_text() == original
@SVCs("SVC_POETRY_PLUGIN_002")
def test_create_annotations_file_passes_configured_sources_and_output_path(tmp_path):
plugin = _make_plugin(
tmp_path, reqstool_config={"sources": ["src", "tests/unit"], "output_directory": "build/reqstool"}
)
with patch("reqstool_python_poetry_plugin.plugin.DecoratorProcessor") as decorator_processor_cls:
plugin._create_annotations_file()
decorator_processor_cls.return_value.process_decorated_data.assert_called_once_with(
path_to_python_files=["src", "tests/unit"],
output_file="build/reqstool/annotations.yml",
)
@SVCs("SVC_POETRY_PLUGIN_001")
def test_generate_reqstool_config_raises_when_requirements_missing(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
(tmp_path / "reqstool").mkdir()
plugin = _make_plugin(tmp_path)
with pytest.raises(RuntimeError, match="requirements.yml"):
plugin._generate_reqstool_config()
@SVCs("SVC_POETRY_PLUGIN_001")
def test_generate_reqstool_config_includes_only_existing_optional_files(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
dataset_dir = tmp_path / "reqstool"
dataset_dir.mkdir()
(dataset_dir / "requirements.yml").write_text("requirements: []\n")
(dataset_dir / "software_verification_cases.yml").write_text("software_verification_cases: []\n")
plugin = _make_plugin(tmp_path)
plugin._generate_reqstool_config()
output = tmp_path / "reqstool_config.yml"
data = YAML().load(output.read_text())
resources = data["resources"]
assert "requirements" in resources
assert "software_verification_cases" in resources
assert "manual_verification_results" not in resources
assert "annotations" not in resources
@SVCs("SVC_POETRY_PLUGIN_001")
def test_generate_reqstool_config_normalizes_test_results_to_a_list(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
dataset_dir = tmp_path / "reqstool"
dataset_dir.mkdir()
(dataset_dir / "requirements.yml").write_text("requirements: []\n")
plugin = _make_plugin(tmp_path, reqstool_config={"test_results": "build/junit.xml"})
plugin._generate_reqstool_config()
output = tmp_path / "reqstool_config.yml"
data = YAML().load(output.read_text())
assert data["resources"]["test_results"] == ["build/junit.xml"]
# ---------------------------------------------------------------------------
# Text files are UTF-8 whatever the platform default encoding is (cp1252 on Windows)
# ---------------------------------------------------------------------------
# "Á" is C3 81 in UTF-8 and 0x81 is undefined in cp1252; "✓" has no cp1252 mapping at all.
_NON_ASCII = "Ágnes Müller ✓"
@pytest.fixture
def cp1252_default_encoding(monkeypatch):
"""Make open() and Path.read_text()/write_text() default to cp1252, as on a Windows machine."""
import builtins
import pathlib
real_open = builtins.open
real_read_text = pathlib.Path.read_text
real_write_text = pathlib.Path.write_text
def open_cp1252_default(file, mode="r", *args, **kwargs):
if "b" not in mode and kwargs.get("encoding") is None and len(args) < 2:
kwargs["encoding"] = "cp1252"
return real_open(file, mode, *args, **kwargs)
def read_text_cp1252_default(self, encoding=None, errors=None, newline=None):
return real_read_text(self, encoding=encoding or "cp1252", errors=errors, newline=newline)
def write_text_cp1252_default(self, data, encoding=None, errors=None, newline=None):
return real_write_text(self, data, encoding=encoding or "cp1252", errors=errors, newline=newline)
monkeypatch.setattr("reqstool_python_poetry_plugin.plugin.open", open_cp1252_default, raising=False)
monkeypatch.setattr(pathlib.Path, "read_text", read_text_cp1252_default)
monkeypatch.setattr(pathlib.Path, "write_text", write_text_cp1252_default)
@SVCs("SVC_POETRY_PLUGIN_003")
def test_update_sdist_include_preserves_utf8_pyproject(tmp_path, cp1252_default_encoding):
pyproject_path = tmp_path / "pyproject.toml"
pyproject_path.write_bytes(f'[tool.poetry]\nname = "x"\nauthors = ["{_NON_ASCII}"]\n'.encode("utf-8"))
plugin = _make_plugin(tmp_path)
plugin._update_sdist_include()
data = tomlkit.loads(pyproject_path.read_bytes().decode("utf-8"))
assert data["tool"]["poetry"]["authors"] == [_NON_ASCII]
assert "reqstool_config.yml" in list(data["tool"]["poetry"]["include"])
@SVCs("SVC_POETRY_PLUGIN_005")
def test_cleanup_pyproject_preserves_utf8_pyproject(tmp_path, cp1252_default_encoding):
pyproject_path = tmp_path / "pyproject.toml"
pyproject_path.write_bytes(f'[tool.poetry]\nname = "x"\n\n\n\nauthors = ["{_NON_ASCII}"]\n'.encode("utf-8"))
plugin = _make_plugin(tmp_path)
plugin._cleanup_pyproject_install_after_install()
expected = f'[tool.poetry]\nname = "x"\n\nauthors = ["{_NON_ASCII}"]\n'
assert pyproject_path.read_bytes().decode("utf-8") == expected
@SVCs("SVC_POETRY_PLUGIN_001")
def test_generate_reqstool_config_is_written_as_utf8(tmp_path, monkeypatch, cp1252_default_encoding):
monkeypatch.chdir(tmp_path)
dataset_dir = tmp_path / "dokumentation ✓"
dataset_dir.mkdir()
(dataset_dir / "requirements.yml").write_bytes(b"requirements: []\n")
plugin = _make_plugin(tmp_path, reqstool_config={"dataset_directory": "dokumentation ✓"})
plugin._generate_reqstool_config()
written = (tmp_path / "reqstool_config.yml").read_bytes().decode("utf-8")
assert YAML().load(written)["resources"]["requirements"] == "dokumentation ✓/requirements.yml"