|
| 1 | +''' |
| 2 | +Golden tests for OPT frontend JS rendering |
| 3 | +Created on 2014-04-21 |
| 4 | +
|
| 5 | +Pre-reqs: |
| 6 | + phantomjs installed (tested on v1.9.2) |
| 7 | + OPT running locally on localhost:8080 using "python bottle_server.py" |
| 8 | + must run from the current directory |
| 9 | +
|
| 10 | +TODO: |
| 11 | + for fuzzy image diffing, consider: |
| 12 | + http://www.imagemagick.org/Usage/compare/ |
| 13 | +
|
| 14 | +--- |
| 15 | +
|
| 16 | +Forked from ../golden_test.py |
| 17 | +
|
| 18 | +A simple framework for regression testing based on golden files |
| 19 | +by Philip Guo |
| 20 | +
|
| 21 | +(sloppily) customized for the Online Python Tutor project |
| 22 | +''' |
| 23 | + |
| 24 | +# test with different display options |
| 25 | +OPTIONS_DICT = { |
| 26 | + 'regular': 'cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&py=2', |
| 27 | + 'csc108h': 'cumulative=false&heapPrimitives=true&drawParentPointers=false&textReferences=true&py=2', |
| 28 | + 'composingprograms': 'cumulative=true&heapPrimitives=false&drawParentPointers=false&textReferences=false&py=3', |
| 29 | +} |
| 30 | + |
| 31 | +import os, re, shutil, optparse, filecmp |
| 32 | +from subprocess import * |
| 33 | + |
| 34 | +RED = '\033[91m' |
| 35 | +ENDC = '\033[0m' # end color |
| 36 | + |
| 37 | + |
| 38 | +def execute(input_filename, option_name): |
| 39 | + assert os.path.isfile(input_filename) |
| 40 | + (base, ext) = os.path.splitext(input_filename) |
| 41 | + assert ext == INPUT_FILE_EXTENSION |
| 42 | + |
| 43 | + (stdout, stderr) = Popen(PROGRAM + [input_filename, 'localhost:8080', OPTIONS_DICT[option_name]], |
| 44 | + stdout=PIPE, stderr=PIPE).communicate() |
| 45 | + |
| 46 | + |
| 47 | +def clobber_golden_file(outfile, golden_file): |
| 48 | + print ' Clobber %s => %s' % (outfile, golden_file) |
| 49 | + shutil.copy(outfile, golden_file) |
| 50 | + |
| 51 | + |
| 52 | +def run_test(input_filename, clobber_golden=False): |
| 53 | + print 'Testing', input_filename |
| 54 | + bn = os.path.basename(input_filename) |
| 55 | + |
| 56 | + # test all frontend display options |
| 57 | + for option in OPTIONS_DICT: |
| 58 | + print " " + option |
| 59 | + execute(input_filename, option) |
| 60 | + |
| 61 | + output_png_files = [e for e in os.listdir('.') if bn + '.step.' in e] |
| 62 | + for e in output_png_files: |
| 63 | + golden_file = 'golden-files/' + option + '/' + e |
| 64 | + |
| 65 | + if os.path.isfile(golden_file): |
| 66 | + if not filecmp.cmp(e, golden_file): |
| 67 | + print " " + RED + e + " differs!" + ENDC |
| 68 | + if clobber_golden: |
| 69 | + clobber_golden_file(e, golden_file) |
| 70 | + else: |
| 71 | + clobber_golden_file(e, golden_file) |
| 72 | + |
| 73 | + # do a crude cleanup pass at the very end of the test |
| 74 | + # to not interfere as much with Dropbox's weird behavior |
| 75 | + for e in os.listdir('.'): |
| 76 | + if '.step.' in e: |
| 77 | + os.remove(e) |
| 78 | + |
| 79 | + |
| 80 | +def run_all_tests(clobber=False): |
| 81 | + for t in ALL_TESTS: |
| 82 | + run_test(t, clobber) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + parser = optparse.OptionParser() |
| 87 | + parser.add_option("--all", action="store_true", dest="run_all", |
| 88 | + help="Run all tests") |
| 89 | + parser.add_option("--clobber", action="store_true", dest="clobber", |
| 90 | + help="Clobber golden files when running tests") |
| 91 | + parser.add_option("--test", dest="test_name", |
| 92 | + help="Run one test") |
| 93 | + (options, args) = parser.parse_args() |
| 94 | + |
| 95 | + INPUT_FILE_EXTENSION = '.txt' # input test files are .txt, NOT .py |
| 96 | + |
| 97 | + PROGRAM = ['phantomjs', '../../screenshot-renderer/render-opt-screenshots.js'] |
| 98 | + |
| 99 | + # grab a representative subset of backend tests |
| 100 | + # don't need ALL of them since they run way too slowly |
| 101 | + ALL_TESTS = ['../example-code/aliasing.txt', |
| 102 | + '../example-code/fact.txt', |
| 103 | + '../example-code/filter.txt', |
| 104 | + '../example-code/aliasing/aliasing8.txt', |
| 105 | + '../example-code/oop_inherit.txt', |
| 106 | + '../example-code/linked-lists/ll1.txt', |
| 107 | + '../example-code/sum-list.txt', |
| 108 | + '../example-code/closures/closure3.txt', |
| 109 | + ] |
| 110 | + |
| 111 | + if options.run_all: |
| 112 | + if options.clobber: |
| 113 | + print 'Running all tests and clobbering results ...' |
| 114 | + else: |
| 115 | + print 'Running all tests ...' |
| 116 | + run_all_tests(options.clobber) |
| 117 | + elif options.test_name: |
| 118 | + assert options.test_name in ALL_TESTS |
| 119 | + run_test(options.test_name, options.clobber) |
| 120 | + else: |
| 121 | + parser.print_help() |
| 122 | + |
0 commit comments