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, generate, dry_run):
|
---|
35 |
|
---|
36 | out_file = (".out/%s.log" % test) if not generate else (".expect/%s.txt" % test)
|
---|
37 |
|
---|
38 | sh("rm -f %s" % out_file, dry_run)
|
---|
39 | sh("rm -f %s > /dev/null 2>&1" % test, dry_run)
|
---|
40 |
|
---|
41 | # build, skipping to next test on error
|
---|
42 | make_ret = sh("make -j 8 %s > %s 2>&1" % (test, out_file), dry_run)
|
---|
43 |
|
---|
44 | if make_ret == 0 :
|
---|
45 | # fetch optional input
|
---|
46 | stdinput = "< .in/%s.txt" % test if isfile(".in/%s.txt" % test) else ""
|
---|
47 |
|
---|
48 | # run test
|
---|
49 | sh("./%s %s > %s 2>&1" % (test, stdinput, out_file), dry_run)
|
---|
50 |
|
---|
51 | retcode = 0
|
---|
52 | if not generate :
|
---|
53 | # touch expected files so empty output are supported by default
|
---|
54 | sh("touch .expect/%s.txt" % test, dry_run)
|
---|
55 |
|
---|
56 | # diff the output of the files
|
---|
57 | retcode = sh("diff .expect/%s.txt .out/%s.log" % (test, test), dry_run)
|
---|
58 |
|
---|
59 | # clean the executable
|
---|
60 | sh("rm -f %s > /dev/null 2>&1" % test, dry_run)
|
---|
61 |
|
---|
62 | return retcode
|
---|
63 |
|
---|
64 | def run_tests(tests, generate, dry_run) :
|
---|
65 | sh('make clean > /dev/null 2>&1', dry_run)
|
---|
66 | sh('mkdir -p .out .expect', dry_run)
|
---|
67 |
|
---|
68 | if generate :
|
---|
69 | print( "Regenerate tests for: ", end="" )
|
---|
70 | print( ", ".join( tests ) )
|
---|
71 |
|
---|
72 | failed = False;
|
---|
73 | for t in tests:
|
---|
74 | if not generate :
|
---|
75 | print("%20s " % t, end="")
|
---|
76 | sys.stdout.flush()
|
---|
77 | test_failed = run_test_instance(t, generate, dry_run)
|
---|
78 | failed = test_failed or failed
|
---|
79 |
|
---|
80 | if not generate :
|
---|
81 | print("FAILED" if test_failed else "PASSED")
|
---|
82 |
|
---|
83 | sh('make clean > /dev/null 2>&1', dry_run)
|
---|
84 |
|
---|
85 | if generate :
|
---|
86 | print( "Done" )
|
---|
87 |
|
---|
88 | return 0 if failed else 1
|
---|
89 |
|
---|
90 | ################################################################################
|
---|
91 | # main loop
|
---|
92 | ################################################################################
|
---|
93 | parser = argparse.ArgumentParser(description='Script which runs cforall tests')
|
---|
94 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true')
|
---|
95 | parser.add_argument('--all', help='Run all test available', action='store_true')
|
---|
96 | parser.add_argument('--generate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true')
|
---|
97 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run')
|
---|
98 |
|
---|
99 | options = parser.parse_args()
|
---|
100 |
|
---|
101 | if len(options.tests) > 0 and options.all :
|
---|
102 | print('ERROR: must have option \'--all\' or non-empty test list', file=sys.stderr)
|
---|
103 | parser.print_help()
|
---|
104 | sys.exit(1)
|
---|
105 |
|
---|
106 | tests = listTests() if options.all else options.tests
|
---|
107 |
|
---|
108 | sys.exit( run_tests(tests, options.generate_expected, options.dry_run) )
|
---|