| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
import os |
| 6 |
import shutil |
| 7 |
import subprocess |
| 8 |
from pathlib import Path |
| 9 |
from typing import List |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
# Execute the first element in each list of steps within a `repo` directory, |
| 14 |
# then copy the whole directory to a `remoterepo`, and finally execute the |
| 15 |
# second element on just `repo`. |
| 16 |
SETUP = { |
| 17 |
"hg": [ |
| 18 |
""" |
| 19 |
echo "foo" > foo |
| 20 |
echo "bar" > bar |
| 21 |
hg init |
| 22 |
hg add * |
| 23 |
hg commit -m "Initial commit" |
| 24 |
hg phase --public . |
| 25 |
""", |
| 26 |
""" |
| 27 |
echo [paths] > .hg/hgrc |
| 28 |
echo "default = ../remoterepo" >> .hg/hgrc |
| 29 |
""", |
| 30 |
], |
| 31 |
"git": [ |
| 32 |
""" |
| 33 |
echo "foo" > foo |
| 34 |
echo "bar" > bar |
| 35 |
git init |
| 36 |
git config user.name "Testing McTesterson" |
| 37 |
git config user.email "<[email protected]>" |
| 38 |
git add * |
| 39 |
git commit -am "Initial commit" |
| 40 |
""", |
| 41 |
""" |
| 42 |
git remote add upstream ../remoterepo |
| 43 |
git fetch upstream |
| 44 |
git branch -u upstream/master |
| 45 |
""", |
| 46 |
], |
| 47 |
"jj": [ |
| 48 |
""" |
| 49 |
echo "foo" > foo |
| 50 |
echo "bar" > bar |
| 51 |
git init |
| 52 |
git config user.name "Testing McTesterson" |
| 53 |
git config user.email "<[email protected]>" |
| 54 |
git add * |
| 55 |
git commit -am "Initial commit" |
| 56 |
""", |
| 57 |
""" |
| 58 |
# Pass in user name/email via env vars because the initial commit |
| 59 |
# will use them before we have a chance to configure them. |
| 60 |
JJ_USER="Testing McTesterson" JJ_EMAIL="[email protected]" jj git init --colocate |
| 61 |
jj config set --repo user.name "Testing McTesterson" |
| 62 |
jj config set --repo user.email "[email protected]" |
| 63 |
jj git remote add upstream ../remoterepo |
| 64 |
jj git fetch --remote upstream |
| 65 |
jj bookmark track master@upstream |
| 66 |
""", |
| 67 |
], |
| 68 |
"src": [ |
| 69 |
""" |
| 70 |
echo "foo" > foo |
| 71 |
echo "bar" > bar |
| 72 |
mkdir config |
| 73 |
echo 1.0 > config/milestone.txt |
| 74 |
""", |
| 75 |
"", |
| 76 |
], |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
class RepoTestFixture: |
| 81 |
def __init__(self, repo_dir: Path, vcs: str, steps: List[str]): |
| 82 |
self.dir = repo_dir |
| 83 |
self.vcs = vcs |
| 84 |
|
| 85 |
# This creates a step iterator. Each time execute_next_step() |
| 86 |
# is called the next set of instructions will be executed. |
| 87 |
self.steps = (shell(cmd, self.dir) for cmd in steps) |
| 88 |
|
| 89 |
def execute_next_step(self): |
| 90 |
next(self.steps) |
| 91 |
|
| 92 |
|
| 93 |
def shell(cmd, working_dir): |
| 94 |
for step in cmd.split(os.linesep): |
| 95 |
subprocess.check_call(step, shell=True, cwd=working_dir) |
| 96 |
|
| 97 |
|
| 98 |
@pytest.fixture(params=["git", "hg", "jj", "src"]) |
| 99 |
def repo(tmpdir, request): |
| 100 |
if request.param == "jj": |
| 101 |
if os.getenv("MOZ_AVOID_JJ_VCS") not in (None, "0", ""): |
| 102 |
pytest.skip("jj support disabled") |
| 103 |
try: |
| 104 |
subprocess.call(["jj", "--version"], stdout=subprocess.DEVNULL) |
| 105 |
except OSError: |
| 106 |
pytest.skip("jj unavailable") |
| 107 |
|
| 108 |
tmpdir = Path(tmpdir) |
| 109 |
vcs = request.param |
| 110 |
steps = SETUP[vcs] |
| 111 |
|
| 112 |
if hasattr(request.module, "STEPS"): |
| 113 |
if vcs == "src" and vcs not in request.module.STEPS: |
| 114 |
# Special-case SourceRepository: most tests do not handle this case, |
| 115 |
# so allow it to be skipped if STEPS is defined but not for src. |
| 116 |
# (Tests without STEPS will need to skip manually.) |
| 117 |
pytest.skip("not applicable for src repo") |
| 118 |
steps.extend(request.module.STEPS[vcs]) |
| 119 |
|
| 120 |
repo_dir = (tmpdir / "repo").resolve() |
| 121 |
(tmpdir / "repo").mkdir() |
| 122 |
|
| 123 |
repo_test_fixture = RepoTestFixture(repo_dir, vcs, steps) |
| 124 |
|
| 125 |
repo_test_fixture.execute_next_step() |
| 126 |
|
| 127 |
shutil.copytree(str(repo_dir), str(tmpdir / "remoterepo")) |
| 128 |
|
| 129 |
repo_test_fixture.execute_next_step() |
| 130 |
|
| 131 |
yield repo_test_fixture |