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