| [efc15918] | 1 | #!python | 
|---|
|  | 2 | from __future__ import print_function | 
|---|
|  | 3 |  | 
|---|
|  | 4 | from os import listdir | 
|---|
|  | 5 | from os.path import isfile, join | 
|---|
|  | 6 | from subprocess import Popen, PIPE, STDOUT | 
|---|
|  | 7 |  | 
|---|
|  | 8 | import argparse | 
|---|
|  | 9 | import sys | 
|---|
|  | 10 |  | 
|---|
|  | 11 | ################################################################################ | 
|---|
|  | 12 | #               help functions | 
|---|
|  | 13 | ################################################################################ | 
|---|
|  | 14 | def listTests(): | 
|---|
|  | 15 | list = [f.rstrip('.c') for f in listdir('.') | 
|---|
|  | 16 | if not f.startswith('.') and ( | 
|---|
|  | 17 | not isfile(f) or f.endswith('.c') | 
|---|
|  | 18 | )] | 
|---|
|  | 19 |  | 
|---|
|  | 20 | return list | 
|---|
|  | 21 |  | 
|---|
|  | 22 | def sh(cmd, dry_run): | 
|---|
|  | 23 | if dry_run : | 
|---|
|  | 24 | print("cmd: %s" % cmd) | 
|---|
|  | 25 | return 0 | 
|---|
|  | 26 | else : | 
|---|
|  | 27 | proc = Popen(cmd, stderr=STDOUT, shell=True) | 
|---|
|  | 28 | proc.communicate() | 
|---|
|  | 29 | return proc.returncode | 
|---|
|  | 30 |  | 
|---|
|  | 31 | ################################################################################ | 
|---|
|  | 32 | #               running test functions | 
|---|
|  | 33 | ################################################################################ | 
|---|
|  | 34 | def run_test_instance(test, dry_run): | 
|---|
|  | 35 | sh("rm -f .out/%s.log" % test, dry_run) | 
|---|
|  | 36 | sh("rm -f %s" % test, dry_run) | 
|---|
|  | 37 |  | 
|---|
|  | 38 | # build, skipping to next test on error | 
|---|
|  | 39 | sh("make -j 8 %s >> .out/%s.log 2>&1" % (test, test), dry_run) | 
|---|
|  | 40 |  | 
|---|
|  | 41 | # fetch optional input | 
|---|
|  | 42 | stdinput = "< .in/%s.txt" % test if isfile(".in/%s.txt" % test) else "" | 
|---|
|  | 43 |  | 
|---|
|  | 44 | # run test | 
|---|
|  | 45 | sh("./%s %s >> .out/%s.log 2>&1" % (test, stdinput, test), dry_run) | 
|---|
|  | 46 |  | 
|---|
|  | 47 | # touch expected files so empty output are supported by default | 
|---|
|  | 48 | sh("touch .expect/%s.txt" % test, dry_run) | 
|---|
|  | 49 |  | 
|---|
|  | 50 | # diff the output of the files | 
|---|
|  | 51 | retcode = sh("diff .expect/%s.txt .out/%s.log" % (test, test), dry_run) | 
|---|
|  | 52 |  | 
|---|
|  | 53 | # clean the executable | 
|---|
|  | 54 | sh("rm -f %s" % test, dry_run) | 
|---|
|  | 55 |  | 
|---|
|  | 56 | return retcode | 
|---|
|  | 57 |  | 
|---|
|  | 58 | def run_tests(tests, dry_run) : | 
|---|
|  | 59 | sh('make clean > /dev/null 2>&1', dry_run) | 
|---|
|  | 60 |  | 
|---|
|  | 61 | failed = False; | 
|---|
|  | 62 | for t in tests: | 
|---|
|  | 63 | print("%10s  " % t, end="") | 
|---|
|  | 64 | sys.stdout.flush() | 
|---|
|  | 65 | test_failed = run_test_instance(t, dry_run) | 
|---|
|  | 66 | failed = test_failed or failed | 
|---|
|  | 67 | print("FAILED" if test_failed else "PASSED") | 
|---|
|  | 68 |  | 
|---|
|  | 69 | sh('make clean > /dev/null 2>&1', dry_run) | 
|---|
|  | 70 |  | 
|---|
|  | 71 | return 0 if failed else 1 | 
|---|
|  | 72 |  | 
|---|
|  | 73 | ################################################################################ | 
|---|
|  | 74 | #               generate expectation functions | 
|---|
|  | 75 | ################################################################################ | 
|---|
|  | 76 | def generate_test_expect(test): | 
|---|
|  | 77 | sh("rm -f .out/%s.log" % test, False) | 
|---|
|  | 78 | sh("rm -f %s" % test, False) | 
|---|
|  | 79 |  | 
|---|
|  | 80 | # build, skipping to next test on error | 
|---|
|  | 81 | sh("make -j 8 %s > .expect/%s.txt 2>&1" % (test, test), False) | 
|---|
|  | 82 |  | 
|---|
|  | 83 | # fetch optional input | 
|---|
|  | 84 | stdinput = "< .in/%s.txt" % test if isfile(".in/%s.txt" % test) else "" | 
|---|
|  | 85 |  | 
|---|
|  | 86 | # run test | 
|---|
|  | 87 | sh("./%s %s >> .expect/%s.txt 2>&1" % (test, stdinput, test), False) | 
|---|
|  | 88 |  | 
|---|
|  | 89 | # clean the executable | 
|---|
|  | 90 | sh("rm -f %s" % test, False) | 
|---|
|  | 91 |  | 
|---|
|  | 92 | def generate_expect(tests) : | 
|---|
|  | 93 | sh('make clean > /dev/null 2>&1', False) | 
|---|
|  | 94 |  | 
|---|
|  | 95 | print( "Regenerate tests for" ) | 
|---|
|  | 96 |  | 
|---|
|  | 97 | print( ", ".join( tests ) ) | 
|---|
|  | 98 |  | 
|---|
|  | 99 | for t in tests: | 
|---|
|  | 100 | generate_test_expect( t ) | 
|---|
|  | 101 |  | 
|---|
|  | 102 | print( "Done" ) | 
|---|
|  | 103 |  | 
|---|
|  | 104 | sh('make clean > /dev/null 2>&1', False) | 
|---|
|  | 105 |  | 
|---|
|  | 106 | ################################################################################ | 
|---|
|  | 107 | #               main loop | 
|---|
|  | 108 | ################################################################################ | 
|---|
|  | 109 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') | 
|---|
|  | 110 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') | 
|---|
|  | 111 | parser.add_argument('--all', help='Run all test available', action='store_true') | 
|---|
|  | 112 | parser.add_argument('--generate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') | 
|---|
|  | 113 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') | 
|---|
|  | 114 |  | 
|---|
|  | 115 | options = parser.parse_args() | 
|---|
|  | 116 |  | 
|---|
|  | 117 | tests = listTests() | 
|---|
|  | 118 |  | 
|---|
|  | 119 | if options.all : | 
|---|
|  | 120 | if len(options.tests) > 0 : | 
|---|
|  | 121 | print('ERROR: cannot specify tests with \'--all\' option', file=sys.stderr) | 
|---|
|  | 122 | sys.exit(1) | 
|---|
|  | 123 |  | 
|---|
|  | 124 | if options.generate_expected : | 
|---|
|  | 125 | generate_expect( tests ) | 
|---|
|  | 126 | else : | 
|---|
|  | 127 | sys.exit( run_tests(tests, options.dry_run) ) | 
|---|
|  | 128 |  | 
|---|
|  | 129 | else : | 
|---|
|  | 130 | if len(options.tests) == 0 : | 
|---|
|  | 131 | print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr) | 
|---|
|  | 132 | parser.print_help() | 
|---|
|  | 133 | sys.exit(1) | 
|---|
|  | 134 |  | 
|---|
|  | 135 | unkownTests = False | 
|---|
|  | 136 | for test in options.tests : | 
|---|
|  | 137 | if not (test in tests) : | 
|---|
|  | 138 | print("ERROR: unkown test %s" % test) | 
|---|
|  | 139 | unkownTests = true | 
|---|
|  | 140 |  | 
|---|
|  | 141 | if unkownTests : | 
|---|
|  | 142 | sys.exit(1) | 
|---|
|  | 143 |  | 
|---|
|  | 144 | if options.generate_expected : | 
|---|
|  | 145 | generate_expect( options.tests ) | 
|---|
|  | 146 | else : | 
|---|
|  | 147 | sys.exit( run_tests(options.tests, options.dry_run) ) | 
|---|
|  | 148 |  | 
|---|
|  | 149 | sys.exit(0) | 
|---|