-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathconftest.py
144 lines (111 loc) · 3.66 KB
/
conftest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import json
import os
import shutil
import subprocess
import sys
import tempfile
from collections.abc import Iterator
from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
from typing import Any, cast
import pytest
from nixpkgs_review.utils import current_system
TEST_ROOT = Path(__file__).parent.resolve()
sys.path.append(str(TEST_ROOT.parent))
@dataclass
class Nixpkgs:
path: Path
remote: Path
def run(cmd: list[str | Path]) -> None:
subprocess.run(cmd, check=True)
def real_nixpkgs() -> str:
proc = subprocess.run(
["nix-instantiate", "--find-file", "nixpkgs"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
check=False,
)
if proc.returncode == 0:
return proc.stdout.strip()
proc = subprocess.run(
[
"nix",
"eval",
"--extra-experimental-features",
"nix-command flakes",
"--raw",
"nixpkgs#path",
],
check=True,
stdout=subprocess.PIPE,
text=True,
)
return proc.stdout.strip()
def setup_nixpkgs(target: Path) -> Path:
shutil.copytree(
Helpers.root().joinpath("assets/nixpkgs"),
target,
dirs_exist_ok=True,
)
default_nix = target.joinpath("default.nix")
text = default_nix.read_text().replace('"@NIXPKGS@"', real_nixpkgs())
default_nix.write_text(text)
return target
class Chdir:
def __init__(self, path: Path | str) -> None:
self.old_dir = Path.cwd()
self.new_dir = path
def __enter__(self) -> None:
os.chdir(self.new_dir)
def __exit__(self, *args: object) -> None:
os.chdir(self.old_dir)
def setup_git(path: Path) -> Nixpkgs:
os.environ["GIT_AUTHOR_NAME"] = "nixpkgs-review"
os.environ["GIT_AUTHOR_EMAIL"] = "[email protected]"
os.environ["GIT_COMMITTER_NAME"] = "nixpkgs-review"
os.environ["GIT_COMMITTER_EMAIL"] = "[email protected]"
run(["git", "-C", path, "init", "-b", "master"])
run(["git", "-C", path, "add", "."])
run(["git", "-C", path, "commit", "-m", "first commit"])
remote = path.joinpath("remote")
run(["git", "-C", path, "init", "--bare", str(remote)])
run(["git", "-C", path, "remote", "add", "origin", str(remote)])
run(["git", "-C", path, "push", "origin", "HEAD"])
return Nixpkgs(path=path, remote=remote)
class Helpers:
@staticmethod
def root() -> Path:
return TEST_ROOT
@staticmethod
def read_asset(asset: str) -> str:
return (TEST_ROOT / "assets" / asset).read_text()
@staticmethod
def load_report(review_dir: str) -> dict[str, Any]:
data = (Path(review_dir) / "report.json").read_text()
return cast(dict[str, Any], json.loads(data))
@staticmethod
def assert_built(pkg_name: str, path: str) -> None:
report = Helpers.load_report(path)
assert report["result"][current_system()]["built"] == [pkg_name]
@staticmethod
@contextmanager
def save_environ() -> Iterator[None]:
old = os.environ.copy()
yield
os.environ.clear()
os.environ.update(old)
@staticmethod
@contextmanager
def nixpkgs() -> Iterator[Nixpkgs]:
with Helpers.save_environ(), tempfile.TemporaryDirectory() as tmpdirname:
path = Path(tmpdirname)
nixpkgs_path = path.joinpath("nixpkgs")
os.environ["XDG_CACHE_HOME"] = str(path.joinpath("cache"))
setup_nixpkgs(nixpkgs_path)
with Chdir(nixpkgs_path):
yield setup_git(nixpkgs_path)
@pytest.fixture
def helpers() -> type[Helpers]:
return Helpers