[945047e] | 1 | #!/usr/bin/python |
---|
[efc15918] | 2 | from __future__ import print_function |
---|
| 3 | |
---|
[122cac7] | 4 | from os import listdir, environ |
---|
[0534c3c] | 5 | from os.path import isfile, join, splitext |
---|
[efc15918] | 6 | from subprocess import Popen, PIPE, STDOUT |
---|
| 7 | |
---|
| 8 | import argparse |
---|
[122cac7] | 9 | import os |
---|
| 10 | import re |
---|
[a43e1d7] | 11 | import stat |
---|
[efc15918] | 12 | import sys |
---|
| 13 | |
---|
| 14 | ################################################################################ |
---|
| 15 | # help functions |
---|
| 16 | ################################################################################ |
---|
| 17 | def listTests(): |
---|
[0534c3c] | 18 | list = [splitext(f)[0] for f in listdir('./.expect') |
---|
| 19 | if not f.startswith('.') and f.endswith('.txt') |
---|
| 20 | ] |
---|
[efc15918] | 21 | |
---|
| 22 | return list |
---|
| 23 | |
---|
[472ca32] | 24 | def sh(cmd, dry_run = False, print2stdout = True): |
---|
[efc15918] | 25 | if dry_run : |
---|
| 26 | print("cmd: %s" % cmd) |
---|
[472ca32] | 27 | return 0, None |
---|
[efc15918] | 28 | else : |
---|
[472ca32] | 29 | proc = Popen(cmd, stdout=None if print2stdout else PIPE, stderr=STDOUT, shell=True) |
---|
| 30 | out, err = proc.communicate() |
---|
| 31 | return proc.returncode, out |
---|
[efc15918] | 32 | |
---|
[122cac7] | 33 | def file_replace(fname, pat, s_after): |
---|
| 34 | # first, see if the pattern is even in the file. |
---|
| 35 | with open(fname) as f: |
---|
| 36 | if not any(re.search(pat, line) for line in f): |
---|
| 37 | return # pattern does not occur in file so we are done. |
---|
| 38 | |
---|
| 39 | # pattern is in the file, so perform replace operation. |
---|
| 40 | with open(fname) as f: |
---|
| 41 | out_fname = fname + ".tmp" |
---|
| 42 | out = open(out_fname, "w") |
---|
| 43 | for line in f: |
---|
| 44 | out.write(re.sub(pat, s_after, line)) |
---|
| 45 | out.close() |
---|
| 46 | os.rename(out_fname, fname) |
---|
| 47 | |
---|
| 48 | def fix_MakeLevel(file) : |
---|
| 49 | if environ.get('MAKELEVEL') : |
---|
| 50 | file_replace(file, "make\[%i\]" % int(environ.get('MAKELEVEL')), 'make' ) |
---|
| 51 | |
---|
[84d4d6f] | 52 | def fileContainsOnly(file, text) : |
---|
| 53 | with open(file) as f: |
---|
| 54 | ff = f.read().strip() |
---|
| 55 | result = ff == text.strip() |
---|
| 56 | # |
---|
| 57 | # print("Comparing :\n\t'%s'\nWith:\n\t'%s'" % (ff, text)) |
---|
| 58 | # print("Result is : \n\t", end="") |
---|
| 59 | # print(result) |
---|
| 60 | |
---|
| 61 | return result; |
---|
| 62 | |
---|
[a43e1d7] | 63 | def fileIsExecutable(file) : |
---|
| 64 | try : |
---|
| 65 | fileinfo = os.stat(file) |
---|
| 66 | return bool(fileinfo.st_mode & stat.S_IXUSR) |
---|
| 67 | except Exception as inst: |
---|
| 68 | print(type(inst)) # the exception instance |
---|
| 69 | print(inst.args) # arguments stored in .args |
---|
| 70 | print(inst) |
---|
| 71 | return False |
---|
[122cac7] | 72 | |
---|
[efc15918] | 73 | ################################################################################ |
---|
| 74 | # running test functions |
---|
| 75 | ################################################################################ |
---|
[3c1d702] | 76 | def run_test_instance(test, generate, dry_run): |
---|
| 77 | |
---|
| 78 | out_file = (".out/%s.log" % test) if not generate else (".expect/%s.txt" % test) |
---|
| 79 | |
---|
| 80 | sh("rm -f %s" % out_file, dry_run) |
---|
[245510f] | 81 | sh("rm -f %s > /dev/null 2>&1" % test, dry_run) |
---|
[efc15918] | 82 | |
---|
| 83 | # build, skipping to next test on error |
---|
[472ca32] | 84 | make_ret, _ = sh("%s %s 2> %s 1> /dev/null" % (make_cmd, test, out_file), dry_run) |
---|
[efc15918] | 85 | |
---|
[3c1d702] | 86 | if make_ret == 0 : |
---|
| 87 | # fetch optional input |
---|
| 88 | stdinput = "< .in/%s.txt" % test if isfile(".in/%s.txt" % test) else "" |
---|
[efc15918] | 89 | |
---|
[a43e1d7] | 90 | if fileIsExecutable(test) : |
---|
| 91 | # run test |
---|
| 92 | sh("./%s %s > %s 2>&1" % (test, stdinput, out_file), dry_run) |
---|
| 93 | else : |
---|
| 94 | # simply cat the result into the output |
---|
| 95 | sh("cat %s > %s" % (test, out_file), dry_run) |
---|
[efc15918] | 96 | |
---|
[3c1d702] | 97 | retcode = 0 |
---|
[472ca32] | 98 | error = None |
---|
[122cac7] | 99 | |
---|
| 100 | fix_MakeLevel(out_file) |
---|
| 101 | |
---|
[84d4d6f] | 102 | if generate : |
---|
| 103 | if not dry_run and fileContainsOnly(out_file, "make: *** No rule to make target `%s'. Stop." % test) : |
---|
| 104 | retcode = 1; |
---|
| 105 | error = "\t\tNo make target for test %s!" % test |
---|
| 106 | sh("rm %s" % out_file, False) |
---|
| 107 | |
---|
| 108 | else : |
---|
[3c1d702] | 109 | # diff the output of the files |
---|
[472ca32] | 110 | diff_cmd = ("diff --old-group-format='\t\tmissing lines :\n" |
---|
| 111 | "%%<' \\\n" |
---|
| 112 | "--new-group-format='\t\tnew lines :\n" |
---|
| 113 | "%%>' \\\n" |
---|
| 114 | "--unchanged-group-format='%%=' \\" |
---|
| 115 | "--changed-group-format='\t\texpected :\n" |
---|
| 116 | "%%<\n" |
---|
| 117 | "\t\tgot :\n" |
---|
| 118 | "%%>' \\\n" |
---|
| 119 | "--new-line-format='\t\t%%dn\t%%L' \\\n" |
---|
| 120 | "--old-line-format='\t\t%%dn\t%%L' \\\n" |
---|
| 121 | "--unchanged-line-format='' \\\n" |
---|
| 122 | ".expect/%s.txt .out/%s.log") |
---|
| 123 | |
---|
| 124 | retcode, error = sh(diff_cmd % (test, test), dry_run, False) |
---|
[efc15918] | 125 | |
---|
| 126 | # clean the executable |
---|
[245510f] | 127 | sh("rm -f %s > /dev/null 2>&1" % test, dry_run) |
---|
[efc15918] | 128 | |
---|
[472ca32] | 129 | return retcode, error |
---|
[efc15918] | 130 | |
---|
[3c1d702] | 131 | def run_tests(tests, generate, dry_run) : |
---|
[74358c3] | 132 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run) |
---|
[3c1d702] | 133 | sh('mkdir -p .out .expect', dry_run) |
---|
| 134 | |
---|
| 135 | if generate : |
---|
[ebcd82b] | 136 | print( "Regenerate tests for: " ) |
---|
[efc15918] | 137 | |
---|
| 138 | failed = False; |
---|
| 139 | for t in tests: |
---|
[ebcd82b] | 140 | print("%20s " % t, end="") |
---|
[efc15918] | 141 | sys.stdout.flush() |
---|
[472ca32] | 142 | test_failed, error = run_test_instance(t, generate, dry_run) |
---|
[efc15918] | 143 | failed = test_failed or failed |
---|
| 144 | |
---|
[84d4d6f] | 145 | if generate : |
---|
| 146 | failed_txt = "ERROR" |
---|
| 147 | success_txt = "Done" |
---|
[ebcd82b] | 148 | else : |
---|
[84d4d6f] | 149 | failed_txt = "FAILED" |
---|
| 150 | success_txt = "PASSED" |
---|
| 151 | |
---|
| 152 | print(failed_txt if test_failed else success_txt) |
---|
| 153 | if error : |
---|
| 154 | print(error, file=sys.stderr) |
---|
[efc15918] | 155 | |
---|
[74358c3] | 156 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run) |
---|
[efc15918] | 157 | |
---|
[b70b6fc] | 158 | return 1 if failed else 0 |
---|
[efc15918] | 159 | |
---|
| 160 | ################################################################################ |
---|
| 161 | # main loop |
---|
| 162 | ################################################################################ |
---|
| 163 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') |
---|
| 164 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') |
---|
[0534c3c] | 165 | parser.add_argument('--list', help='List all test available', action='store_true') |
---|
[efc15918] | 166 | parser.add_argument('--all', help='Run all test available', action='store_true') |
---|
[0534c3c] | 167 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') |
---|
[efc15918] | 168 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') |
---|
| 169 | |
---|
| 170 | options = parser.parse_args() |
---|
| 171 | |
---|
[0534c3c] | 172 | if (len(options.tests) > 0 and options.all and not options.list) \ |
---|
| 173 | or (len(options.tests) == 0 and not options.all and not options.list) : |
---|
[3c1d702] | 174 | print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr) |
---|
| 175 | parser.print_help() |
---|
| 176 | sys.exit(1) |
---|
[efc15918] | 177 | |
---|
[0534c3c] | 178 | allTests = listTests() |
---|
| 179 | |
---|
| 180 | if options.all or options.list : |
---|
| 181 | tests = allTests |
---|
| 182 | |
---|
| 183 | else : |
---|
| 184 | tests = [] |
---|
| 185 | for test in options.tests: |
---|
[177a5ce] | 186 | if test in allTests or options.regenerate_expected : |
---|
[0534c3c] | 187 | tests.append(test) |
---|
| 188 | else : |
---|
| 189 | print('ERROR: No expected file for test %s, ignoring it' % test, file=sys.stderr) |
---|
| 190 | |
---|
| 191 | if len(tests) == 0 : |
---|
| 192 | print('ERROR: No valid test to run', file=sys.stderr) |
---|
| 193 | sys.exit(1) |
---|
| 194 | |
---|
[74358c3] | 195 | tests.sort() |
---|
[7bd045d] | 196 | make_flags = environ.get('MAKEFLAGS') |
---|
| 197 | make_cmd = "make" if make_flags and "-j" in make_flags else "make -j8" |
---|
[74358c3] | 198 | |
---|
[0534c3c] | 199 | if options.list : |
---|
| 200 | print("\n".join(tests)) |
---|
[efc15918] | 201 | |
---|
[0534c3c] | 202 | else : |
---|
| 203 | sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run) ) |
---|