Debian Codegraph @

python/tests/run_pytest_with_artifacts.py

raw symbols SCIP
1 import argparse
2 import os
3 import subprocess
4 import sys
5
6
7 def _filter_pythonpath(repo_root: str) -> str:
8 excluded = {
9 os.path.normcase(os.path.abspath(repo_root)),
10 os.path.normcase(os.path.abspath(os.path.join(repo_root, 'python'))),
11 }
12 entries = []
13 for entry in os.environ.get('PYTHONPATH', '').split(os.pathsep):
14 if not entry:
15 continue
16 normalized = os.path.normcase(os.path.abspath(entry))
17 if normalized in excluded:
18 continue
19 entries.append(entry)
20 return os.pathsep.join(entries)
21
22
23 def main() -> int:
24 parser = argparse.ArgumentParser(
25 description='Run pytest and capture both JUnit XML and console output.',
26 )
27 parser.add_argument(
28 'paths',
29 nargs='*',
30 default=['python/tests'],
31 help='Test paths passed to pytest.',
32 )
33 parser.add_argument(
34 '--artifact-dir',
35 default='python-test-artifacts',
36 help='Directory for generated pytest artifacts.',
37 )
38 parser.add_argument(
39 '--artifact-prefix',
40 default='pytest',
41 help='Filename prefix for generated pytest artifacts.',
42 )
43 args, passthrough = parser.parse_known_args()
44
45 artifact_dir = os.path.abspath(args.artifact_dir)
46 os.makedirs(artifact_dir, exist_ok=True)
47 repo_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
48
49 junit_path = os.path.join(artifact_dir, f'{args.artifact_prefix}.xml')
50 log_path = os.path.join(artifact_dir, f'{args.artifact_prefix}.log')
51 test_paths = [os.path.abspath(path) for path in args.paths]
52
53 env = os.environ.copy()
54 filtered_pythonpath = _filter_pythonpath(repo_root)
55 if filtered_pythonpath:
56 env['PYTHONPATH'] = filtered_pythonpath
57 else:
58 env.pop('PYTHONPATH', None)
59
60 command = [
61 sys.executable,
62 '-m',
63 'pytest',
64 *test_paths,
65 f'--junitxml={junit_path}',
66 *passthrough,
67 ]
68
69 print(f'Running: {" ".join(command)}')
70 print(f'Writing JUnit XML to: {junit_path}')
71 print(f'Writing console log to: {log_path}')
72 print(f'Running from cwd: {artifact_dir}')
73
74 with open(log_path, 'w', encoding='utf-8', newline='') as log_file:
75 probe = subprocess.run(
76 [
77 sys.executable,
78 '-c',
79 'import opencc; print(opencc.__file__)',
80 ],
81 stdout=subprocess.PIPE,
82 stderr=subprocess.STDOUT,
83 text=True,
84 cwd=artifact_dir,
85 env=env,
86 check=False,
87 )
88 probe_output = probe.stdout.strip()
89 import_line = f'opencc import probe: {probe_output or "<no output>"}\n'
90 sys.stdout.write(import_line)
91 log_file.write(import_line)
92 if probe.returncode != 0:
93 failure_line = f'opencc import probe failed with exit code {probe.returncode}\n'
94 sys.stdout.write(failure_line)
95 log_file.write(failure_line)
96
97 process = subprocess.Popen(
98 command,
99 stdout=subprocess.PIPE,
100 stderr=subprocess.STDOUT,
101 text=True,
102 cwd=artifact_dir,
103 env=env,
104 )
105 assert process.stdout is not None
106 for line in process.stdout:
107 sys.stdout.write(line)
108 log_file.write(line)
109 return process.wait()
110
111
112 if __name__ == '__main__':
113 sys.exit(main())