Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import hashlib |
| 4 | import operator |
| 5 | import os |
| 6 | import shutil |
| 7 | import stat |
| 8 | import subprocess |
| 9 | import sys |
| 10 | import tempfile |
| 11 | |
| 12 | |
| 13 | def rel_to_abs(rel_path): |
| 14 | return os.path.join(script_path, rel_path) |
| 15 | |
| 16 | |
| 17 | java_exec = 'java -Xms1024m -server -XX:+TieredCompilation' |
| 18 | tests_dir = 'tests' |
| 19 | jar_name = 'jsdoc_validator.jar' |
| 20 | script_path = os.path.dirname(os.path.abspath(__file__)) |
| 21 | tests_path = rel_to_abs(tests_dir) |
| 22 | validator_jar_file = rel_to_abs(jar_name) |
| 23 | golden_file = os.path.join(tests_path, 'golden.dat') |
| 24 | |
| 25 | test_files = [ |
| 26 | os.path.join(tests_path, f) for f in os.listdir(tests_path) |
| 27 | if f.endswith('.js') and os.path.isfile(os.path.join(tests_path, f)) |
| 28 | ] |
| 29 | |
| 30 | validator_command = "%s -jar %s %s" % (java_exec, validator_jar_file, " ".join(sorted(test_files))) |
| 31 | |
| 32 | |
| 33 | def run_and_communicate(command, error_template): |
| 34 | proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) |
| 35 | (out, _) = proc.communicate() |
| 36 | if proc.returncode: |
| 37 | print >> sys.stderr, error_template % proc.returncode |
| 38 | sys.exit(proc.returncode) |
| 39 | return out |
| 40 | |
| 41 | |
| 42 | def help(): |
| 43 | print 'usage: %s [option]' % os.path.basename(__file__) |
| 44 | print 'Options:' |
| 45 | print '--generate-golden: Re-generate golden file' |
| 46 | print '--dump: Dump the test results to stdout' |
| 47 | |
| 48 | |
| 49 | def main(): |
| 50 | need_golden = False |
| 51 | need_dump = False |
| 52 | if len(sys.argv) > 1: |
| 53 | if sys.argv[1] == '--generate-golden': |
| 54 | need_golden = True |
| 55 | elif sys.argv[1] == '--dump': |
| 56 | need_dump = True |
| 57 | else: |
| 58 | help() |
| 59 | return |
| 60 | |
| 61 | result = run_and_communicate(validator_command, "Error running validator: %d") |
| 62 | result = result.replace(script_path, "") # pylint: disable=E1103 |
| 63 | if need_dump: |
| 64 | print result |
| 65 | return |
| 66 | |
| 67 | if need_golden: |
| 68 | with open(golden_file, 'wt') as golden: |
| 69 | golden.write(result) |
| 70 | else: |
| 71 | with open(golden_file, 'rt') as golden: |
| 72 | golden_text = golden.read() |
| 73 | if golden_text == result: |
| 74 | print 'OK' |
| 75 | else: |
| 76 | print 'ERROR: Golden output mismatch' |
| 77 | |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | main() |