Debian Codegraph @

src/test/test.py

raw symbols SCIP
1 #!/usr/bin/python3
2
3 import sys
4 import os
5 import subprocess
6 import time
7 # 1.4.1.1 (added) nshaheed for windows CI
8 import posixpath
9
10
11 failures = 0
12 successes = 0
13 skipped = 0
14 skipTests = {}
15
16
17 def handle_directory(dir, exe):
18 print("")
19 print(">>> Performing tests in %s <<<" % dir)
20
21 global successes
22
23 for filename in sorted(os.listdir(dir)):
24 path = os.path.join(dir, filename)
25 # 1.4.1.1 (added) nshaheed
26 # split path into its components and then rejoin cast as a posix path
27 path_components = os.path.normpath(path).lstrip(os.path.sep).split(os.path.sep)
28 path = posixpath.join(*path_components)
29
30 if os.path.isfile(path):
31 if os.path.splitext(filename)[1] == ".ck":
32 run_test(exe, path, filename, 0)
33 elif os.path.isdir(path) and filename[0] != '.':
34 handle_directory(path, exe)
35
36
37 def run_test(exe, path, filename, attempt):
38 global successes
39 global skipped
40
41 print("> %s %s" % (exe, path))
42
43 if filename in skipTests:
44 skipped += 1
45 return
46
47 try:
48 # NOTE: each command line argument is a new arg to check_output
49 # e.g., chuck_output([exe, "--silent", "--disable-error-show-code", ...
50 # NOTE: use --disable-error-show-code to not showing code on error
51 # NOTE: use --no-color to explicitly disable ANSI escape codes (e.g.,
52 # for color terminal text) from showing up in chuck output;
53 # FYI chuck implicitly checks for TTY; if not will disable printing
54 # escape codes; but leaving this note here FYI
55 result = subprocess.check_output([exe, "--silent", "--chugin-load:off", "%s" % path], stderr=subprocess.STDOUT).decode("utf-8")
56
57 if result.strip().endswith(("\"success\" :(string)",)):
58 successes += 1
59
60 else:
61 if os.path.isfile(path.replace(".ck", ".txt")):
62 # print "\tChecking result with answer file: " + path.replace(".ck", ".txt")
63
64 with open(path.replace(".ck", ".txt")) as answerFile:
65 answer = answerFile.read()
66
67 if answer.replace("\r\n", "\n") != result.replace("\r\n", "\n"):
68 handle_failure(exe, path, filename, attempt, result)
69 else:
70 successes += 1
71 else:
72 handle_failure(exe, path, filename, attempt, result)
73 except subprocess.CalledProcessError as e:
74 handle_failure(exe, path, filename, attempt, e.output)
75
76
77 def handle_failure(exe, path, filename, attempt, error_string):
78 # We'll attempt up to run the test at most this more times
79 retry_attempts_for_failed_tests = 2
80
81 # Between failed runs, let things sleep a little to cool off
82 sleep_interval_between_runs_seconds = 2
83
84 if attempt < retry_attempts_for_failed_tests:
85 time.sleep(sleep_interval_between_runs_seconds)
86 print("Retrying test: %s %s" % (exe, path))
87 run_test(exe, path, filename, attempt + 1)
88 else:
89 fail(filename, error_string)
90
91
92 def fail(test_name, output):
93 global failures
94 print("*** Test '%s' failed: ***" % test_name)
95 print(output)
96 failures += 1
97
98
99 def main():
100 exe = 'chuck'
101 test_dir = '.'
102
103 if len(sys.argv) >= 2:
104 exe = sys.argv[1]
105 if len(sys.argv) >= 3:
106 test_dir = sys.argv[2]
107
108 handle_directory(test_dir, exe)
109
110 print("")
111
112 if skipped > 0:
113 print("Skipped " + str(skipped) + " tests: " + str(skipTests))
114
115 if failures == 0:
116 print("Success - all " + str(successes) + " tests passed!")
117 sys.exit(0)
118 else:
119 print("Failure - " + str(failures) + " test(s) failed")
120 sys.exit(-1)
121
122
123 if __name__ == "__main__":
124 main()