diff options
-rw-r--r-- | tests/conftest.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index e1a2292a..3c312015 100644 --- a/tests/conftest.py +++ b/tests/conftest.py | |||
@@ -14,8 +14,11 @@ | |||
14 | 14 | ||
15 | """Common fixtures for pytests.""" | 15 | """Common fixtures for pytests.""" |
16 | 16 | ||
17 | import pathlib | ||
18 | |||
17 | import pytest | 19 | import pytest |
18 | 20 | ||
21 | import platform_utils | ||
19 | import repo_trace | 22 | import repo_trace |
20 | 23 | ||
21 | 24 | ||
@@ -23,3 +26,49 @@ import repo_trace | |||
23 | def disable_repo_trace(tmp_path): | 26 | def disable_repo_trace(tmp_path): |
24 | """Set an environment marker to relax certain strict checks for test code.""" # noqa: E501 | 27 | """Set an environment marker to relax certain strict checks for test code.""" # noqa: E501 |
25 | repo_trace._TRACE_FILE = str(tmp_path / "TRACE_FILE_from_test") | 28 | repo_trace._TRACE_FILE = str(tmp_path / "TRACE_FILE_from_test") |
29 | |||
30 | |||
31 | # adapted from pytest-home 0.5.1 | ||
32 | def _set_home(monkeypatch, path: pathlib.Path): | ||
33 | """ | ||
34 | Set the home dir using a pytest monkeypatch context. | ||
35 | """ | ||
36 | win = platform_utils.isWindows() | ||
37 | vars = ["HOME"] + win * ["USERPROFILE"] | ||
38 | for var in vars: | ||
39 | monkeypatch.setenv(var, str(path)) | ||
40 | return path | ||
41 | |||
42 | |||
43 | # copied from | ||
44 | # https://github.com/pytest-dev/pytest/issues/363#issuecomment-1335631998 | ||
45 | @pytest.fixture(scope="session") | ||
46 | def monkeysession(): | ||
47 | with pytest.MonkeyPatch.context() as mp: | ||
48 | yield mp | ||
49 | |||
50 | |||
51 | @pytest.fixture(autouse=True, scope="session") | ||
52 | def session_tmp_home_dir(tmp_path_factory, monkeysession): | ||
53 | """Set HOME to a temporary directory, avoiding user's .gitconfig. | ||
54 | |||
55 | b/302797407 | ||
56 | |||
57 | Set home at session scope to take effect prior to | ||
58 | ``test_wrapper.GitCheckoutTestCase.setUpClass``. | ||
59 | """ | ||
60 | return _set_home(monkeysession, tmp_path_factory.mktemp("home")) | ||
61 | |||
62 | |||
63 | # adapted from pytest-home 0.5.1 | ||
64 | @pytest.fixture(autouse=True) | ||
65 | def tmp_home_dir(monkeypatch, tmp_path_factory): | ||
66 | """Set HOME to a temporary directory. | ||
67 | |||
68 | Ensures that state doesn't accumulate in $HOME across tests. | ||
69 | |||
70 | Note that in conjunction with session_tmp_homedir, the HOME | ||
71 | dir is patched twice, once at session scope, and then again at | ||
72 | the function scope. | ||
73 | """ | ||
74 | return _set_home(monkeypatch, tmp_path_factory.mktemp("home")) | ||