|
3 | 3 | import importlib.util |
4 | 4 | import json |
5 | 5 | import re |
| 6 | +import runpy |
6 | 7 | import shutil |
7 | 8 | import sys |
8 | 9 | from dataclasses import dataclass |
@@ -158,6 +159,64 @@ def test_fixer_cases( |
158 | 159 | ) |
159 | 160 |
|
160 | 161 |
|
| 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 | + |
161 | 220 | @pytest.mark.parametrize( |
162 | 221 | ("dependency", "version", "original", "expected"), |
163 | 222 | [ |
|
0 commit comments