Skip to content

Commit dc9585c

Browse files
committed
test: Add tests for invocation as a script
All other tests are calling the fixer.main() function (or other internal functions), but none is really testing the fixer works as a script, and its arguments work as expected. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent 7d69322 commit dc9585c

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

tests/cookiecutter/scripts/test_dependabot-grpc-fixer.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import importlib.util
44
import json
55
import re
6+
import runpy
67
import shutil
78
import sys
89
from dataclasses import dataclass
@@ -158,6 +159,64 @@ def test_fixer_cases(
158159
)
159160

160161

162+
def test_fixer_runs_as_script(
163+
tmp_path: Path,
164+
monkeypatch: pytest.MonkeyPatch,
165+
capsys: pytest.CaptureFixture[str],
166+
) -> None:
167+
"""Exercise the module's `__main__` entrypoint."""
168+
case = _load_case(CASE_ROOT / "update_runtime_bounds")
169+
workspace = _prepare_workspace(case, tmp_path, monkeypatch)
170+
171+
monkeypatch.setattr(sys, "argv", [str(FIXER_PATH)])
172+
173+
runpy.run_path(str(FIXER_PATH), run_name="__main__")
174+
175+
captured = capsys.readouterr()
176+
assert (
177+
captured.out == "Updated pyproject.toml with 2 grpc/protobuf constraint(s).\n"
178+
)
179+
assert captured.err == ""
180+
assert workspace.read_text(encoding="utf-8") == case.expected_pyproject.read_text(
181+
encoding="utf-8"
182+
)
183+
184+
185+
def test_fixer_runs_as_script_with_custom_pyproject_path(
186+
tmp_path: Path,
187+
monkeypatch: pytest.MonkeyPatch,
188+
capsys: pytest.CaptureFixture[str],
189+
) -> None:
190+
"""Exercise the module's `__main__` entrypoint with a custom path."""
191+
case = _load_case(CASE_ROOT / "update_runtime_bounds")
192+
workspace = _prepare_workspace(case, tmp_path, monkeypatch)
193+
input_pyproject = case.path / "input" / "pyproject.toml"
194+
custom_pyproject = tmp_path / "custom" / "pyproject.toml"
195+
custom_pyproject.parent.mkdir(parents=True)
196+
shutil.copyfile(input_pyproject, custom_pyproject)
197+
198+
monkeypatch.setattr(
199+
sys,
200+
"argv",
201+
[str(FIXER_PATH), str(custom_pyproject.relative_to(tmp_path))],
202+
)
203+
204+
runpy.run_path(str(FIXER_PATH), run_name="__main__")
205+
206+
captured = capsys.readouterr()
207+
assert (
208+
captured.out
209+
== "Updated custom/pyproject.toml with 2 grpc/protobuf constraint(s).\n"
210+
)
211+
assert captured.err == ""
212+
assert workspace.read_text(encoding="utf-8") == input_pyproject.read_text(
213+
encoding="utf-8"
214+
)
215+
assert custom_pyproject.read_text(
216+
encoding="utf-8"
217+
) == case.expected_pyproject.read_text(encoding="utf-8")
218+
219+
161220
@pytest.mark.parametrize(
162221
("dependency", "version", "original", "expected"),
163222
[

0 commit comments

Comments
 (0)