-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add R: prefix implementation and unit tests (#9)
Co-authored-by: Bernát Gábor <gaborjbernat@gmail.com>
- Loading branch information
1 parent
59d909b
commit 83f64b1
Showing
6 changed files
with
152 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from __future__ import annotations | ||
|
||
import ast | ||
import os | ||
|
||
|
||
def test_env() -> None: | ||
for key, value in ast.literal_eval(os.environ["_TEST_ENV"]).items(): | ||
assert os.environ[key] == value, key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,106 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import re | ||
from pathlib import Path | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture() | ||
def example(testdir: pytest.Testdir) -> pytest.Testdir: | ||
src = Path(__file__).parent / "example.py" | ||
dest = Path(str(testdir.tmpdir / "test_example.py")) | ||
dest.symlink_to(src) | ||
return testdir | ||
@pytest.mark.parametrize( | ||
("env", "ini", "expected_env"), | ||
[ | ||
pytest.param( | ||
{}, | ||
"[pytest]\nenv = MAGIC=alpha", | ||
{"MAGIC": "alpha"}, | ||
id="new key - add to env", | ||
), | ||
pytest.param( | ||
{}, | ||
"[pytest]\nenv = MAGIC=alpha\n SORCERY=beta", | ||
{"MAGIC": "alpha", "SORCERY": "beta"}, | ||
id="two new keys - add to env", | ||
), | ||
pytest.param( | ||
# This test also tests for non-interference of env variables between this test and tests above | ||
{}, | ||
"[pytest]\nenv = d:MAGIC=beta", | ||
{"MAGIC": "beta"}, | ||
id="D flag - add to env", | ||
), | ||
pytest.param( | ||
{"MAGIC": "alpha"}, | ||
"[pytest]\nenv = MAGIC=beta", | ||
{"MAGIC": "beta"}, | ||
id="key exists in env - overwrite", | ||
), | ||
pytest.param( | ||
{"MAGIC": "alpha"}, | ||
"[pytest]\nenv = D:MAGIC=beta", | ||
{"MAGIC": "alpha"}, | ||
id="D exists - original val kept", | ||
), | ||
pytest.param( | ||
{"PLANET": "world"}, | ||
"[pytest]\nenv = MAGIC=hello_{PLANET}", | ||
{"MAGIC": "hello_world"}, | ||
id="curly exist - interpolate var", | ||
), | ||
pytest.param( | ||
{"PLANET": "world"}, | ||
"[pytest]\nenv = R:MAGIC=hello_{PLANET}", | ||
{"MAGIC": "hello_{PLANET}"}, | ||
id="R exists - not interpolate var", | ||
), | ||
pytest.param( | ||
{"MAGIC": "a"}, | ||
"[pytest]\nenv = R:MAGIC={MAGIC}b\n D:MAGIC={MAGIC}c\n MAGIC={MAGIC}d", | ||
{"MAGIC": "{MAGIC}bd"}, | ||
id="incremental interpolation", | ||
), | ||
pytest.param( | ||
{"PLANET": "world"}, | ||
"[pytest]\nenv = D:R:RESULT=hello_{PLANET}", | ||
{"RESULT": "hello_{PLANET}"}, | ||
id="two flags", | ||
), | ||
pytest.param( | ||
{"PLANET": "world"}, | ||
"[pytest]\nenv = R:D:RESULT=hello_{PLANET}", | ||
{"RESULT": "hello_{PLANET}"}, | ||
id="two flags - reversed", | ||
), | ||
pytest.param( | ||
{"PLANET": "world"}, | ||
"[pytest]\nenv = d:r:RESULT=hello_{PLANET}", | ||
{"RESULT": "hello_{PLANET}"}, | ||
id="lowercase flags", | ||
), | ||
pytest.param( | ||
{"PLANET": "world"}, | ||
"[pytest]\nenv = D : R : RESULT = hello_{PLANET}", | ||
{"RESULT": "hello_{PLANET}"}, | ||
id="whitespace is ignored", | ||
), | ||
pytest.param( | ||
{"MAGIC": "zero"}, | ||
"", | ||
{"MAGIC": "zero"}, | ||
id="empty ini works", | ||
), | ||
], | ||
) | ||
def test_env( | ||
testdir: pytest.Testdir, env: dict[str, str], ini: str, expected_env: dict[str, str], request: pytest.FixtureRequest | ||
) -> None: | ||
test_name = re.sub(r"\W|^(?=\d)", "_", request.node.callspec.id).lower() | ||
Path(str(testdir.tmpdir / f"test_{test_name}.py")).symlink_to(Path(__file__).parent / "template.py") | ||
(testdir.tmpdir / "pytest.ini").write_text(ini, encoding="utf-8") | ||
|
||
# monkeypatch persists env variables across parametrized tests, therefore using mock.patch.dict | ||
with mock.patch.dict(os.environ, {**env, "_TEST_ENV": repr(expected_env)}, clear=True): | ||
result = testdir.runpytest() | ||
|
||
def test_simple(example: pytest.Testdir) -> None: | ||
(example.tmpdir / "pytest.ini").write_text("[pytest]\nenv = MAGIC=alpha", encoding="utf-8") | ||
example.monkeypatch.setenv("_PATCH", "alpha") | ||
result = example.runpytest() | ||
result.assert_outcomes(passed=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters