-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-world
246 lines (213 loc) · 10.4 KB
/
build-world
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python
"""
build-world - Build and run tests for all available targets
Copyright 2019, Daniel Kristensen, Garmin Ltd, or its subsidiaries.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import io
import os
import subprocess
TOP_DIR = os.path.dirname(os.path.realpath(__file__))
if os.name == "nt":
PLATFORMS = ["win32", "win64"]
WAF_EXE = "waf.bat"
BFSDL_TESTS_EXE = "BfsdlTests.exe"
BFDP_EXE = "bfdp.exe"
else:
raise Exception("OS {} not supported yet".format(os.name))
MODES = ["development", "release"]
def check_cmd(cmdline):
proc = subprocess.Popen(cmdline)
proc.communicate();
if proc.returncode != 0:
print("FAILED on {}".format(cmdline))
exit(proc.returncode)
def compare_match_list(section_name, captured_stream, file_baseline):
captured_stream
if not os.path.exists(file_baseline):
print("Missing baseline: {}".format(file_baseline))
print("{}:".format(section_name))
for line in io.BytesIO(captured_stream):
print(" {}".format(line.decode().strip()))
return False
with open(file_baseline, "r") as f_err:
captured_strings = [line.strip() for line in f_err]
if not captured_strings:
print("Baseline has no data: {}".format(file_baseline))
print("{}:".format(section_name))
for line in io.BytesIO(captured_stream):
print(" {}".format(line.decode().strip()))
return False
# Iterate through stderr output
for line in io.BytesIO(captured_stream):
str_err_line = line.decode().strip()
for err_match in captured_strings:
if err_match in str_err_line:
captured_strings.remove(err_match)
break;
if captured_strings:
print("FAILURE: Strings not matched: {}".format(captured_strings[0]))
print("{}:".format(section_name))
for line in io.BytesIO(captured_stream):
print(" {}".format(line.decode().strip()))
return False
else:
return True
def compare_files(file1, file2):
line_num = 1
with open(file1, "r", encoding="UTF-8") as f1, open(file2, "r", encoding="UTF-8") as f2:
while(True):
f1_line = f1.readline()
f2_line = f2.readline()
if not f1_line and not f2_line:
# end of files, both matched
return True
elif f1_line == f2_line:
# match; keep looking
line_num = line_num + 1
continue
f1_line = f1_line.strip()
f2_line = f2_line.strip()
print("FAILED: Mismatch at line {}".format(line_num))
# Ignore the common part of the file names:
for fn_len in range(0, min(len(file1), len(file2))):
if file1[fn_len] != file2[fn_len]:
f1_name = file1[fn_len:]
f2_name = file2[fn_len:]
break;
else:
f1_name = file1
f2_name = file2
if len(f1_name) < len(f2_name):
f1_name += (" " * (len(f2_name) - len(f1_name)))
elif len(f1_name) > len(f2_name):
f2_name += (" " * (len(f1_name) - len(f2_name)))
print("{}: {}".format(f1_name, f1_line))
print("{}: {}".format(f2_name, f2_line))
return False
def run_test_suites(out_path):
bfdp_path = os.path.join(out_path, BFDP_EXE)
test_path = os.path.join(TOP_DIR, "test")
test_specs_path = os.path.join(test_path, "specs")
test_baseline_path = os.path.join(test_path, "baseline")
result_path = os.path.join(out_path, "test_results")
os.makedirs(result_path, exist_ok = True)
for spec_filename in os.listdir(test_specs_path):
if not spec_filename.endswith(".bfsdl"):
# Only process .bfsdl filenames
continue
spec_file_path = os.path.join(test_specs_path, spec_filename)
spec_name = spec_filename[:-6]
cmdline = [bfdp_path, "-v", "validate-spec", "--file", spec_file_path, "--testing"]
if spec_filename.startswith("valid_"):
# This one should succeed
out_file_basename = spec_name + "_out.txt"
err_file_basename = spec_name + "_err.txt"
out_file_destpath = os.path.join(result_path, out_file_basename)
err_file_destpath = os.path.join(result_path, err_file_basename )
out_file_baseline = os.path.join(test_baseline_path, out_file_basename)
err_file_baseline = os.path.join(test_baseline_path, err_file_basename)
print("Testing {}...".format(spec_name), end="")
proc = None
with open(out_file_destpath, "wb") as f_out, open(err_file_destpath, "wb") as f_err:
proc = subprocess.Popen(cmdline, stdout=f_out, stderr=f_err)
proc.communicate();
if proc.returncode != 0:
print("FAILED ({}) on {}".format(proc.returncode, cmdline))
with open(err_file_destpath, "r", encoding="UTF-8") as f_err:
for line in f_err:
print(line.strip())
exit(proc.returncode)
elif not compare_files(out_file_baseline, out_file_destpath):
exit(1)
elif not compare_files(err_file_baseline, err_file_destpath):
exit(1)
else:
print("SUCCESS")
elif spec_filename.startswith("invalid_"):
# This one should fail
err_file_basename = spec_name + "_err.txt"
err_file_destpath = os.path.join(result_path, err_file_basename )
err_file_baseline = os.path.join(test_baseline_path, err_file_basename)
print("Testing {}...".format(spec_name), end="")
# Ignore stdout, but pipe error text to a stream
proc = subprocess.Popen(cmdline, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
_out, err = proc.communicate()
if proc.returncode == 0:
print("FAILED ({}) on {}".format(proc.returncode, cmdline))
exit(proc.returncode)
elif not compare_match_list("stderr", err, err_file_baseline):
exit(1)
else:
print("SUCCESS")
elif spec_filename.startswith("parse_"):
# This one is for parsing
# Use a different command line for parsing
for input_format in ["raw", "hex"]:
out_file_basename = "{}_{}_out.txt".format(spec_name, input_format)
err_file_basename = "{}_{}_err.txt".format(spec_name, input_format)
out_file_destpath = os.path.join(result_path, out_file_basename)
err_file_destpath = os.path.join(result_path, err_file_basename )
out_file_baseline = os.path.join(test_baseline_path, out_file_basename)
err_file_baseline = os.path.join(test_baseline_path, err_file_basename)
in_data_file_path = os.path.join(test_specs_path, "{}_{}.bin".format(spec_name, input_format))
if not os.path.exists(in_data_file_path):
continue;
print("Testing {}:{}...".format(spec_name, input_format), end="")
cmdline = [bfdp_path, "-v", "parse", "--spec", spec_file_path,
"--format", input_format, "--data", in_data_file_path]
# Pipe stdout to a file, stderr to a stream
with open(out_file_destpath, "wb") as f_out:
proc = subprocess.Popen(cmdline, stdout=f_out, stderr=subprocess.PIPE)
_out, err = proc.communicate()
if os.path.exists(err_file_baseline):
expected_out_code = 1
else:
expected_out_code = 0
retcode_match = (proc.returncode == expected_out_code)
if not retcode_match:
print("FAILED ({}) on {}".format(proc.returncode, cmdline))
if err:
print("stderr:")
for line in io.BytesIO(err):
print(" {}".format(line.decode().strip()))
exit(1)
elif not compare_files(out_file_baseline, out_file_destpath):
print("FAILED")
exit(1)
elif err and not compare_match_list("stderr", err, err_file_baseline):
print("FAILED")
exit(1)
else:
print("SUCCESS")
def run_cmd():
for p in PLATFORMS:
for m in MODES:
check_cmd([os.path.join(TOP_DIR, WAF_EXE), "build", "-p", p, "-m", m])
print(p, m)
out_path = os.path.join(TOP_DIR, "_out", "{}_{}".format(p, m))
check_cmd([os.path.join(out_path, BFSDL_TESTS_EXE)])
run_test_suites(out_path)
return 0
if __name__ == "__main__":
exit( run_cmd() )