-
-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathtestdist.py
More file actions
137 lines (102 loc) · 3.41 KB
/
Copy pathtestdist.py
File metadata and controls
137 lines (102 loc) · 3.41 KB
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import argparse
import json
import os
import subprocess
import tempfile
from pathlib import Path
from typing import Optional
from .utils import extract_python_archive
def run_dist_python(
dist_root: Path,
python_info,
args: list[str],
extra_env: Optional[dict[str, str]] = None,
**runargs,
) -> subprocess.CompletedProcess[str]:
"""Runs a `python` process from an extracted PBS distribution.
This function attempts to isolate the spawned interpreter from any
external interference (PYTHON* environment variables), etc.
"""
env = dict(os.environ)
# Wipe PYTHON environment variables.
for k in list(env):
if k.startswith("PYTHON"):
del env[k]
if extra_env:
env.update(extra_env)
return subprocess.run(
[str(dist_root / python_info["python_exe"])] + args,
cwd=dist_root,
env=env,
**runargs,
)
def run_custom_unittests(pbs_source_dir: Path, dist_root: Path, python_info) -> int:
"""Runs custom PBS unittests against a distribution."""
args = [
"-m",
"unittest",
"pythonbuild.disttests",
]
env = {
"PYTHONPATH": str(pbs_source_dir),
"TARGET_TRIPLE": python_info["target_triple"],
"BUILD_OPTIONS": python_info["build_options"],
}
res = run_dist_python(dist_root, python_info, args, env, stderr=subprocess.STDOUT)
return res.returncode
def run_stdlib_tests(dist_root: Path, python_info, harness_args: list[str]) -> int:
"""Run Python stdlib tests for a PBS distribution.
The passed path is the `python` directory from the extracted distribution
archive.
"""
args = [
str(dist_root / python_info["run_tests"]),
]
args.extend(harness_args)
return run_dist_python(dist_root, python_info, args).returncode
def main(pbs_source_dir: Path, raw_args: list[str]) -> int:
"""test-distribution.py functionality."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--stdlib",
action="store_true",
help="Run the stdlib test harness",
)
parser.add_argument(
"dist",
nargs=1,
help="Path to distribution to test",
)
parser.add_argument(
"harness_args",
nargs=argparse.REMAINDER,
help="Raw arguments to pass to Python's test harness",
)
args = parser.parse_args(raw_args)
dist_path_raw = Path(args.dist[0])
td = None
try:
if dist_path_raw.is_file():
td = tempfile.TemporaryDirectory()
dist_path = extract_python_archive(dist_path_raw, Path(td.name))
else:
dist_path = dist_path_raw
python_json = dist_path / "PYTHON.json"
with python_json.open("r", encoding="utf-8") as fh:
python_info = json.load(fh)
codes = []
codes.append(run_custom_unittests(pbs_source_dir, dist_path, python_info))
if args.stdlib:
codes.append(run_stdlib_tests(dist_path, python_info, args.harness_args))
if len(codes) == 0:
print("no tests run")
return 1
if any(code != 0 for code in codes):
return 1
return 0
finally:
if td:
td.cleanup()