|
| 1 | +from typing import Union, Any, Iterator |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import shutil |
| 8 | +import pathlib |
| 9 | +from .settings import ( |
| 10 | + TEST_SAMPLES_PATH, |
| 11 | + REPO_FIXTURE_PATH, |
| 12 | + REPO_SAFE_PREFIX, |
| 13 | + REPO_GOOD_REF, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(scope="module", autouse=True) |
| 18 | +def repo_fixture( |
| 19 | + repo_path: Union[str, os.PathLike] = REPO_FIXTURE_PATH, |
| 20 | + samples_path: Union[str, os.PathLike] = TEST_SAMPLES_PATH, |
| 21 | + safe_prefix: str = REPO_SAFE_PREFIX, |
| 22 | + good_ref: str = REPO_GOOD_REF, |
| 23 | +) -> Iterator[Any]: |
| 24 | + """Create a temporary git repository to test git-based filesystems. |
| 25 | +
|
| 26 | + Args: |
| 27 | + repo_path (str): The path at which to create the temporary repo. Defaults to REPO_FIXTURE_PATH. It is safest |
| 28 | + to create the repo outside your software project's directory tree so it does not interfere with real repos. |
| 29 | + samples_path (str): The path to the sample files, which will be added to the repo. Defaults to TEST_SAMPLES_PATH. |
| 30 | + safe_prefix (str): Helps prevent mixups between the test repo and real repos. Defaults to REPO_SAFE_PREFIX. |
| 31 | + good_ref (str): The git ref for the unmodified sample files. Later commits intentionally break the sample |
| 32 | + files so that tests will fail if the system under test doesn't correctly handle refs. |
| 33 | + Defaults to REPO_SAFE_REF. |
| 34 | +
|
| 35 | + Yields: |
| 36 | + Tuple[str, str]: A tuple containing the repo_path and good_ref. |
| 37 | +
|
| 38 | + """ |
| 39 | + repo_path = pathlib.Path(repo_path) |
| 40 | + samples_path = pathlib.Path(samples_path) |
| 41 | + |
| 42 | + try: |
| 43 | + try: |
| 44 | + os.mkdir(repo_path) |
| 45 | + except FileExistsError: |
| 46 | + raise FileExistsError( |
| 47 | + f"Directory `{repo_path.absolute()}` already exists." |
| 48 | + "It should have been removed by the previous test run." |
| 49 | + ) |
| 50 | + |
| 51 | + # NOTE: `git init -b` option requires git 2.28 or later. |
| 52 | + subprocess.call(f"git init -b {safe_prefix}master", shell=True, cwd=repo_path) |
| 53 | + subprocess.call( |
| 54 | + "git config user.email '[email protected]'", shell=True, cwd=repo_path |
| 55 | + ) |
| 56 | + subprocess.call("git config user.name 'Your Name'", shell=True, cwd=repo_path) |
| 57 | + shutil.copytree( |
| 58 | + samples_path, repo_path / f"{safe_prefix}samples", dirs_exist_ok=False |
| 59 | + ) |
| 60 | + subprocess.call("git add --all", shell=True, cwd=repo_path) |
| 61 | + subprocess.call( |
| 62 | + "git commit -m 'add standard sample files for tests'", |
| 63 | + shell=True, |
| 64 | + cwd=repo_path, |
| 65 | + ) |
| 66 | + subprocess.call( |
| 67 | + f"git tag -a {good_ref} -m 'The sample test files with no modifications'", |
| 68 | + shell=True, |
| 69 | + cwd=repo_path, |
| 70 | + ) |
| 71 | + subprocess.call( |
| 72 | + "git mv sample.txt sample_renamed.txt", |
| 73 | + shell=True, |
| 74 | + cwd=repo_path / f"{safe_prefix}samples", |
| 75 | + ) |
| 76 | + subprocess.call( |
| 77 | + "git commit -m 'rename samples.txt to make primitive test fail if at HEAD'", |
| 78 | + shell=True, |
| 79 | + cwd=repo_path, |
| 80 | + ) |
| 81 | + yield repo_path, good_ref |
| 82 | + finally: |
| 83 | + shutil.rmtree(repo_path) |
0 commit comments