|
| 1 | +''' |
| 2 | +A simple framework for regression testing based on golden files |
| 3 | +by Philip Guo |
| 4 | +''' |
| 5 | + |
| 6 | +# find all input files with extension INPUT_FILE_EXTENSION in TESTDIRS |
| 7 | +TESTDIRS = ['tests/'] |
| 8 | +INPUT_FILE_EXTENSION = '.js' |
| 9 | + |
| 10 | +# program to run, with input file as an extra argument |
| 11 | +PROGRAM = ['node', '--expose-debug-as=Debug', 'jslogger.js', '--prettydump=true'] |
| 12 | + |
| 13 | +# this program should output to stdout, which will be redirected to this |
| 14 | +# extension: |
| 15 | +OUTPUT_FILE_EXTENSION = '.out' |
| 16 | + |
| 17 | +# extension for golden files: |
| 18 | +GOLDEN_FILE_EXTENSION = '.golden' |
| 19 | + |
| 20 | + |
| 21 | +import itertools |
| 22 | +import os, re, shutil, optparse, difflib |
| 23 | +from subprocess import * |
| 24 | + |
| 25 | +RED = '\033[91m' |
| 26 | +ENDC = '\033[0m' # end color |
| 27 | + |
| 28 | +def filter_output(s): |
| 29 | + return s |
| 30 | + |
| 31 | + |
| 32 | +def execute(input_filename): |
| 33 | + assert os.path.isfile(input_filename) |
| 34 | + (base, ext) = os.path.splitext(input_filename) |
| 35 | + assert ext == INPUT_FILE_EXTENSION |
| 36 | + |
| 37 | + (stdout, stderr) = Popen(PROGRAM + [input_filename], stdout=PIPE, stderr=PIPE).communicate() |
| 38 | + |
| 39 | + if stderr: |
| 40 | + print '(has stderr)' |
| 41 | + # print ' stderr {' |
| 42 | + # print stderr, '}' |
| 43 | + else: |
| 44 | + print |
| 45 | + |
| 46 | + # capture stdout into outfile, filtering out machine-specific addresses |
| 47 | + outfile = base + OUTPUT_FILE_EXTENSION |
| 48 | + outf = open(outfile, 'w') |
| 49 | + |
| 50 | + for line in stdout.splitlines(): |
| 51 | + filtered_line = re.sub(' 0x.+?>', ' 0xADDR>', line) |
| 52 | + print >> outf, filtered_line |
| 53 | + |
| 54 | + outf.close() |
| 55 | + |
| 56 | + |
| 57 | +def clobber_golden_file(golden_file): |
| 58 | + (base, ext) = os.path.splitext(golden_file) |
| 59 | + outfile = base + OUTPUT_FILE_EXTENSION |
| 60 | + assert os.path.isfile(outfile) |
| 61 | + print ' Clobber %s => %s' % (outfile, golden_file) |
| 62 | + shutil.copy(outfile, golden_file) |
| 63 | + |
| 64 | + |
| 65 | +# returns True if there is a diff, False otherwise |
| 66 | +def golden_differs_from_out(golden_file): |
| 67 | + (base, ext) = os.path.splitext(golden_file) |
| 68 | + outfile = base + OUTPUT_FILE_EXTENSION |
| 69 | + assert os.path.isfile(outfile) |
| 70 | + assert os.path.isfile(golden_file) |
| 71 | + |
| 72 | + golden_s = open(golden_file).readlines() |
| 73 | + out_s = open(outfile).readlines() |
| 74 | + |
| 75 | + golden_s_filtered = filter_output(golden_s) |
| 76 | + out_s_filtered = filter_output(out_s) |
| 77 | + |
| 78 | + return out_s_filtered != golden_s_filtered |
| 79 | + |
| 80 | + |
| 81 | +def diff_test_output(test_name): |
| 82 | + (base, ext) = os.path.splitext(test_name) |
| 83 | + |
| 84 | + golden_file = base + GOLDEN_FILE_EXTENSION |
| 85 | + assert os.path.isfile(golden_file) |
| 86 | + outfile = base + OUTPUT_FILE_EXTENSION |
| 87 | + assert os.path.isfile(outfile) |
| 88 | + |
| 89 | + golden_s = open(golden_file).readlines() |
| 90 | + out_s = open(outfile).readlines() |
| 91 | + |
| 92 | + golden_s_filtered = filter_output(golden_s) |
| 93 | + out_s_filtered = filter_output(out_s) |
| 94 | + |
| 95 | + first_line = True |
| 96 | + for line in difflib.unified_diff(golden_s_filtered, out_s_filtered, \ |
| 97 | + fromfile=golden_file, tofile=outfile): |
| 98 | + if first_line: |
| 99 | + print # print an extra line to ease readability |
| 100 | + first_line = False |
| 101 | + print line, |
| 102 | + |
| 103 | + |
| 104 | +def run_test(input_filename, clobber_golden=False): |
| 105 | + print 'Testing', input_filename, |
| 106 | + |
| 107 | + (base, ext) = os.path.splitext(input_filename) |
| 108 | + assert ext == INPUT_FILE_EXTENSION |
| 109 | + |
| 110 | + # to eliminate possibility of using stale output: |
| 111 | + outfile = base + OUTPUT_FILE_EXTENSION |
| 112 | + if os.path.isfile(outfile): |
| 113 | + os.remove(outfile) |
| 114 | + |
| 115 | + input_fullpath = input_filename |
| 116 | + execute(input_fullpath) |
| 117 | + |
| 118 | + golden_file = base + GOLDEN_FILE_EXTENSION |
| 119 | + if os.path.isfile(golden_file): |
| 120 | + if golden_differs_from_out(golden_file): |
| 121 | + print " " + RED + "FAILED!!!" + ENDC |
| 122 | + if clobber_golden: |
| 123 | + clobber_golden_file(golden_file) |
| 124 | + else: |
| 125 | + clobber_golden_file(golden_file) |
| 126 | + |
| 127 | + |
| 128 | +def run_all_tests(clobber=False): |
| 129 | + for t in ALL_TESTS: |
| 130 | + run_test(t, clobber) |
| 131 | + |
| 132 | +def diff_all_test_outputs(): |
| 133 | + for t in ALL_TESTS: |
| 134 | + diff_test_output(t) |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + parser = optparse.OptionParser() |
| 139 | + parser.add_option("--all", action="store_true", dest="run_all", |
| 140 | + help="Run all tests") |
| 141 | + parser.add_option("--only-clobber", action="store_true", dest="only_clobber", |
| 142 | + help="Clobber ALL golden files WITHOUT re-running tests") |
| 143 | + parser.add_option("--clobber", action="store_true", dest="clobber", |
| 144 | + help="Clobber golden files when running tests") |
| 145 | + parser.add_option("--test", dest="test_name", |
| 146 | + help="Run one test") |
| 147 | + parser.add_option("--difftest", dest="diff_test_name", |
| 148 | + help="Diff against golden file for one test") |
| 149 | + parser.add_option("--diffall", action="store_true", dest="diff_all", |
| 150 | + help="Diff against golden file for all tests") |
| 151 | + (options, args) = parser.parse_args() |
| 152 | + |
| 153 | + ALL_TESTS = [] |
| 154 | + |
| 155 | + for (pwd, subdirs, files) in itertools.chain(*[os.walk(e) for e in TESTDIRS]): |
| 156 | + for f in files: |
| 157 | + (base, ext) = os.path.splitext(f) |
| 158 | + if ext == INPUT_FILE_EXTENSION: |
| 159 | + fullpath = os.path.join(pwd, f) |
| 160 | + ALL_TESTS.append(fullpath) |
| 161 | + |
| 162 | + |
| 163 | + if options.run_all: |
| 164 | + if options.clobber: |
| 165 | + print 'Running all tests and clobbering results ...' |
| 166 | + else: |
| 167 | + print 'Running all tests ...' |
| 168 | + run_all_tests(options.clobber) |
| 169 | + |
| 170 | + elif options.diff_all: |
| 171 | + diff_all_test_outputs() |
| 172 | + elif options.diff_test_name: |
| 173 | + assert options.diff_test_name in ALL_TESTS |
| 174 | + diff_test_output(options.diff_test_name) |
| 175 | + elif options.test_name: |
| 176 | + assert options.test_name in ALL_TESTS |
| 177 | + run_test(options.test_name, options.clobber) |
| 178 | + elif options.only_clobber: |
| 179 | + for t in ALL_TESTS: |
| 180 | + (base, ext) = os.path.splitext(t) |
| 181 | + golden_file = base + GOLDEN_FILE_EXTENSION |
| 182 | + clobber_golden_file(golden_file) |
| 183 | + else: |
| 184 | + parser.print_help() |
| 185 | + |
0 commit comments