-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.py
94 lines (77 loc) · 2.66 KB
/
run_test.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
#!/usr/bin/env python3
import argparse
import difflib
import os
import shlex
import shutil
import sys
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).parent.absolute()
TEST_ROOT = ROOT / "test" / "data"
COMPILE_COMMANDS_JSON = TEST_ROOT / "build" / "compile_commands.json"
TEST_FILES = [
"audio_encoder.h", "byte_buffer.h", "data_encoding.h", "input.cc",
"mutex_lock.h", "osinfo.h", "ref_counted_base.h"
]
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument(
"--program", type=str, required=True, help="Modernizer Program")
parser.add_argument(
"--keep_temp", action="store_true", help="Keep Temporary files")
args = parser.parse_args(argv)
program = Path(args.program).absolute()
keep_temp = args.keep_temp
cmd = [
f"{program}", f"--project_root={TEST_ROOT}",
f"--compile_commands={COMPILE_COMMANDS_JSON}", "--in_place=false"
]
r = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=sys.stderr, check=True)
patch = r.stdout.decode("UTF-8")
print(f"patch:\n{patch}")
td = tempfile.TemporaryDirectory()
try:
for test_file in TEST_FILES:
src = TEST_ROOT.joinpath(test_file)
dest = Path(td.name).joinpath(test_file)
dest.parent.mkdir(exist_ok=True)
shutil.copy(src, dest)
patch_path = Path(td.name).joinpath("patch.patch")
with open(patch_path, "wb") as f:
f.write(patch.encode("utf-8"))
cmd = ["patch", "-p1", f"-i{patch_path}", "--verbose"]
r = subprocess.run(
cmd,
cwd=td.name,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
if r.returncode != 0:
raise RuntimeError(
f"Command '{shlex.join(cmd)}' returned non-zero exit status"
f" {r.returncode}. stdout:\n{r.stdout}\nstderr:\n{r.stderr}")
print(f"stout:\n{r.stdout}")
print(f"stderr:\n{r.stderr}")
for test_file in TEST_FILES:
expected_name, ext = os.path.splitext(test_file)
expected = TEST_ROOT.joinpath(expected_name + "-expected" + ext)
actual = Path(td.name).joinpath(test_file)
with open(expected, "r") as f:
expected_contents = f.read()
with open(actual, "r") as f:
actual_contents = f.read()
diff = "".join(
list(
x for x in difflib.unified_diff(
expected_contents.splitlines(
keepends=True), actual_contents.splitlines(
keepends=True)))[2:])
if diff:
raise RuntimeError(f"Diff found in {test_file}: \n{diff}")
finally:
if not keep_temp:
td.cleanup()
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))