|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import sys |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from fetchcode.vcs.pip._internal.utils.temp_dir import global_tempdir_manager |
| 8 | +from lib import VCSTestEnvironment |
| 9 | +from lib.path import Path |
| 10 | +from lib.compat import nullcontext |
| 11 | + |
| 12 | + |
| 13 | +def pytest_addoption(parser): |
| 14 | + parser.addoption( |
| 15 | + "--keep-tmpdir", |
| 16 | + action="store_true", |
| 17 | + default=False, |
| 18 | + help="keep temporary test directories", |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope="session") |
| 23 | +def tmpdir_factory(request, tmpdir_factory): |
| 24 | + """Modified `tmpdir_factory` session fixture |
| 25 | + that will automatically cleanup after itself. |
| 26 | + """ |
| 27 | + yield tmpdir_factory |
| 28 | + if not request.config.getoption("--keep-tmpdir"): |
| 29 | + shutil.rmtree( |
| 30 | + tmpdir_factory.getbasetemp(), |
| 31 | + ignore_errors=True, |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def tmpdir(request, tmpdir): |
| 37 | + """ |
| 38 | + Return a temporary directory path object which is unique to each test |
| 39 | + function invocation, created as a sub directory of the base temporary |
| 40 | + directory. The returned object is a ``tests.lib.path.Path`` object. |
| 41 | +
|
| 42 | + This uses the built-in tmpdir fixture from pytest itself but modified |
| 43 | + to return our typical path object instead of py.path.local as well as |
| 44 | + deleting the temporary directories at the end of each test case. |
| 45 | + """ |
| 46 | + assert tmpdir.isdir() |
| 47 | + yield Path(str(tmpdir)) |
| 48 | + # Clear out the temporary directory after the test has finished using it. |
| 49 | + # This should prevent us from needing a multiple gigabyte temporary |
| 50 | + # directory while running the tests. |
| 51 | + if not request.config.getoption("--keep-tmpdir"): |
| 52 | + tmpdir.remove(ignore_errors=True) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture(autouse=True) |
| 56 | +def isolate(tmpdir, monkeypatch): |
| 57 | + """ |
| 58 | + Isolate our tests so that things like global configuration files and the |
| 59 | + like do not affect our test results. |
| 60 | +
|
| 61 | + We use an autouse function scoped fixture because we want to ensure that |
| 62 | + every test has it's own isolated home directory. |
| 63 | + """ |
| 64 | + |
| 65 | + # TODO: Figure out how to isolate from *system* level configuration files |
| 66 | + # as well as user level configuration files. |
| 67 | + |
| 68 | + # Create a directory to use as our home location. |
| 69 | + home_dir = os.path.join(str(tmpdir), "home") |
| 70 | + os.makedirs(home_dir) |
| 71 | + |
| 72 | + # Create a directory to use as a fake root |
| 73 | + fake_root = os.path.join(str(tmpdir), "fake-root") |
| 74 | + os.makedirs(fake_root) |
| 75 | + |
| 76 | + if sys.platform == "win32": |
| 77 | + # Note: this will only take effect in subprocesses... |
| 78 | + home_drive, home_path = os.path.splitdrive(home_dir) |
| 79 | + monkeypatch.setenv("USERPROFILE", home_dir) |
| 80 | + monkeypatch.setenv("HOMEDRIVE", home_drive) |
| 81 | + monkeypatch.setenv("HOMEPATH", home_path) |
| 82 | + for env_var, sub_path in ( |
| 83 | + ("APPDATA", "AppData/Roaming"), |
| 84 | + ("LOCALAPPDATA", "AppData/Local"), |
| 85 | + ): |
| 86 | + path = os.path.join(home_dir, *sub_path.split("/")) |
| 87 | + monkeypatch.setenv(env_var, path) |
| 88 | + os.makedirs(path) |
| 89 | + else: |
| 90 | + # Set our home directory to our temporary directory, this should force |
| 91 | + # all of our relative configuration files to be read from here instead |
| 92 | + # of the user's actual $HOME directory. |
| 93 | + monkeypatch.setenv("HOME", home_dir) |
| 94 | + |
| 95 | + # Configure git, because without an author name/email git will complain |
| 96 | + # and cause test failures. |
| 97 | + monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 98 | + monkeypatch.setenv("GIT_AUTHOR_NAME", "pip") |
| 99 | + monkeypatch.setenv("GIT_AUTHOR_EMAIL", "distutils-sig@python.org") |
| 100 | + |
| 101 | + # FIXME: Windows... |
| 102 | + #os.makedirs(os.path.join(home_dir, ".gitconfig", "git")) |
| 103 | + with open(os.path.join(home_dir, ".gitconfig"), "wb") as fp: |
| 104 | + fp.write(b"[user]\n\tname = pip\n\temail = distutils-sig@python.org\n") |
| 105 | + |
| 106 | + |
| 107 | +@pytest.fixture(autouse=True) |
| 108 | +def scoped_global_tempdir_manager(request): |
| 109 | + """Make unit tests with globally-managed tempdirs easier |
| 110 | +
|
| 111 | + Each test function gets its own individual scope for globally-managed |
| 112 | + temporary directories in the application. |
| 113 | + """ |
| 114 | + if "no_auto_tempdir_manager" in request.keywords: |
| 115 | + ctx = nullcontext |
| 116 | + else: |
| 117 | + ctx = global_tempdir_manager |
| 118 | + |
| 119 | + with ctx(): |
| 120 | + yield |
| 121 | + |
| 122 | + |
| 123 | +@pytest.fixture(scope="session") |
| 124 | +def script_factory(): |
| 125 | + def factory(tmpdir): |
| 126 | + return VCSTestEnvironment( |
| 127 | + tmpdir, |
| 128 | + ignore_hidden=False, |
| 129 | + start_clear=False, |
| 130 | + capture_temp=True, |
| 131 | + assert_no_temp=True) |
| 132 | + return factory |
| 133 | + |
| 134 | + |
| 135 | +@pytest.fixture |
| 136 | +#def script(tmpdir, virtualenv, script_factory): |
| 137 | +def script(tmpdir, script_factory): |
| 138 | + """ |
| 139 | + Return a PipTestEnvironment which is unique to each test function and |
| 140 | + will execute all commands inside of the unique virtual environment for this |
| 141 | + test function. The returned object is a |
| 142 | + ``tests.lib.PipTestEnvironment``. |
| 143 | + """ |
| 144 | + #return script_factory(tmpdir.joinpath("workspace"), virtualenv) |
| 145 | + return script_factory(tmpdir.joinpath("workspace")) |
0 commit comments