162 lines
5.1 KiB
Python
162 lines
5.1 KiB
Python
"""Shared fixtures for the functional test suite."""
|
|
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from subprocess import CompletedProcess
|
|
|
|
import pytest
|
|
|
|
# A git-annex stub that logs every invocation and emulates ``init`` so
|
|
# that commands running ``git annex init`` inside a fresh clone (via
|
|
# datalad_core) work without a git-annex installation.
|
|
GIT_ANNEX_STUB = """\
|
|
#!/bin/sh
|
|
printf 'git-annex %s\\n' "$*" >> "${ANNEX_LOG:-/dev/null}"
|
|
if [ "$1" = "init" ]; then git config annex.uuid "${ANNEX_STUB_UUID:-STUB-UUID}"; fi
|
|
exit 0
|
|
"""
|
|
|
|
|
|
def entry_point(name: str) -> str:
|
|
"""Resolve an installed entry point, skipping the test if absent."""
|
|
entry = shutil.which(name)
|
|
if entry is None:
|
|
pytest.skip(f'{name} entry point is not installed')
|
|
return entry
|
|
|
|
|
|
@dataclass
|
|
class GitEnv:
|
|
"""An isolated environment for running git and installed entry points."""
|
|
|
|
work: Path
|
|
home: Path
|
|
env: dict[str, str]
|
|
|
|
def git(
|
|
self,
|
|
*args: str,
|
|
cwd: Path | None = None,
|
|
check: bool = True,
|
|
inputs: str | None = None,
|
|
) -> CompletedProcess[str]:
|
|
"""Run a git command in the isolated environment."""
|
|
return subprocess.run(
|
|
['git', *args], # noqa: S607 - PATH is deliberately isolated
|
|
cwd=cwd or self.work,
|
|
env=self.env,
|
|
check=check,
|
|
capture_output=True,
|
|
text=True,
|
|
input=inputs,
|
|
)
|
|
|
|
def git_success(self, *args: str, cwd: Path | None = None) -> bool:
|
|
"""Run a git command and report success without raising."""
|
|
return self.git(*args, cwd=cwd, check=False).returncode == 0
|
|
|
|
def git_oneline(
|
|
self,
|
|
*args: str,
|
|
cwd: Path | None = None,
|
|
inputs: str | None = None,
|
|
) -> str:
|
|
"""Run a git command and return its single line of output."""
|
|
return self.git(*args, cwd=cwd, inputs=inputs).stdout.strip()
|
|
|
|
def add_bin(self, name: str, script: str, **extra: str) -> None:
|
|
"""Place a stub binary on PATH, optionally extending the environment."""
|
|
bin_dir = self.work / 'bin'
|
|
bin_dir.mkdir(exist_ok=True)
|
|
stub = bin_dir / name
|
|
stub.write_text(script)
|
|
stub.chmod(0o755)
|
|
self.env['PATH'] = f'{bin_dir}{os.pathsep}{self.env["PATH"]}'
|
|
self.env.update(extra)
|
|
|
|
def run(
|
|
self,
|
|
entry: str,
|
|
*args: str,
|
|
stdin: str = '',
|
|
cwd: Path | None = None,
|
|
) -> CompletedProcess[str]:
|
|
"""Run an installed entry point in the isolated environment."""
|
|
return subprocess.run(
|
|
[entry, *args],
|
|
cwd=cwd or self.work,
|
|
env=self.env,
|
|
input=stdin,
|
|
check=False,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
|
|
|
|
# the project root, i.e. where coverage combines its data files
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
@pytest.fixture
|
|
def git_env(tmp_path: Path) -> GitEnv:
|
|
"""An isolated git environment rooted at ``tmp_path``."""
|
|
work = tmp_path
|
|
home = work / 'home'
|
|
home.mkdir()
|
|
env = dict(os.environ)
|
|
# when coverage is running (hatch test --cover), child processes
|
|
# auto-start their own coverage via COVERAGE_PROCESS_START; redirect
|
|
# their data files to the project root (the combine directory), so
|
|
# subprocess lines are measured and nothing lands in the repository
|
|
if 'COVERAGE_PROCESS_START' in env or 'COVERAGE_PROCESS_CONFIG' in env:
|
|
env['COVERAGE_ROOT'] = str(PROJECT_ROOT)
|
|
env['HOME'] = str(home)
|
|
env['GIT_CONFIG_NOSYSTEM'] = '1'
|
|
env['GIT_TERMINAL_PROMPT'] = '0'
|
|
env['GIT_PAGER'] = 'cat'
|
|
return GitEnv(work=work, home=home, env=env)
|
|
|
|
|
|
@dataclass
|
|
class AnnexRepos:
|
|
"""A seeded source repository and a bare annex target repository."""
|
|
|
|
source: Path
|
|
target: Path
|
|
main: str
|
|
feature: str
|
|
|
|
|
|
@pytest.fixture
|
|
def annex_repos(git_env: GitEnv) -> AnnexRepos:
|
|
"""Seed a repository with two branches and a tag, and clone it bare."""
|
|
work = git_env.work
|
|
seed = work / 'seed'
|
|
git_env.git('init', '-q', '-b', 'main', str(seed))
|
|
git_env.git('config', 'user.email', 't@example.com', cwd=seed)
|
|
git_env.git('config', 'user.name', 'tester', cwd=seed)
|
|
(seed / 'a.txt').write_text('hello\n')
|
|
git_env.git('add', 'a.txt', cwd=seed)
|
|
git_env.git('commit', '-qm', 'initial', cwd=seed)
|
|
git_env.git('tag', 'v1.0', cwd=seed)
|
|
git_env.git('checkout', '-qb', 'feature', cwd=seed)
|
|
(seed / 'b.txt').write_text('feat\n')
|
|
git_env.git('add', 'b.txt', cwd=seed)
|
|
git_env.git('commit', '-qm', 'feature', cwd=seed)
|
|
|
|
source = work / 'source.git'
|
|
git_env.git('clone', '-q', '--bare', str(seed), str(source))
|
|
|
|
target = work / 'target.git'
|
|
git_env.git('init', '-q', '--bare', '-b', 'main', str(target))
|
|
git_env.git('config', 'annex.uuid', 'TEST-TARGET-UUID', cwd=target)
|
|
|
|
return AnnexRepos(
|
|
source=source,
|
|
target=target,
|
|
main=git_env.git_oneline('rev-parse', 'refs/heads/main', cwd=source),
|
|
feature=git_env.git_oneline('rev-parse', 'refs/heads/feature', cwd=source),
|
|
)
|