-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
69 lines (51 loc) · 2.5 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import shutil
import tempfile
from collections.abc import Generator
from contextlib import contextmanager
from pathlib import Path
from corallium.log import get_logger
from pytestshellutils.shell import Subprocess
from pytestshellutils.utils.processes import ProcessResult
from .configuration import TEST_DATA_DIR, TEST_DIR
logger = get_logger()
CTT_CMD = ['poetry', 'run', 'ctt']
DEMO_DIR = TEST_DATA_DIR / 'copier_demo'
NO_ANSWER_FILE_DIR = TEST_DATA_DIR / 'no_answers_file_demo'
class ExpectedError(Exception):
"""Test-only exception."""
def _log_shell(message: str, ret: ProcessResult, *args, **kwargs) -> None:
"""Cleanup the shell output for printing."""
logger.text(message, stdout=f'`{ret.stdout.strip()}`', stderr=f'`{ret.stderr.strip()}`', args=args, _kwargs=kwargs)
def run_ctt(shell: Subprocess, cwd: Path, args: list[str] | None = None) -> ProcessResult:
"""Run `ctt` in the specified directory."""
ret = shell.run(*CTT_CMD, *(args or []), cwd=cwd)
_log_shell('ran ctt', ret)
return ret
def run_check(shell: Subprocess, *args, **kwargs) -> ProcessResult:
"""Check that the shell process completed with exit code 0."""
ret = shell.run(*args, **kwargs)
_log_shell('run_check', ret, args, kwargs=kwargs)
if ret.returncode != 0:
msg = f'Failed to run {args} with {kwargs}'
raise RuntimeError(msg)
return ret
def add_commit(shell: Subprocess, cwd: Path) -> None:
"""Add and commit all files within the specified directory."""
assert not cwd.is_relative_to(TEST_DIR.parent) # Prevent accidents!
run_check(shell, 'git', 'add', '.', cwd=cwd)
run_check(shell, 'git', 'commit', '-m="add-commit"', cwd=cwd)
@contextmanager
def temporary_git_dir(shell: Subprocess, *, source_dir: Path | None = None) -> Generator[Path, None, None]:
"""Initialize a temporary directory for testing."""
with tempfile.TemporaryDirectory() as tmp_dir:
working_dir = Path(tmp_dir) / (source_dir.name if source_dir else 'subdir')
if source_dir:
shutil.copytree(source_dir, working_dir)
working_dir.mkdir(exist_ok=True)
run_check(shell, 'git', 'init', cwd=working_dir)
# Required for Windows CI and works without issue everywhere else
run_check(shell, 'git', 'config', '--local', 'user.email', 'tester@py.test', cwd=working_dir)
run_check(shell, 'git', 'config', '--local', 'user.name', 'Pytest', cwd=working_dir)
if source_dir:
add_commit(shell, cwd=working_dir)
yield working_dir