diff options
| author | Jason R. Coombs <jaraco@google.com> | 2023-10-02 13:58:54 -0400 |
|---|---|---|
| committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-10-17 15:15:55 +0000 |
| commit | 024df06ec15d7304fbb5f9a2b1aa44f2af9daf4c (patch) | |
| tree | 731c6f2176308623193f05e58c268c783efca2af /tests | |
| parent | 45809e51ca6f4dfb84c9400e5dffef6daa2d667d (diff) | |
| download | git-repo-024df06ec15d7304fbb5f9a2b1aa44f2af9daf4c.tar.gz | |
tests: Set HOME to a temporary directory when running tests.v2.38
When running the tests in my environment, tests that derived from `test_wrapper.GitCheckoutTestCase` would fail on commit or tag due to incomplete or incorrect gpg config. Ideally, the tests should not be dependent on the user's git config. This change ensures $HOME (or Windows equivalent) is replaced for the session.
Bug: 302797407
Change-Id: Ib42b712dd7b6602fee6e18329a8c6d52fb9458b9
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/388235
Tested-by: Jason R. Coombs <jaraco@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
Commit-Queue: Jason R. Coombs <jaraco@google.com>
Diffstat (limited to 'tests')
| -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")) | ||
