|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import os |
| 4 | +import re |
3 | 5 | from pathlib import Path |
| 6 | +from unittest import mock |
4 | 7 |
|
5 | 8 | import pytest |
6 | 9 |
|
7 | 10 |
|
8 | | -@pytest.fixture() |
9 | | -def example(testdir: pytest.Testdir) -> pytest.Testdir: |
10 | | - src = Path(__file__).parent / "example.py" |
11 | | - dest = Path(str(testdir.tmpdir / "test_example.py")) |
12 | | - dest.symlink_to(src) |
13 | | - return testdir |
| 11 | +@pytest.mark.parametrize( |
| 12 | + ("env", "ini", "expected_env"), |
| 13 | + [ |
| 14 | + pytest.param( |
| 15 | + {}, |
| 16 | + "[pytest]\nenv = MAGIC=alpha", |
| 17 | + {"MAGIC": "alpha"}, |
| 18 | + id="new key - add to env", |
| 19 | + ), |
| 20 | + pytest.param( |
| 21 | + {}, |
| 22 | + "[pytest]\nenv = MAGIC=alpha\n SORCERY=beta", |
| 23 | + {"MAGIC": "alpha", "SORCERY": "beta"}, |
| 24 | + id="two new keys - add to env", |
| 25 | + ), |
| 26 | + pytest.param( |
| 27 | + # This test also tests for non-interference of env variables between this test and tests above |
| 28 | + {}, |
| 29 | + "[pytest]\nenv = d:MAGIC=beta", |
| 30 | + {"MAGIC": "beta"}, |
| 31 | + id="D flag - add to env", |
| 32 | + ), |
| 33 | + pytest.param( |
| 34 | + {"MAGIC": "alpha"}, |
| 35 | + "[pytest]\nenv = MAGIC=beta", |
| 36 | + {"MAGIC": "beta"}, |
| 37 | + id="key exists in env - overwrite", |
| 38 | + ), |
| 39 | + pytest.param( |
| 40 | + {"MAGIC": "alpha"}, |
| 41 | + "[pytest]\nenv = D:MAGIC=beta", |
| 42 | + {"MAGIC": "alpha"}, |
| 43 | + id="D exists - original val kept", |
| 44 | + ), |
| 45 | + pytest.param( |
| 46 | + {"PLANET": "world"}, |
| 47 | + "[pytest]\nenv = MAGIC=hello_{PLANET}", |
| 48 | + {"MAGIC": "hello_world"}, |
| 49 | + id="curly exist - interpolate var", |
| 50 | + ), |
| 51 | + pytest.param( |
| 52 | + {"PLANET": "world"}, |
| 53 | + "[pytest]\nenv = R:MAGIC=hello_{PLANET}", |
| 54 | + {"MAGIC": "hello_{PLANET}"}, |
| 55 | + id="R exists - not interpolate var", |
| 56 | + ), |
| 57 | + pytest.param( |
| 58 | + {"MAGIC": "a"}, |
| 59 | + "[pytest]\nenv = R:MAGIC={MAGIC}b\n D:MAGIC={MAGIC}c\n MAGIC={MAGIC}d", |
| 60 | + {"MAGIC": "{MAGIC}bd"}, |
| 61 | + id="incremental interpolation", |
| 62 | + ), |
| 63 | + pytest.param( |
| 64 | + {"PLANET": "world"}, |
| 65 | + "[pytest]\nenv = D:R:RESULT=hello_{PLANET}", |
| 66 | + {"RESULT": "hello_{PLANET}"}, |
| 67 | + id="two flags", |
| 68 | + ), |
| 69 | + pytest.param( |
| 70 | + {"PLANET": "world"}, |
| 71 | + "[pytest]\nenv = R:D:RESULT=hello_{PLANET}", |
| 72 | + {"RESULT": "hello_{PLANET}"}, |
| 73 | + id="two flags - reversed", |
| 74 | + ), |
| 75 | + pytest.param( |
| 76 | + {"PLANET": "world"}, |
| 77 | + "[pytest]\nenv = d:r:RESULT=hello_{PLANET}", |
| 78 | + {"RESULT": "hello_{PLANET}"}, |
| 79 | + id="lowercase flags", |
| 80 | + ), |
| 81 | + pytest.param( |
| 82 | + {"PLANET": "world"}, |
| 83 | + "[pytest]\nenv = D : R : RESULT = hello_{PLANET}", |
| 84 | + {"RESULT": "hello_{PLANET}"}, |
| 85 | + id="whitespace is ignored", |
| 86 | + ), |
| 87 | + pytest.param( |
| 88 | + {"MAGIC": "zero"}, |
| 89 | + "", |
| 90 | + {"MAGIC": "zero"}, |
| 91 | + id="empty ini works", |
| 92 | + ), |
| 93 | + ], |
| 94 | +) |
| 95 | +def test_env( |
| 96 | + testdir: pytest.Testdir, env: dict[str, str], ini: str, expected_env: dict[str, str], request: pytest.FixtureRequest |
| 97 | +) -> None: |
| 98 | + test_name = re.sub(r"\W|^(?=\d)", "_", request.node.callspec.id).lower() |
| 99 | + Path(str(testdir.tmpdir / f"test_{test_name}.py")).symlink_to(Path(__file__).parent / "template.py") |
| 100 | + (testdir.tmpdir / "pytest.ini").write_text(ini, encoding="utf-8") |
14 | 101 |
|
| 102 | + # monkeypatch persists env variables across parametrized tests, therefore using mock.patch.dict |
| 103 | + with mock.patch.dict(os.environ, {**env, "_TEST_ENV": repr(expected_env)}, clear=True): |
| 104 | + result = testdir.runpytest() |
15 | 105 |
|
16 | | -def test_simple(example: pytest.Testdir) -> None: |
17 | | - (example.tmpdir / "pytest.ini").write_text("[pytest]\nenv = MAGIC=alpha", encoding="utf-8") |
18 | | - example.monkeypatch.setenv("_PATCH", "alpha") |
19 | | - result = example.runpytest() |
20 | 106 | result.assert_outcomes(passed=1) |
0 commit comments