1 | import os, os.path, sys, platform, re, copy |
---|
2 | import errno # for delete_file_if_present() |
---|
3 | import argparse |
---|
4 | from subprocess import Popen, PIPE |
---|
5 | from time import sleep |
---|
6 | import telnetlib |
---|
7 | |
---|
8 | import comparison # our source |
---|
9 | import globals # our source |
---|
10 | |
---|
11 | |
---|
12 | def test(args, test_name, input, output_net, output_msg, exe_prog, exeargs): |
---|
13 | print(test_name, end=" ") |
---|
14 | command = prepare_exe_with_name(exe_prog) |
---|
15 | command += exeargs |
---|
16 | if len(output_net) > 0: |
---|
17 | command += globals.EXENETMODE |
---|
18 | if args.valgrind: |
---|
19 | command = globals.EXEVALGRINDMODE + command |
---|
20 | |
---|
21 | p = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE) |
---|
22 | |
---|
23 | if len(output_net) > 0: |
---|
24 | sleep(10 if args.valgrind else 1) # time for the server to warm up |
---|
25 | tn = telnetlib.Telnet("localhost", 9009) |
---|
26 | tn.write(bytes(input, "UTF-8")) |
---|
27 | sleep(2) # time for the server to respond... |
---|
28 | # if we had a command in the frams server protocol to close the connection gracefully, then we could use read_all() instead of the trick with sleep()+read_very_eager()+close() |
---|
29 | stdnet = tn.read_very_eager().decode().split("\n") # the server uses "\n" as the end-of-line character on each platform |
---|
30 | tn.close() # after this, the server is supposed to close by itself (the -N option) |
---|
31 | input = "" |
---|
32 | # under Windows, p.stderr.read() and p.stdout.read() block while the process works, under linux it may be different |
---|
33 | # http://stackoverflow.com/questions/3076542/how-can-i-read-all-availably-data-from-subprocess-popen-stdout-non-blocking?rq=1 |
---|
34 | # http://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python |
---|
35 | # p.terminate() #this was required when the server did not have the -N option |
---|
36 | # stderrdata=p.stderr.read() #fortunately it is possible to reclaim (a part of?) stream contents after the process is killed... under Windows this is the ending of the stream |
---|
37 | |
---|
38 | (stdoutdata, stderrdata) = p.communicate(bytes(input, "UTF-8")) # the server process ends... |
---|
39 | stdoutdata = stdoutdata.decode() # bytes to str |
---|
40 | stderrdata = stderrdata.decode() # bytes to str |
---|
41 | # p.stdin.write(we) #this is not recommended because buffers can overflow and the process will hang up (and indeed it does under Windows) - so communicate() is recommended |
---|
42 | # stdout = p.stdout.read() |
---|
43 | # p.terminate() |
---|
44 | |
---|
45 | # print repr(input) |
---|
46 | # print repr(stdoutdata) |
---|
47 | |
---|
48 | stdout = stdoutdata.split(os.linesep) |
---|
49 | # print stdout |
---|
50 | stderr = stderrdata.split(os.linesep) |
---|
51 | ok = check(stdnet if len(output_net) > 0 else stdout, output_list if len(output_list) > 0 else output_net, output_msg) |
---|
52 | |
---|
53 | if p.returncode != 0 and p.returncode is not None: |
---|
54 | print(" ", p.returncode, "<- returned code") |
---|
55 | |
---|
56 | if len(stderrdata) > 0: |
---|
57 | print(" (stderr has %d lines)" % len(stderr)) |
---|
58 | # valgrind examples: |
---|
59 | # ==2176== ERROR SUMMARY: 597 errors from 50 contexts (suppressed: 35 from 8) |
---|
60 | # ==3488== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 35 from 8) |
---|
61 | if (not args.valgrind) or ("ERROR SUMMARY:" in stderrdata and " 0 errors" not in stderrdata) or (args.always_show_stderr): |
---|
62 | print(stderrdata) |
---|
63 | |
---|
64 | if not ok and args.stop: |
---|
65 | sys.exit("First test failure, stopping early.") |
---|
66 | return ok |
---|
67 | |
---|
68 | |
---|
69 | def compare(jest, goal, was_compared_to): |
---|
70 | p = comparison.Comparison(jest, goal) |
---|
71 | if p.ok: |
---|
72 | print("\r", globals.ANSI_SETGREEN + " ok" + globals.ANSI_RESET) |
---|
73 | else: |
---|
74 | print("\r", globals.ANSI_SETRED + " FAIL\7" + globals.ANSI_RESET) |
---|
75 | print(p.result) |
---|
76 | f = open(os.path.join(globals.THISDIR, '_last_failed' + was_compared_to + '.output'), 'w') # files are easier to compare than stdout |
---|
77 | print('\n'.join(jest), end="", file=f) |
---|
78 | f = open(os.path.join(globals.THISDIR, '_last_failed' + was_compared_to + '.goal'), 'w') # files are easier to compare than stdout |
---|
79 | print('\n'.join(goal), end="", file=f) |
---|
80 | return p.ok |
---|
81 | |
---|
82 | |
---|
83 | def remove_prefix(text, prefix): |
---|
84 | return text[len(prefix):] if text.startswith(prefix) else text |
---|
85 | |
---|
86 | |
---|
87 | def check(stdout, output_net, output_msg): |
---|
88 | actual_out_msg = [] |
---|
89 | if len(output_net) > 0: # in case of the server, there is no filtering |
---|
90 | for line in stdout: |
---|
91 | actual_out_msg.append(line) |
---|
92 | return compare(actual_out_msg, output_net, '') |
---|
93 | else: |
---|
94 | FROMSCRIPT = "Script.Message: " |
---|
95 | beginnings = tuple(["[" + v + "] " for v in ("INFO", "WARN", "ERROR", "CRITICAL")]) # there is also "DEBUG" |
---|
96 | header_begin = 'VMNeuronManager.autoload: Neuro classes added: ' # header section printed when the simulator is created |
---|
97 | header_end = "UserScripts.autoload: " # ending of the header section |
---|
98 | now_in_header = False |
---|
99 | for line in stdout: |
---|
100 | if now_in_header: |
---|
101 | if header_end in line: # "in" because multithreaded simulators prefix their messages with their numerical id, e.g. #12/... |
---|
102 | now_in_header = False |
---|
103 | continue |
---|
104 | else: |
---|
105 | if header_begin in line: # as above |
---|
106 | now_in_header = True |
---|
107 | continue |
---|
108 | line = remove_prefix(line, beginnings[0]) # cut out [INFO], other prefixes we want to leave as they are |
---|
109 | line = remove_prefix(line, FROMSCRIPT) # cut out FROMSCRIPT |
---|
110 | actual_out_msg.append(line) |
---|
111 | if actual_out_msg[-1] == '': # empty line at the end which is not present in our "goal" contents |
---|
112 | actual_out_msg.pop() |
---|
113 | return compare(actual_out_msg, output_msg, '') |
---|
114 | |
---|
115 | |
---|
116 | def delete_file_if_present(filename): |
---|
117 | print('"%s" (%s)' % (filename, "the file was present" if os.path.exists(filename) else "this file did not exist")) |
---|
118 | try: |
---|
119 | os.remove(filename) |
---|
120 | except OSError as e: |
---|
121 | if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory |
---|
122 | raise # re-raise exception if a different error occurred |
---|
123 | |
---|
124 | |
---|
125 | def reset_values(): |
---|
126 | global input_text, output_net, output_msg, test_name, ini, output_list, exeargs, exe_prog |
---|
127 | input_text = "" |
---|
128 | output_list = [] |
---|
129 | ini = "" |
---|
130 | output_net, output_msg = [], [] |
---|
131 | exeargs = [] |
---|
132 | test_name = "no-name test" |
---|
133 | |
---|
134 | |
---|
135 | def is_test_active(): |
---|
136 | global test_name |
---|
137 | if name_template == "": |
---|
138 | return True |
---|
139 | if re.match(name_template, test_name): |
---|
140 | return True |
---|
141 | return False |
---|
142 | |
---|
143 | |
---|
144 | def prepare_exe_with_name(name): |
---|
145 | if name in globals.EXENAMES: |
---|
146 | exename = copy.copy(globals.EXENAMES[name]) # without copy, the following modifications would change values in the EXENAMES table |
---|
147 | else: |
---|
148 | exename = [name] |
---|
149 | for rule in globals.EXERULES: |
---|
150 | exename[0] = re.sub(rule[0], rule[1], exename[0]) |
---|
151 | return exename |
---|
152 | |
---|
153 | |
---|
154 | def main(): |
---|
155 | global input_text, name_template, test_name, exe_prog, exeargs |
---|
156 | name_template = "" |
---|
157 | exeargs = [] |
---|
158 | |
---|
159 | parser = argparse.ArgumentParser() |
---|
160 | parser.add_argument("-val", "--valgrind", help="Use valgrind", action="store_true") |
---|
161 | parser.add_argument("-c", "--nocolor", help="Don't use color output", action="store_true") |
---|
162 | parser.add_argument("-f", "--file", help="File name with tests", required=True) |
---|
163 | parser.add_argument("-tp", "--tests-path", help="tests directory, files containing test definitions, inputs and outputs are relative to this directory, default is '" + globals.THISDIR + "'") |
---|
164 | parser.add_argument("-fp", "--files-path", help="files directory, files tested by OUTFILECOMPARE are referenced relative to this directory, default is '" + globals.FILESDIR + "'") |
---|
165 | parser.add_argument("-wp", "--working-path", help="working directory, test executables are launched after chdir to this directory, default is '" + globals.EXEDIR + "'") |
---|
166 | parser.add_argument("-n", "--name", help="Test name (regexp)") # e.g. '^((?!python).)*$' = these tests which don't have the word "python" in their name |
---|
167 | parser.add_argument("-s", "--stop", help="Stops on first difference", action="store_true") |
---|
168 | parser.add_argument("-ds", "--diffslashes", help="Discriminate between slashes (consider / and \\ different)", action="store_true") |
---|
169 | parser.add_argument("-err", "--always-show-stderr", help="Always print stderr (by default it is hidden if 0 errors in valgrind)", action="store_true") |
---|
170 | parser.add_argument("-e", "--exe", help="Regexp 'search=replace' rule(s) transforming executable name(s) into paths (eg. '(.*)=path/to/\\1.exe')", action='append') # in the example, double backslash is just for printing |
---|
171 | parser.add_argument("-p", "--platform", help="Override platform identifier (referencing platform specific files " + globals.SPEC_INSERTPLATFORMDEPENDENTFILE + "), default:sys.platform (win32,linux2)") |
---|
172 | args = parser.parse_args() |
---|
173 | if args.valgrind: |
---|
174 | print("Using valgrind...") |
---|
175 | if args.diffslashes: |
---|
176 | globals.DIFFSLASHES = args.diffslashes |
---|
177 | if args.file: |
---|
178 | main_test_filename = args.file |
---|
179 | if args.tests_path: |
---|
180 | globals.THISDIR = args.tests_path |
---|
181 | if args.files_path: |
---|
182 | globals.FILESDIR = args.files_path |
---|
183 | if args.working_path: |
---|
184 | globals.EXEDIR = args.working_path |
---|
185 | if args.name: |
---|
186 | name_template = args.name |
---|
187 | if args.exe: |
---|
188 | for e in args.exe: |
---|
189 | search, replace = e.split('=', 1) |
---|
190 | globals.EXERULES.append((search, replace)) |
---|
191 | if args.platform: |
---|
192 | globals.PLATFORM = args.platform |
---|
193 | |
---|
194 | os.chdir(globals.EXEDIR) |
---|
195 | |
---|
196 | globals.init_colors(args) |
---|
197 | |
---|
198 | fin = open(os.path.join(globals.THISDIR, args.file)) |
---|
199 | reset_values() |
---|
200 | exe_prog = "default" # no longer in reset_values (exe: persists across tests) |
---|
201 | outfile = [] |
---|
202 | tests_failed = 0 |
---|
203 | tests_total = 0 |
---|
204 | for line in fin: |
---|
205 | line = globals.stripEOL(line) |
---|
206 | if len(line) == 0 or line.startswith("#"): |
---|
207 | continue |
---|
208 | line = line.split(":", 1) |
---|
209 | # print line |
---|
210 | command = line[0] |
---|
211 | if command == "TESTNAME": |
---|
212 | reset_values() |
---|
213 | test_name = line[1] |
---|
214 | elif command == "arg": |
---|
215 | exeargs.append(line[1]) |
---|
216 | elif command == "exe": |
---|
217 | exe_prog = line[1] |
---|
218 | elif command == "in": |
---|
219 | input_text += line[1] + "\n" |
---|
220 | elif command == "out-net": |
---|
221 | output_net.append(line[1]) |
---|
222 | elif command == "out-file": |
---|
223 | outfile.append(line[1]) |
---|
224 | elif command == "out-mesg": |
---|
225 | output_msg.append(line[1]) |
---|
226 | elif command == "out": |
---|
227 | output_list.append(line[1]) |
---|
228 | elif command == "DELETEFILENOW": |
---|
229 | if is_test_active(): |
---|
230 | print("\t ", command, end=" ") |
---|
231 | delete_file_if_present(os.path.join(globals.FILESDIR, line[1])) |
---|
232 | elif command == "OUTFILECLEAR": |
---|
233 | outfile = [] |
---|
234 | elif command == "OUTFILECOMPARE": |
---|
235 | if is_test_active(): |
---|
236 | print("\t", command, '"%s"' % line[1], end=" ") |
---|
237 | contents = [] |
---|
238 | with open(os.path.join(globals.FILESDIR, line[1]), 'r') as main_test_filename: |
---|
239 | for line in main_test_filename: |
---|
240 | contents.append(globals.stripEOL(line)) |
---|
241 | ok = compare(contents, outfile, '_file') |
---|
242 | tests_failed += int(not ok) |
---|
243 | tests_total += 1 |
---|
244 | elif command == "RUNTEST": |
---|
245 | if is_test_active(): |
---|
246 | print("\t", command, end=" ") |
---|
247 | ok = test(args, test_name, input_text, output_net, output_msg, exe_prog, exeargs) |
---|
248 | tests_failed += int(not ok) |
---|
249 | tests_total += 1 |
---|
250 | else: |
---|
251 | raise Exception("Don't know what to do with this line in test file: ", line) |
---|
252 | |
---|
253 | return (tests_failed, tests_total) |
---|
254 | |
---|
255 | |
---|
256 | if __name__ == "__main__": |
---|
257 | tests_failed, tests_total = main() |
---|
258 | print("%d / %d failed tests" % (tests_failed, tests_total)) |
---|
259 | sys.exit(tests_failed) # return the number of failed tests as exit code ("error level") to shell |
---|