| 1 | #!python | 
|---|
| 2 | from __future__ import print_function | 
|---|
| 3 |  | 
|---|
| 4 | from os import listdir, environ | 
|---|
| 5 | from os.path import isfile, join, splitext | 
|---|
| 6 | from subprocess import Popen, PIPE, STDOUT | 
|---|
| 7 |  | 
|---|
| 8 | import argparse | 
|---|
| 9 | import os | 
|---|
| 10 | import re | 
|---|
| 11 | import sys | 
|---|
| 12 |  | 
|---|
| 13 | ################################################################################ | 
|---|
| 14 | #               help functions | 
|---|
| 15 | ################################################################################ | 
|---|
| 16 | def listTests(): | 
|---|
| 17 | list = [splitext(f)[0] for f in listdir('./.expect') | 
|---|
| 18 | if not f.startswith('.') and f.endswith('.txt') | 
|---|
| 19 | ] | 
|---|
| 20 |  | 
|---|
| 21 | return list | 
|---|
| 22 |  | 
|---|
| 23 | def sh(cmd, dry_run = False, print2stdout = True): | 
|---|
| 24 | if dry_run : | 
|---|
| 25 | print("cmd: %s" % cmd) | 
|---|
| 26 | return 0, None | 
|---|
| 27 | else : | 
|---|
| 28 | proc = Popen(cmd, stdout=None if print2stdout else PIPE, stderr=STDOUT, shell=True) | 
|---|
| 29 | out, err = proc.communicate() | 
|---|
| 30 | return proc.returncode, out | 
|---|
| 31 |  | 
|---|
| 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 |  | 
|---|
| 52 | ################################################################################ | 
|---|
| 53 | #               running test functions | 
|---|
| 54 | ################################################################################ | 
|---|
| 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) | 
|---|
| 60 | sh("rm -f %s > /dev/null 2>&1" % test, dry_run) | 
|---|
| 61 |  | 
|---|
| 62 | # build, skipping to next test on error | 
|---|
| 63 | make_ret, _ = sh("%s %s 2> %s 1> /dev/null" % (make_cmd, test, out_file), dry_run) | 
|---|
| 64 |  | 
|---|
| 65 | if make_ret == 0 : | 
|---|
| 66 | # fetch optional input | 
|---|
| 67 | stdinput = "< .in/%s.txt" % test if isfile(".in/%s.txt" % test) else "" | 
|---|
| 68 |  | 
|---|
| 69 | # run test | 
|---|
| 70 | sh("./%s %s > %s 2>&1" % (test, stdinput, out_file), dry_run) | 
|---|
| 71 |  | 
|---|
| 72 | retcode = 0 | 
|---|
| 73 | error = None | 
|---|
| 74 |  | 
|---|
| 75 | fix_MakeLevel(out_file) | 
|---|
| 76 |  | 
|---|
| 77 | if not generate : | 
|---|
| 78 | # diff the output of the files | 
|---|
| 79 | diff_cmd = ("diff --old-group-format='\t\tmissing lines :\n" | 
|---|
| 80 | "%%<' \\\n" | 
|---|
| 81 | "--new-group-format='\t\tnew lines :\n" | 
|---|
| 82 | "%%>' \\\n" | 
|---|
| 83 | "--unchanged-group-format='%%=' \\" | 
|---|
| 84 | "--changed-group-format='\t\texpected :\n" | 
|---|
| 85 | "%%<\n" | 
|---|
| 86 | "\t\tgot :\n" | 
|---|
| 87 | "%%>' \\\n" | 
|---|
| 88 | "--new-line-format='\t\t%%dn\t%%L' \\\n" | 
|---|
| 89 | "--old-line-format='\t\t%%dn\t%%L' \\\n" | 
|---|
| 90 | "--unchanged-line-format='' \\\n" | 
|---|
| 91 | ".expect/%s.txt .out/%s.log") | 
|---|
| 92 |  | 
|---|
| 93 | retcode, error = sh(diff_cmd % (test, test), dry_run, False) | 
|---|
| 94 |  | 
|---|
| 95 | # clean the executable | 
|---|
| 96 | sh("rm -f %s > /dev/null 2>&1" % test, dry_run) | 
|---|
| 97 |  | 
|---|
| 98 | return retcode, error | 
|---|
| 99 |  | 
|---|
| 100 | def run_tests(tests, generate, dry_run) : | 
|---|
| 101 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run) | 
|---|
| 102 | sh('mkdir -p .out .expect', dry_run) | 
|---|
| 103 |  | 
|---|
| 104 | if generate : | 
|---|
| 105 | print( "Regenerate tests for: " ) | 
|---|
| 106 |  | 
|---|
| 107 | failed = False; | 
|---|
| 108 | for t in tests: | 
|---|
| 109 | print("%20s  " % t, end="") | 
|---|
| 110 | sys.stdout.flush() | 
|---|
| 111 | test_failed, error = run_test_instance(t, generate, dry_run) | 
|---|
| 112 | failed = test_failed or failed | 
|---|
| 113 |  | 
|---|
| 114 | if not generate : | 
|---|
| 115 | print("FAILED" if test_failed else "PASSED") | 
|---|
| 116 | if error : | 
|---|
| 117 | print(error) | 
|---|
| 118 | else : | 
|---|
| 119 | print( "Done" ) | 
|---|
| 120 |  | 
|---|
| 121 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run) | 
|---|
| 122 |  | 
|---|
| 123 | return 1 if failed else 0 | 
|---|
| 124 |  | 
|---|
| 125 | ################################################################################ | 
|---|
| 126 | #               main loop | 
|---|
| 127 | ################################################################################ | 
|---|
| 128 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') | 
|---|
| 129 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') | 
|---|
| 130 | parser.add_argument('--list', help='List all test available', action='store_true') | 
|---|
| 131 | parser.add_argument('--all', help='Run all test available', action='store_true') | 
|---|
| 132 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') | 
|---|
| 133 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') | 
|---|
| 134 |  | 
|---|
| 135 | options = parser.parse_args() | 
|---|
| 136 |  | 
|---|
| 137 | if (len(options.tests) > 0  and     options.all and not options.list) \ | 
|---|
| 138 | or (len(options.tests) == 0 and not options.all and not options.list) : | 
|---|
| 139 | print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr) | 
|---|
| 140 | parser.print_help() | 
|---|
| 141 | sys.exit(1) | 
|---|
| 142 |  | 
|---|
| 143 | allTests = listTests() | 
|---|
| 144 |  | 
|---|
| 145 | if options.all or options.list : | 
|---|
| 146 | tests = allTests | 
|---|
| 147 |  | 
|---|
| 148 | else : | 
|---|
| 149 | tests = [] | 
|---|
| 150 | for test in options.tests: | 
|---|
| 151 | if test in allTests : | 
|---|
| 152 | tests.append(test) | 
|---|
| 153 | else : | 
|---|
| 154 | print('ERROR: No expected file for test %s, ignoring it' % test, file=sys.stderr) | 
|---|
| 155 |  | 
|---|
| 156 | if len(tests) == 0 : | 
|---|
| 157 | print('ERROR: No valid test to run', file=sys.stderr) | 
|---|
| 158 | sys.exit(1) | 
|---|
| 159 |  | 
|---|
| 160 | tests.sort() | 
|---|
| 161 | make_flags = environ.get('MAKEFLAGS') | 
|---|
| 162 | make_cmd = "make" if make_flags and "-j" in make_flags else "make -j8" | 
|---|
| 163 |  | 
|---|
| 164 | if options.list : | 
|---|
| 165 | print("\n".join(tests)) | 
|---|
| 166 |  | 
|---|
| 167 | else : | 
|---|
| 168 | sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run) ) | 
|---|