Skip to content

Commit 8b71c96

Browse files
authored
Merge pull request #16 from dmarin-pixel/BCM-35781_git-helpers-avoid-writing-to-the-project-directory-during-test-runs
BCM-35781: GIT-HELPERS: Avoid writing to the project directory during test runs
2 parents a276838 + 5081d08 commit 8b71c96

6 files changed

Lines changed: 57 additions & 20 deletions

File tree

tests/conftest.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@
22
import pytest
33
from pathlib import Path
44

5-
FIXTURES_DIR = Path(__file__).parent / 'fixtures'
65
BIN_DIR = Path(__file__).parent.parent / 'bin'
76

87

8+
def pytest_addoption(parser):
9+
parser.addoption('--work-dir', default=None, help='Root temp directory for fixtures and test repos')
10+
11+
12+
@pytest.fixture(scope='session')
13+
def work_dir(request):
14+
d = request.config.getoption('--work-dir')
15+
if d is None:
16+
pytest.exit(
17+
'Missing --work-dir. Run the suite via tests/run_tests.sh, '
18+
'which prepares fixtures and passes --work-dir automatically.',
19+
returncode=1,
20+
)
21+
return Path(d)
22+
23+
24+
925
def cmd_info(result):
1026
"""Format a CompletedProcess into a readable diagnostic block for assertion messages."""
1127
cmd = ' '.join(str(a) for a in result.args)
@@ -31,16 +47,25 @@ def resolve_ref(repo_dir, ref):
3147
return git(repo_dir, 'rev-parse', ref).stdout.strip()
3248

3349

