-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
149 lines (109 loc) · 4.09 KB
/
tests.py
File metadata and controls
149 lines (109 loc) · 4.09 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
138
139
140
141
142
143
144
145
146
147
148
149
# Small script to test if the examples still compile after compiler changes
from __future__ import annotations
from typing import Iterable, List, Any, Tuple, Union, TypedDict, Dict
import os
import pathlib
import subprocess
import sys
import shlex
import json
cwd = pathlib.Path(__file__).parent
examples = cwd / 'examples'
bin = cwd / 'bin'
if not bin.exists():
print('Quart executable not found. Please run "make" before running this script.')
exit(1)
if not (bin / 'quart').exists():
print('Quart executable not found. Please run "make" before running this script.')
exit(1)
EXECUTABLE = bin / 'quart'
def run(executable: Union[str, os.PathLike[str]], args: Iterable[Any]) -> Tuple[int, str, str]:
new: List[str] = [shlex.quote(str(arg)) for arg in args]
new.insert(0, str(executable))
process = subprocess.Popen(new, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
process.wait()
assert process.stdout and process.stderr
return process.returncode, process.stdout.read().decode(), process.stderr.read().decode()
class ExampleResult(TypedDict):
returncode: int
args: List[str]
stdout: str
stderr: str
class Example:
def __init__(self, file: pathlib.Path) -> None:
self.file = file
def compile(self) -> None:
returncode, stdout, stderr = run(EXECUTABLE, [self.file])
if returncode != 0:
print(stdout)
print(stderr)
print('-----------------------------------------')
print(f'Failed to compile: {self.file}. Update the example or fix the compiler.')
exit(returncode)
def run(self) -> None:
self.compile()
result = self.parse_output_file()
returncode, stdout, stderr = run(self.file.with_suffix(''), result['args'])
if returncode != result['returncode']:
print(f'Example {self.file} failed with return code {returncode}.')
print('Expected return code:', result['returncode'])
exit(1)
if stdout != result['stdout']:
print(f'Example {self.file} failed.')
print('Expected stdout:', result['stdout'])
print('Actual stdout:', stdout)
exit(1)
if stderr != result['stderr']:
print(f'Example {self.file} failed.')
print('Expected stderr:', result['stderr'])
print('Actual stderr:', stderr)
exit(1)
def update(self, *args: str) -> None:
self.compile()
returncode, stdout, stderr = run(self.file.with_suffix(''), args)
self.update_output_file(returncode, list(args), stdout, stderr)
def has_output_file(self) -> bool:
return self.file.with_suffix('.output.json').exists()
def parse_output_file(self) -> ExampleResult:
output = self.file.with_suffix('.output.json')
with open(output, 'r') as f:
return json.load(f)
def update_output_file(
self, returncode: int, args: List[str], stdout: str, stderr: str
) -> None:
output = self.file.with_suffix('.output.json')
with open(output, 'w') as f:
json.dump({
'returncode': returncode,
'args': args,
'stdout': stdout,
'stderr': stderr
}, f, indent=4)
def get(l: List[str], index: int, default: Any = None) -> Any:
try:
return l[index]
except IndexError:
return default
def main() -> None:
do_update = (get(sys.argv, 1) == 'update')
i = 0
for file in examples.iterdir():
if file.suffix != '.qr':
continue
example = Example(file)
print(f'Running example {i} ({str(file)!r})')
if not example.has_output_file() or do_update:
example.update()
print('Updated output file.\n')
i += 1
continue
example.run()
print()
i += 1
if do_update:
action = 'updated'
else:
action = 'compiled and tested'
print(f'\nSuccessfully {action} {i} examples.')
if __name__ == '__main__':
main()