Skip to content

Commit 8c2612d

Browse files
author
Adam Dyess
committed
Allow for pytest to be run with overridden --basetemp argument
1 parent eba4d48 commit 8c2612d

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

pytest_operator/plugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ def pytest_configure(config: Config):
172172
config.addinivalue_line("markers", "abort_on_fail")
173173
config.addinivalue_line("markers", "skip_if_deployed")
174174

175-
if tox_dir := os.environ.get("TOX_ENV_DIR"):
176-
config.option.basetemp = Path(tox_dir) / "tmp/pytest"
175+
if config.option.basetemp is None:
176+
if tox_dir := os.environ.get("TOX_ENV_DIR"):
177+
config.option.basetemp = Path(tox_dir) / "tmp/pytest"
177178
log.info("Using basetemp: %s", config.option.basetemp)
178179

179180

tests/unit/test_pytest_operator.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@
1414

1515
log = logging.getLogger(__name__)
1616

17-
ENV = {_: os.environ.get(_) for _ in ["HOME", "TOX_ENV_DIR"]}
17+
USER_HOME = os.getenv("HOME")
18+
TOX_ENV_DIR = os.getenv("TOX_ENV_DIR")
1819

1920

2021
@patch.object(plugin, "check_deps", Mock())
2122
@patch.object(plugin.OpsTest, "_setup_model", AsyncMock())
2223
@patch.object(plugin.OpsTest, "_cleanup_models", AsyncMock())
23-
def test_tmp_path_with_tox(pytester):
24+
def test_tmp_path_with_tox(pytester, monkeypatch):
25+
monkeypatch.setenv("TOX_ENV_DIR", TOX_ENV_DIR)
26+
monkeypatch.setenv("HOME", USER_HOME)
2427
pytester.makepyfile(
2528
f"""
2629
import os
2730
from pathlib import Path
2831
import pytest
2932
30-
os.environ.update(**{ENV})
31-
3233
def test_with_tox(ops_test):
33-
tox_env_dir = "{ENV["TOX_ENV_DIR"]}"
34+
tox_env_dir = "{TOX_ENV_DIR}"
3435
passed_tox_env_dir = os.environ.get("TOX_ENV_DIR")
3536
assert passed_tox_env_dir == tox_env_dir
3637
@@ -39,25 +40,26 @@ def test_with_tox(ops_test):
3940
assert expected_base == Path(common), f"{{ops_test.tmp_path}} didn't match {{expected_base}}"
4041
"""
4142
)
42-
result = pytester.runpytest()
43+
result = pytester.runpytest_inprocess("-s")
4344
result.assert_outcomes(passed=1)
4445

4546

4647
@patch.object(plugin, "check_deps", Mock())
4748
@patch.object(plugin.OpsTest, "_setup_model", AsyncMock())
4849
@patch.object(plugin.OpsTest, "_cleanup_models", AsyncMock())
49-
def test_tmp_path_without_tox(pytester):
50+
def test_tmp_path_without_tox(pytester, monkeypatch):
51+
monkeypatch.setenv("TOX_ENV_DIR", None)
52+
monkeypatch.setenv("HOME", USER_HOME)
5053
pytester.makepyfile(
5154
f"""
5255
import os
5356
from pathlib import Path
5457
import pytest
5558
56-
os.environ.update(**{ENV})
5759
os.environ.pop("TOX_ENV_DIR", None)
5860
5961
def test_without_tox(request, ops_test):
60-
unexpected_base = Path("{ENV["TOX_ENV_DIR"]}") / "tmp" / "pytest"
62+
unexpected_base = Path("{TOX_ENV_DIR}") / "tmp" / "pytest"
6163
common = os.path.commonpath([ops_test.tmp_path, unexpected_base])
6264
assert unexpected_base != Path(common)
6365
@@ -66,7 +68,7 @@ def test_without_tox(request, ops_test):
6668
assert expected_base == Path(common)
6769
"""
6870
)
69-
result = pytester.runpytest("--basetemp=/tmp/pytest")
71+
result = pytester.runpytest_inprocess("--basetemp=/tmp/pytest")
7072
result.assert_outcomes(passed=1)
7173

7274

0 commit comments

Comments
 (0)