|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import pathlib |
| 5 | +import subprocess |
| 6 | +import uuid |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def in_integration_env(integration_env, integration_dir): |
| 13 | + curdir = os.getcwd() |
| 14 | + os.chdir(integration_dir) |
| 15 | + yield integration_dir |
| 16 | + os.chdir(curdir) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def integration_dir(tmp_path: pathlib.Path): |
| 21 | + test_dir = tmp_path / "integration_test" |
| 22 | + test_dir.mkdir() |
| 23 | + return test_dir |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def file_path(integration_dir): |
| 28 | + return integration_dir / "foo.py" |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def write_file(file_path): |
| 33 | + def _(*variables): |
| 34 | + content = "import os" |
| 35 | + for i, var in enumerate(variables): |
| 36 | + content += f"""\nif os.environ.get("{var}"):\n {i}\n""" |
| 37 | + file_path.write_text(content, encoding="utf8") |
| 38 | + |
| 39 | + return _ |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def run_coverage(file_path, integration_dir): |
| 44 | + def _(*variables): |
| 45 | + subprocess.check_call( |
| 46 | + ["coverage", "run", "--parallel", file_path.name], |
| 47 | + cwd=integration_dir, |
| 48 | + env=os.environ | dict.fromkeys(variables, "1"), |
| 49 | + ) |
| 50 | + |
| 51 | + return _ |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def commit(integration_dir): |
| 56 | + def _(): |
| 57 | + subprocess.check_call( |
| 58 | + ["git", "add", "."], |
| 59 | + cwd=integration_dir, |
| 60 | + ) |
| 61 | + subprocess.check_call( |
| 62 | + ["git", "commit", "-m", str(uuid.uuid4())], |
| 63 | + cwd=integration_dir, |
| 64 | + env={ |
| 65 | + "GIT_AUTHOR_NAME": "foo", |
| 66 | + "GIT_AUTHOR_EMAIL": "foo", |
| 67 | + "GIT_COMMITTER_NAME": "foo", |
| 68 | + "GIT_COMMITTER_EMAIL": "foo", |
| 69 | + "GIT_CONFIG_GLOBAL": "/dev/null", |
| 70 | + "GIT_CONFIG_SYSTEM": "/dev/null", |
| 71 | + }, |
| 72 | + ) |
| 73 | + |
| 74 | + return _ |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture |
| 78 | +def integration_env(integration_dir, write_file, run_coverage, commit, request): |
| 79 | + subprocess.check_call(["git", "init", "-b", "main"], cwd=integration_dir) |
| 80 | + # diff coverage reads the "origin/{...}" branch so we simulate an origin remote |
| 81 | + subprocess.check_call(["git", "remote", "add", "origin", "."], cwd=integration_dir) |
| 82 | + write_file("A", "B") |
| 83 | + commit() |
| 84 | + |
| 85 | + add_branch_mark = request.node.get_closest_marker("add_branches") |
| 86 | + for additional_branch in add_branch_mark.args if add_branch_mark else []: |
| 87 | + subprocess.check_call( |
| 88 | + ["git", "switch", "-c", additional_branch], |
| 89 | + cwd=integration_dir, |
| 90 | + ) |
| 91 | + |
| 92 | + subprocess.check_call( |
| 93 | + ["git", "switch", "-c", "branch"], |
| 94 | + cwd=integration_dir, |
| 95 | + ) |
| 96 | + |
| 97 | + write_file("A", "B", "C", "D") |
| 98 | + commit() |
| 99 | + |
| 100 | + run_coverage("A", "C") |
| 101 | + subprocess.check_call(["git", "fetch", "origin"], cwd=integration_dir) |
0 commit comments