[efc15918] | 1 | #!python |
---|
| 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 |
---|
[efc15918] | 11 | import sys |
---|
| 12 | |
---|
| 13 | ################################################################################ |
---|
| 14 | # help functions |
---|
| 15 | ################################################################################ |
---|
| 16 | def listTests(): |
---|
[0534c3c] | 17 | list = [splitext(f)[0] for f in listdir('./.expect') |
---|
| 18 | if not f.startswith('.') and f.endswith('.txt') |
---|
| 19 | ] |
---|
[efc15918] | 20 | |
---|
| 21 | return list |
---|
| 22 | |
---|
| 23 | def sh(cmd, dry_run): |
---|
| 24 | if dry_run : |
---|
| 25 | print("cmd: %s" % cmd) |
---|
| 26 | return 0 |
---|
| 27 | else : |
---|
| 28 | proc = Popen(cmd, stderr=STDOUT, shell=True) |
---|
| 29 | proc.communicate() |
---|
| 30 | return proc.returncode |
---|
| 31 | |
---|
[122cac7] | 32 | def file_replace(fname, pat, s_after): |
---|
| 33 | # first, see if the pattern is even in the file. |
---|
| 34 | with open(fname) as f: |
---|
| 35 | if not any(re.search(pat, line) for line in f): |
---|
| 36 | return # pattern does not occur in file so we are done. |
---|
| 37 | |
---|
| 38 | # pattern is in the file, so perform replace operation. |
---|
| 39 | with open(fname) as f: |
---|
| 40 | out_fname = fname + ".tmp" |
---|
| 41 | out = open(out_fname, "w") |
---|
| 42 | for line in f: |
---|
| 43 | out.write(re.sub(pat, s_after, line)) |
---|
| 44 | out.close() |
---|
| 45 | os.rename(out_fname, fname) |
---|
| 46 | |
---|
| 47 | def fix_MakeLevel(file) : |
---|
| 48 | if environ.get('MAKELEVEL') : |
---|
| 49 | file_replace(file, "make\[%i\]" % int(environ.get('MAKELEVEL')), 'make' ) |
---|
| 50 | |
---|
| 51 | |
---|
[efc15918] | 52 | ################################################################################ |
---|
| 53 | # running test functions |
---|
| 54 | ################################################################################ |
---|
[3c1d702] | 55 | def run_test_instance(test, generate, dry_run): |
---|
| 56 | |
---|
| 57 | out_file = (".out/%s.log" % test) if not generate else (".expect/%s.txt" % test) |
---|
| 58 | |
---|
| 59 | sh("rm -f %s" % out_file, dry_run) |
---|
[245510f] | 60 | sh("rm -f %s > /dev/null 2>&1" % test, dry_run) |
---|
[efc15918] | 61 | |
---|
| 62 | # build, skipping to next test on error |
---|
[7bd045d] | 63 | make_ret = sh("%s %s 2> %s 1> /dev/null" % (make_cmd, test, out_file), dry_run) |
---|
[efc15918] | 64 | |
---|
[3c1d702] | 65 | if make_ret == 0 : |
---|
| 66 | # fetch optional input |
---|
| 67 | stdinput = "< .in/%s.txt" % test if isfile(".in/%s.txt" % test) else "" |
---|
[efc15918] | 68 | |
---|
[3c1d702] | 69 | # run test |
---|
| 70 | sh("./%s %s > %s 2>&1" % (test, stdinput, out_file), dry_run) |
---|
[efc15918] | 71 | |
---|
[3c1d702] | 72 | retcode = 0 |
---|
[122cac7] | 73 | |
---|
| 74 | fix_MakeLevel(out_file) |
---|
| 75 | |
---|
[3c1d702] | 76 | if not generate : |
---|
| 77 | # diff the output of the files |
---|
| 78 | retcode = sh("diff .expect/%s.txt .out/%s.log" % (test, test), dry_run) |
---|
[efc15918] | 79 | |
---|
| 80 | # clean the executable |
---|
[245510f] | 81 | sh("rm -f %s > /dev/null 2>&1" % test, dry_run) |
---|
[efc15918] | 82 | |
---|
| 83 | return retcode |
---|
| 84 | |
---|
[3c1d702] | 85 | def run_tests(tests, generate, dry_run) : |
---|
[74358c3] | 86 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run) |
---|
[3c1d702] | 87 | sh('mkdir -p .out .expect', dry_run) |
---|
| 88 | |
---|
| 89 | if generate : |
---|
[ebcd82b] | 90 | print( "Regenerate tests for: " ) |
---|
[efc15918] | 91 | |
---|
| 92 | failed = False; |
---|
| 93 | for t in tests: |
---|
[ebcd82b] | 94 | print("%20s " % t, end="") |
---|
[efc15918] | 95 | sys.stdout.flush() |
---|
[3c1d702] | 96 | test_failed = run_test_instance(t, generate, dry_run) |
---|
[efc15918] | 97 | failed = test_failed or failed |
---|
| 98 | |
---|
[3c1d702] | 99 | if not generate : |
---|
| 100 | print("FAILED" if test_failed else "PASSED") |
---|
[ebcd82b] | 101 | else : |
---|
| 102 | print( "Done" ) |
---|
[efc15918] | 103 | |
---|
[74358c3] | 104 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run) |
---|
[efc15918] | 105 | |
---|
[b70b6fc] | 106 | return 1 if failed else 0 |
---|
[efc15918] | 107 | |
---|
| 108 | ################################################################################ |
---|
| 109 | # main loop |
---|
| 110 | ################################################################################ |
---|
| 111 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') |
---|
| 112 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') |
---|
[0534c3c] | 113 | parser.add_argument('--list', help='List all test available', action='store_true') |
---|
[efc15918] | 114 | parser.add_argument('--all', help='Run all test available', action='store_true') |
---|
[0534c3c] | 115 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') |
---|
[efc15918] | 116 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') |
---|
| 117 | |
---|
| 118 | options = parser.parse_args() |
---|
| 119 | |
---|
[0534c3c] | 120 | if (len(options.tests) > 0 and options.all and not options.list) \ |
---|
| 121 | or (len(options.tests) == 0 and not options.all and not options.list) : |
---|
[3c1d702] | 122 | print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr) |
---|
| 123 | parser.print_help() |
---|
| 124 | sys.exit(1) |
---|
[efc15918] | 125 | |
---|
[0534c3c] | 126 | allTests = listTests() |
---|
| 127 | |
---|
| 128 | if options.all or options.list : |
---|
| 129 | tests = allTests |
---|
| 130 | |
---|
| 131 | else : |
---|
| 132 | tests = [] |
---|
| 133 | for test in options.tests: |
---|
[fbaebc6] | 134 | if test in allTests : |
---|
[0534c3c] | 135 | tests.append(test) |
---|
| 136 | else : |
---|
| 137 | print('ERROR: No expected file for test %s, ignoring it' % test, file=sys.stderr) |
---|
| 138 | |
---|
| 139 | if len(tests) == 0 : |
---|
| 140 | print('ERROR: No valid test to run', file=sys.stderr) |
---|
| 141 | sys.exit(1) |
---|
| 142 | |
---|
[74358c3] | 143 | tests.sort() |
---|
[7bd045d] | 144 | make_flags = environ.get('MAKEFLAGS') |
---|
| 145 | make_cmd = "make" if make_flags and "-j" in make_flags else "make -j8" |
---|
[74358c3] | 146 | |
---|
[0534c3c] | 147 | if options.list : |
---|
| 148 | print("\n".join(tests)) |
---|
[efc15918] | 149 | |
---|
[0534c3c] | 150 | else : |
---|
| 151 | sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run) ) |
---|