-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
65 lines (52 loc) · 1.72 KB
/
core.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
import contextlib
import os
import pathlib
import subprocess
import sys
import urllib.request
import pip_run.deps
import pip_run.launch
from coherent.build import bootstrap
def build_env(target, *, orig=os.environ):
"""
Prepare the environment for invoking pytest.
Updates the environment with target on PYTHONPATH and
sets any system-level options.
>>> env = build_env('foo', orig=dict(PYTHONPATH='bar'))
>>> env['PYTHONPATH'].replace(os.pathsep, ':')
'foo:bar'
"""
overlay = dict(
PYTHONPATH=pip_run.launch._path_insert(
orig.get('PYTHONPATH', ''), os.fspath(target)
),
PYTEST_ADDOPTS='--doctest-modules',
PYTHONSAFEPATH='1',
)
return {**orig, **overlay}
def load_ruff_toml():
url = 'https://raw.githubusercontent.com/jaraco/skeleton/refs/heads/main/ruff.toml'
return urllib.request.urlopen(url).read().decode('utf-8')
def configure_ruff():
"""
>>> getfixture('monkeypatch').chdir(getfixture('tmp_path'))
>>> with configure_ruff():
... pathlib.Path('ruff.toml').stat().st_size > 0
True
"""
if pathlib.Path('(meta)/ruff.toml').exists():
raise NotImplementedError
return bootstrap.assured(pathlib.Path('ruff.toml'), load_ruff_toml)
@contextlib.contextmanager
def project_on_path():
"""
Install the project under test and yield its new install path.
"""
deps = pip_run.deps.load('--editable', '.[test]')
with bootstrap.write_pyproject(), deps as home, configure_ruff():
yield home
def run():
with project_on_path() as home:
cmd = [sys.executable, '-m', 'pytest', *sys.argv[1:]]
proc = subprocess.Popen(cmd, env=build_env(home))
raise SystemExit(proc.wait())