50+
# Multiple tests may use the same fixture name; a counter ensures each gets
51+
# its own subdirectory under work_dir/tests/ without collisions.
52+
_repo_counter = 0
53+
54+
3455
@pytest.fixture
35-
def make_repo(tmp_path):
56+
def make_repo(work_dir):
3657
"""Return a factory that creates a git repo from a .fi fixture file."""
58+
global _repo_counter
59+
_repo_counter += 1
60+
idx = _repo_counter
61+
3762
def _make(fixture_name):
38-
repo_dir = tmp_path / fixture_name
39-
repo_dir.mkdir()
63+
repo_dir = work_dir / 'tests' / f'{idx}_{fixture_name}'
64+
repo_dir.mkdir(parents=True)
4065
git(repo_dir, 'init')
4166
git(repo_dir, 'config', 'user.email', 'test@test')
4267
git(repo_dir, 'config', 'user.name', 'Test')
43-
fi_path = FIXTURES_DIR / f'{fixture_name}.fi'
68+
fi_path = work_dir / 'fixtures' / f'{fixture_name}.fi'
4469
with fi_path.open('rb') as f:
4570
subprocess.run(
4671
['git', 'fast-import', '--quiet'],

tests/fixtures/bc_cherry_pick_scenarios.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
set -e
1818

19-
REPO=$(mktemp -d)
20-
trap "rm -rf $REPO" EXIT
19+
WORK_DIR="${1:?Usage: $0 <work-dir>}"
20+
FIXTURES_OUT="$WORK_DIR/fixtures"
21+
22+
REPO=$(mktemp -d "$WORK_DIR/repo.XXXXXX")
2123

2224
git -C $REPO init -q
2325
git -C $REPO config user.email test@test
@@ -58,5 +60,5 @@ echo "patch" > $REPO/patch.txt
5860
git -C $REPO add patch.txt
5961
git -C $REPO commit -m "XXX-3: Patch" -q
6062

61-
git -C $REPO fast-export --all > "$(dirname "$0")/bc_cherry_pick_scenarios.fi"
63+
git -C $REPO fast-export --all > "$FIXTURES_OUT/bc_cherry_pick_scenarios.fi"
6264
echo "Written bc_cherry_pick_scenarios.fi"

tests/fixtures/mainline_not_first_parent.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
set -e
1919

20-
REPO=$(mktemp -d)
21-
trap "rm -rf $REPO" EXIT
20+
WORK_DIR="${1:?Usage: $0 <work-dir>}"
21+
FIXTURES_OUT="$WORK_DIR/fixtures"
22+
23+
REPO=$(mktemp -d "$WORK_DIR/repo.XXXXXX")
2224

2325
git -C $REPO init -q
2426
git -C $REPO config user.email test@test
@@ -50,5 +52,5 @@ git -C $REPO commit --allow-empty -m "XXX-2: Feature part 2" -q
5052
git -C $REPO checkout master -q
5153
git -C $REPO merge --no-ff feature -m "XXX-2: Feature done" -q
5254

53-
git -C $REPO fast-export --all > "$(dirname "$0")/mainline_not_first_parent.fi"
55+
git -C $REPO fast-export --all > "$FIXTURES_OUT/mainline_not_first_parent.fi"
5456
echo "Written mainline_not_first_parent.fi"

tests/fixtures/merge_differing_messages.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
set -e
2121

22-
REPO=$(mktemp -d)
23-
trap "rm -rf $REPO" EXIT
22+
WORK_DIR="${1:?Usage: $0 <work-dir>}"
23+
FIXTURES_OUT="$WORK_DIR/fixtures"
24+
25+
REPO=$(mktemp -d "$WORK_DIR/repo.XXXXXX")
2426

2527
git -C $REPO init -q
2628
git -C $REPO config user.email test@test
@@ -66,5 +68,5 @@ git -C $REPO commit --allow-empty -m "XXX-40: New API v3" -q
6668
git -C $REPO checkout master -q
6769
git -C $REPO merge --no-ff feature3 -m "XXX-40: New API v3" -q
6870

69-
git -C $REPO fast-export --all > "$(dirname "$0")/merge_differing_messages.fi"
71+
git -C $REPO fast-export --all > "$FIXTURES_OUT/merge_differing_messages.fi"
7072
echo "Written merge_differing_messages.fi"

tests/fixtures/octopus_merge.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
set -e
1818

19-
REPO=$(mktemp -d)
20-
trap "rm -rf $REPO" EXIT
19+
WORK_DIR="${1:?Usage: $0 <work-dir>}"
20+
FIXTURES_OUT="$WORK_DIR/fixtures"
21+
22+
REPO=$(mktemp -d "$WORK_DIR/repo.XXXXXX")
2123

2224
git -C $REPO init -q
2325
git -C $REPO config user.email test@test
@@ -59,5 +61,5 @@ git -C $REPO commit -m "XXX-3: Feature2 commit" -q
5961
git -C $REPO checkout master -q
6062
git -C $REPO merge --no-ff feature1 feature2 -m "XXX-2/3: Merge feature1 and feature2" -q
6163

62-
git -C $REPO fast-export --all > "$(dirname "$0")/octopus_merge.fi"
64+
git -C $REPO fast-export --all > "$FIXTURES_OUT/octopus_merge.fi"
6365
echo "Written octopus_merge.fi"

tests/run_tests.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#!/bin/bash
2-
# Build all fixture repos and run the test suite.
2+
# Build all fixture repos into a temp directory and run the test suite.
33
# Usage: tests/run_tests.sh [pytest options...]
44
set -e
55
cd "$(dirname "$0")"
66

7+
WORK_DIR=$(mktemp -d)
8+
trap "rm -rf $WORK_DIR" EXIT
9+
mkdir "$WORK_DIR/fixtures"
10+
711
for script in fixtures/*.sh; do
812
echo "Building fixture: $script"
9-
bash "$script"
13+
bash "$script" "$WORK_DIR"
1014
done
1115

12-
exec python3 -m pytest . "$@"
16+
PYTHONDONTWRITEBYTECODE=1 exec python3 -m pytest . --work-dir="$WORK_DIR" -o "cache_dir=$WORK_DIR/.pytest_cache" "$@"

0 commit comments

Comments
 (0)