-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
401 lines (344 loc) · 11.6 KB
/
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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import argparse
import os
import subprocess
import json
from pathlib import Path
from enum import Enum
from collections import deque
import logging
from rich.logging import RichHandler
from typing import Deque, Set
parser = argparse.ArgumentParser()
# GENERAL
parser.add_argument(
"-x",
"--exitfirst",
action="store_true",
help="Exit instantly on first error or failed test.",
)
parser.add_argument(
"--cache-path",
type=str,
help="Path to cache file.",
default=".sml_test_cache.json",
)
parser.add_argument("--cache-show", nargs="?", const="*", help="Show cache contents.")
parser.add_argument(
"--cache-clear",
action="store_true",
help="Remove all cache contents at the start of the test run.",
)
parser.add_argument(
"-k",
"--expression",
type=str,
help="Only run tests which match the given substring expression.",
default="test_*.sml",
)
parser.add_argument(
"--lf",
"--last-failed",
action="store_true",
help="Rerun only the tests that failed at the last run.",
)
parser.add_argument(
"--ff",
"--failed-first",
action="store_true",
help="Run all tests but run the last failures first.",
)
parser.add_argument(
"--nf", "--new-first", action="store_true", help="Run tests from new files first."
)
parser.add_argument(
"--nl", "--new-last", action="store_true", help="Run tests from new files last."
)
parser.add_argument(
"--sw-skip",
"--stepwise-skip",
action="store_true",
help="Ignore the first failing test but stop on the next one.",
)
parser.add_argument("--maxfail", type=int, help="Exit after the first num failures.")
# COLLECTION
parser.add_argument(
"--collect-only",
"--co",
action="store_true",
help="Only collect tests, don’t execute them.",
)
parser.add_argument(
"--ignore", action="append", help="Ignore path during collection (multi-allowed)."
)
parser.add_argument(
"--ignore-glob",
action="append",
help="Ignore path pattern during collection (multi-allowed).",
)
parser.add_argument(
"--rootdir", type=str, help="Define root directory for tests.", default=os.getcwd()
)
# LOGGING
parser.add_argument(
"--log-file", type=str, help="Path to a log file.", default="test.log"
)
parser.add_argument(
"--log-file-level",
type=str,
help="Level of messages to catch/display (default depends on the root log handler).",
choices=["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="WARNING",
)
parser.add_argument(
"--log-file-format",
type=str,
help="Log format used by the logging module.",
default="%(asctime)s [%(levelname)8s] %(message)s",
)
parser.add_argument(
"--log-file-date-format",
type=str,
help="Log date format used by the logging module.",
default="%Y-%m-%d %H:%M:%S",
)
parser.add_argument(
"--log-cli-level",
type=str,
help="CLI logging level.",
choices=["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="INFO",
)
parser.add_argument(
"--log-cli-format", type=str, help="CLI log format.", default="%(message)s"
)
parser.add_argument(
"--log-cli-date-format", type=str, help="CLI log date format.", default="%H:%M:%S"
)
parser.add_argument(
"--log-disable-file", type=bool, help="Disable log file output.", default=False
)
args = parser.parse_args()
class LogLevel(Enum):
NOTSET = logging.NOTSET
DEBUG = logging.DEBUG
INFO = logging.INFO
WARNING = logging.WARNING
ERROR = logging.ERROR
CRITICAL = logging.CRITICAL
logger_base = logging.getLogger()
logger_base.setLevel(logging.DEBUG)
# RichHandler for stdout
rich_handler = RichHandler(
rich_tracebacks=True,
tracebacks_show_locals=True,
tracebacks_theme="solarized",
markup=True,
)
formatter_stdout = logging.Formatter(
args.log_cli_format, datefmt=args.log_cli_date_format
)
rich_handler.setFormatter(formatter_stdout)
print(f"Setting log level of CLI to {LogLevel[args.log_cli_level].value}")
rich_handler.setLevel(LogLevel[args.log_cli_level].value)
logger_base.addHandler(rich_handler)
if not args.log_disable_file:
file_handler = logging.FileHandler(args.log_file)
formatter_file = logging.Formatter(
args.log_file_format, datefmt=args.log_file_date_format
)
file_handler.setFormatter(formatter_file)
print(f"Setting log level of file to {LogLevel[args.log_file_level].value}")
file_handler.setLevel(LogLevel[args.log_file_level].value)
logger_base.addHandler(file_handler)
def clear_cache():
with open(args.cache_path, "w") as cache_file:
cache_file.write("{}")
if args.cache_clear:
clear_cache()
if args.cache_show:
with open(args.cache_path, "r") as cache_file:
cache = json.load(cache_file)
logging.info(cache)
tests: Deque[Path] = deque()
# Collect tests
tests_path = Path(args.rootdir)
dir_err_msg = (
f"Path for test ({tests_path}) is not a directory. Set with argument --rootdir."
)
if not tests_path.exists():
raise FileNotFoundError(dir_err_msg)
if not tests_path.is_dir():
raise NotADirectoryError(dir_err_msg)
logging.debug(f"Collecting tests from {tests_path}")
try:
all_seen_tests = set()
last_failed_tests = set()
with open(args.cache_path, "r") as cache_file:
cache_ = json.load(cache_file)
logging.debug(f"Cache file on start: {cache_}")
logging.debug(f"all_seen_tests in cache: {cache_.get('all_seen_tests')}")
logging.debug(f"failed_tests in cache: {cache_.get('failed_tests')}")
all_seen_tests = set(Path(test) for test in cache_.get("all_seen_tests", []))
last_failed_tests = set(Path(test) for test in cache_.get("failed_tests", []))
except Exception as e:
logging.error(f"Error reading cache file: {e}")
# Fall back to empty sets
if not all_seen_tests:
all_seen_tests = set()
if not last_failed_tests:
last_failed_tests = set()
for test in tests_path.rglob(args.expression):
if not test.is_file():
continue
skip_test = False
# Ignore glob
if args.ignore_glob:
logging.debug(f"Checking ignore glob {args.ignore_glob} for test {test}")
for ignore_glob in args.ignore_glob:
if test.match(ignore_glob):
logging.info(
f"Ignoring test {test} because of ignore glob {ignore_glob}"
)
skip_test = True
break
if skip_test:
continue
# Ignore
if args.ignore:
logging.debug(f"Checking ignore {args.ignore} for test {test}")
for ignore in args.ignore:
if test == Path(ignore):
logging.info(f"Ignoring test {test} because of ignore {ignore}")
skip_test = True
break
if skip_test:
continue
# If --last-failed is set, only include the tests that are in the 'failed_tests' cache
if args.lf and test not in last_failed_tests:
continue
logging.info(f"Found test {test}")
# If --failed-first is set and test was failed previously, prepend it to the test list so it is run first
if args.ff and test in last_failed_tests:
tests.appendleft(Path(test))
continue
# If --new-first is set and test is new, prepend it to the test list so it is run first
if args.nf and test not in all_seen_tests:
tests.appendleft(Path(test))
continue
tests.append(Path(test))
logging.info(f"Found {len(tests)} tests")
# If --new-last is set, sort by mtime
if args.nl:
tests = deque(sorted(tests, key=lambda x: x.stat().st_mtime))
def format_test_list(tests: Set[Path]) -> str:
return "\n\t".join([test.name for test in tests])
if args.collect_only:
logging.info(f"Collected tests: {format_test_list(tests)}")
logging.warning("Exiting because --collect-only was set")
exit(0)
# Set environemnt vars that the sml test runner uses
if not args.maxfail:
os.environ["SML_TEST_EXITFIRST"] = "true"
else:
os.environ["SML_TEST_EXITFIRST"] = str(args.exitfirst)
maxfail = args.maxfail or len(tests) + 8
if args.sw_skip:
maxfail = 1 # Only skip the first failing test if --sw-skip is set
os.environ["SML_TEST_MAXFAIL"] = str(maxfail)
def check_for_runtime_error(output: str) -> bool:
if "uncaught exception" in output:
return True
def check_for_compile_error(output: str) -> bool:
compile_errors = {
"syntax error",
"unbound variable",
"type error",
"operator and operand",
"nonexhaustive match failure",
"variable is bound multiple times",
"unbound structure",
}
if any(error in output for error in compile_errors):
return True
failed_tests = set()
runtime_error_tests = set()
compile_error_tests = set()
passed_tests = set()
def run_test(test_path: Path):
global failed_tests
if not isinstance(test_path, Path):
raise TypeError(
f"Expected test_path to be of type Path, but got {type(test_path)}"
)
logging.info(f"Running test {test_path.name}")
logging.debug(f"Command used to run test: 'sml < {test_path}'")
test_file_obj = open(test_path, "r")
process = subprocess.Popen(
["sml"],
stdin=test_file_obj,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True,
)
output, error = process.communicate()
if output:
formatted_output = output.replace("\n", "\n\t\t")
if LogLevel[args.log_cli_level].value <= LogLevel.ERROR.value:
print(f"Output: {formatted_output}")
if check_for_compile_error(output):
compile_error_tests.add(test_path)
logging.critical(f"Compile error in test {test_path}")
elif check_for_runtime_error(output):
runtime_error_tests.add(test_path)
logging.error(f"Runtime error in test {test_path}")
elif "Test failed" in output:
failed_tests.add(test_path)
else:
passed_tests.add(test_path)
if error:
logging.critical("Error with test runner script")
print(f"Error: {error}")
logging.info("Running tests")
logging.debug(f"All Tests: {tests}")
for test in tests:
run_test(test)
if passed_tests:
logging.info(
f"Passed all tests in files:\n\t[green]{format_test_list(passed_tests)}[/green]"
)
if compile_error_tests:
logging.critical(
f"Compilation error in test files:[bold red]\n\t{format_test_list(compile_error_tests)}[/bold red]"
)
if runtime_error_tests:
logging.error(
f"Runtimes errors in test files:\n\t[red]{format_test_list(runtime_error_tests)}[/red]"
)
if failed_tests:
logging.warning(
f"Failed tests in files:\n\t[dim red]{format_test_list(failed_tests)}[/dim red]"
)
passed_all = len(passed_tests) == len(tests)
if passed_all:
logging.info("All tests passed!")
else:
logging.info(f"View the log file for more information on the failed tests.")
logging.info(f"Log file location: {str(Path(args.log_file))}")
try:
with open(args.cache_path, "r") as cache_file:
cache = json.load(cache_file)
except (json.JSONDecodeError, FileNotFoundError):
# Previous error wrote corrupted cache file or first run
clear_cache()
cache = {}
def write_(test_set: Set[Path], cache_key: str):
if len(test_set) == 0:
cache[cache_key] = []
return
cache[cache_key] = [str(test.resolve()) for test in test_set]
write_(runtime_error_tests, "runtime_error_tests")
write_(failed_tests, "failed_tests")
write_(passed_tests, "passed_tests")
write_(runtime_error_tests.union(failed_tests).union(passed_tests), "all_seen_tests")
with open(args.cache_path, "w") as cache_file:
json.dump(cache, cache_file)