Repository navigation
Expand file tree
/
Copy pathupdate_expected_output.py
More file actions
executable file
·97 lines (78 loc) · 2.8 KB
/
Copy pathupdate_expected_output.py
File metadata and controls
executable file
·97 lines (78 loc) · 2.8 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
#!/usr/bin/env python3
import filecmp
import os
import shutil
import subprocess
import sys
from pathlib import Path
GREEN = "\033[0;32m"
RED = "\033[0;31m"
RESET = "\033[0m"
def prompt_yes_no(message):
auto_approve = os.environ.get("AUTO_APPROVE", "")
if auto_approve:
return True
while True:
answer = input(f"{message} [y/n] ").strip().lower()
if answer in ("y", "yes"):
return True
if answer in ("n", "no"):
return False
def show_diff(expected_path, actual_path):
subprocess.run(
["git", "diff", "--color-words", "--color=always", "-u",
str(expected_path), str(actual_path)]
)
def check_output(exp_path, actual_path, label, color):
if not actual_path.exists():
return
if not exp_path.exists():
print(f"{color}BEGIN{RESET} {actual_path}{label}")
print(actual_path.read_text(), end="")
print(f"{color}END{RESET} {actual_path}{label}")
if prompt_yes_no(f"Expected {label} is missing, update with the above?"):
shutil.copy2(actual_path, exp_path)
else:
sys.exit(1)
elif not filecmp.cmp(exp_path, actual_path, shallow=False):
show_diff(exp_path, actual_path)
if prompt_yes_no(f"Expected {label} is different, update with the above?"):
shutil.copy2(actual_path, exp_path)
else:
sys.exit(1)
def main():
if len(sys.argv) < 3:
print(f"Usage: {sys.argv[0]} <srcdir> <builddir>", file=sys.stderr)
sys.exit(1)
srcdir = Path(sys.argv[1])
builddir = Path(sys.argv[2])
expected_dir = srcdir / "expected"
expected_dir.mkdir(parents=True, exist_ok=True)
# expected/Makefile.am uses a `dist-hook` to bulk-glob *.out / *.err
# into the distdir instead of an explicit per-file `dist_noinst_DATA`
# list. The explicit form expanded to >128KB of recipe text and
# tripped Linux's MAX_ARG_STRLEN, breaking `make dist` with
# `bash: Argument list too long`. We no longer touch the Makefile.am
# from this script — the dist-hook picks up new fixtures automatically.
builddir = builddir.resolve()
cmd_files = sorted(builddir.glob("*.cmd"),
key=lambda p: p.stat().st_mtime, reverse=True)
for cmd_file in cmd_files:
print()
print(f"Checking test {cmd_file}:")
print(f" {cmd_file.read_text()}", end="")
stem = str(cmd_file)[:-len(".cmd")]
base = os.path.basename(stem)
exp_stem = expected_dir / base
check_output(
Path(f"{exp_stem}.out"),
Path(f"{stem}.out"),
"stdout", GREEN,
)
check_output(
Path(f"{exp_stem}.err"),
Path(f"{stem}.err"),
"stderr", RED,
)
if __name__ == "__main__":
main()