| 1 | #!python
|
|---|
| 2 | from __future__ import print_function
|
|---|
| 3 |
|
|---|
| 4 | from os import listdir
|
|---|
| 5 | from os.path import isfile, join, splitext
|
|---|
| 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 = [splitext(f)[0] for f in listdir('./.expect')
|
|---|
| 16 | if not f.startswith('.') and f.endswith('.txt')
|
|---|
| 17 | ]
|
|---|
| 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 | ################################################################################
|
|---|
| 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)
|
|---|
| 38 | sh("rm -f %s > /dev/null 2>&1" % test, dry_run)
|
|---|
| 39 |
|
|---|
| 40 | # build, skipping to next test on error
|
|---|
| 41 | make_ret = sh("make -j 8 %s 2> %s 1> /dev/null" % (test, out_file), dry_run)
|
|---|
| 42 |
|
|---|
| 43 | if make_ret == 0 :
|
|---|
| 44 | # fetch optional input
|
|---|
| 45 | stdinput = "< .in/%s.txt" % test if isfile(".in/%s.txt" % test) else ""
|
|---|
| 46 |
|
|---|
| 47 | # run test
|
|---|
| 48 | sh("./%s %s > %s 2>&1" % (test, stdinput, out_file), dry_run)
|
|---|
| 49 |
|
|---|
| 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)
|
|---|
| 54 |
|
|---|
| 55 | # clean the executable
|
|---|
| 56 | sh("rm -f %s > /dev/null 2>&1" % test, dry_run)
|
|---|
| 57 |
|
|---|
| 58 | return retcode
|
|---|
| 59 |
|
|---|
| 60 | def run_tests(tests, generate, dry_run) :
|
|---|
| 61 | sh('make clean > /dev/null 2>&1', dry_run)
|
|---|
| 62 | sh('mkdir -p .out .expect', dry_run)
|
|---|
| 63 |
|
|---|
| 64 | if generate :
|
|---|
| 65 | print( "Regenerate tests for: " )
|
|---|
| 66 |
|
|---|
| 67 | failed = False;
|
|---|
| 68 | for t in tests:
|
|---|
| 69 | print("%20s " % t, end="")
|
|---|
| 70 | sys.stdout.flush()
|
|---|
| 71 | test_failed = run_test_instance(t, generate, dry_run)
|
|---|
| 72 | failed = test_failed or failed
|
|---|
| 73 |
|
|---|
| 74 | if not generate :
|
|---|
| 75 | print("FAILED" if test_failed else "PASSED")
|
|---|
| 76 | else :
|
|---|
| 77 | print( "Done" )
|
|---|
| 78 |
|
|---|
| 79 | sh('make clean > /dev/null 2>&1', dry_run)
|
|---|
| 80 |
|
|---|
| 81 | return 1 if failed else 0
|
|---|
| 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')
|
|---|
| 88 | parser.add_argument('--list', help='List all test available', action='store_true')
|
|---|
| 89 | parser.add_argument('--all', help='Run all test available', action='store_true')
|
|---|
| 90 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true')
|
|---|
| 91 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run')
|
|---|
| 92 |
|
|---|
| 93 | options = parser.parse_args()
|
|---|
| 94 |
|
|---|
| 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) :
|
|---|
| 97 | print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr)
|
|---|
| 98 | parser.print_help()
|
|---|
| 99 | sys.exit(1)
|
|---|
| 100 |
|
|---|
| 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:
|
|---|
| 109 | if test in allTests :
|
|---|
| 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))
|
|---|
| 120 |
|
|---|
| 121 | else :
|
|---|
| 122 | sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run) )
|
|---|