|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import glob |
| 6 | +import difflib |
| 7 | +import argparse |
| 8 | +import subprocess |
| 9 | +import multiprocessing |
| 10 | + |
| 11 | +TEST_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 12 | +SCRIPTS_DIR = os.path.realpath(os.path.join(TEST_DIR, '..', 'scripts')) |
| 13 | + |
| 14 | +def decompyle_one(test_name, pyc_file, outdir, tokenized_expect): |
| 15 | + out_base = os.path.join(outdir, os.path.basename(pyc_file)) |
| 16 | + proc = subprocess.run( |
| 17 | + [os.path.join(os.getcwd(), 'pycdc'), pyc_file, '-o', out_base + '.src.py'], |
| 18 | + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, |
| 19 | + encoding='utf-8', errors='replace') |
| 20 | + pycdc_output = proc.stdout |
| 21 | + if proc.returncode != 0 or pycdc_output: |
| 22 | + with open(out_base + '.err', 'w') as errfile: |
| 23 | + errfile.write(pycdc_output) |
| 24 | + return False, [pycdc_output] |
| 25 | + elif os.path.exists(out_base + '.err'): |
| 26 | + os.unlink(out_base + '.err') |
| 27 | + |
| 28 | + proc = subprocess.run( |
| 29 | + [sys.executable, os.path.join(SCRIPTS_DIR, 'token_dump'), out_base + '.src.py'], |
| 30 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, |
| 31 | + encoding='utf-8', errors='replace') |
| 32 | + tokenized = proc.stdout |
| 33 | + token_dump_err = proc.stderr |
| 34 | + with open(out_base + '.tok.txt', 'w') as tokfile: |
| 35 | + tokfile.write(tokenized) |
| 36 | + if proc.returncode != 0 or token_dump_err: |
| 37 | + with open(out_base + '.tok.err', 'w') as errfile: |
| 38 | + errfile.write(token_dump_err) |
| 39 | + return False, [token_dump_err] |
| 40 | + elif os.path.exists(out_base + '.tok.err'): |
| 41 | + os.unlink(out_base + '.tok.err') |
| 42 | + |
| 43 | + if tokenized != tokenized_expect: |
| 44 | + fromfile = 'tokenized/{}.txt'.format(test_name) |
| 45 | + tofile = 'tests-out/{}.tok.txt'.format(os.path.basename(pyc_file)) |
| 46 | + diff = difflib.unified_diff(tokenized_expect.splitlines(True), tokenized.splitlines(True), |
| 47 | + fromfile=fromfile, tofile=tofile) |
| 48 | + diff = list(diff) |
| 49 | + with open(out_base + '.tok.diff', 'w') as diff_file: |
| 50 | + diff_file.writelines(diff) |
| 51 | + return False, ['Tokenized output does not match expected output:\n'] + diff |
| 52 | + |
| 53 | + return True, [] |
| 54 | + |
| 55 | + |
| 56 | +def run_test(test_file): |
| 57 | + """ |
| 58 | + Runs a single test, and returns a tuple containing the number of failed |
| 59 | + tests and the output of the test. The output is not printed directly |
| 60 | + in order to avoid interleaving output from multiple parallel tests. |
| 61 | + """ |
| 62 | + test_name = os.path.splitext(os.path.basename(test_file))[0] |
| 63 | + compiled_files = glob.glob(os.path.join(TEST_DIR, 'compiled', test_name + '.?.*.pyc')) |
| 64 | + xfail_files = glob.glob(os.path.join(TEST_DIR, 'xfail', test_name + '.?.*.pyc')) |
| 65 | + if not compiled_files and not xfail_files: |
| 66 | + return 1, 'No compiled/xfail modules found for {}\n'.format(test_name) |
| 67 | + |
| 68 | + outdir = os.path.join(os.getcwd(), 'tests-out') |
| 69 | + os.makedirs(outdir, exist_ok=True) |
| 70 | + |
| 71 | + with open(os.path.join(TEST_DIR, 'tokenized', test_name + '.txt'), 'r', |
| 72 | + encoding='utf-8', errors='replace') as tok_file: |
| 73 | + tokenized_expect = tok_file.read() |
| 74 | + |
| 75 | + status_line = '\033[1m*** {}:\033[0m '.format(test_name) |
| 76 | + errlines = [] |
| 77 | + fails = 0 |
| 78 | + xfails = 0 |
| 79 | + upass = 0 |
| 80 | + for xpass_file in compiled_files: |
| 81 | + ok, errs = decompyle_one(test_name, xpass_file, outdir, tokenized_expect) |
| 82 | + if not ok: |
| 83 | + fails += 1 |
| 84 | + errlines.append('\t\033[31m{}\033[0m\n'.format(os.path.basename(xpass_file))) |
| 85 | + errlines.extend(errs) |
| 86 | + for xfail_file in xfail_files: |
| 87 | + ok, _ = decompyle_one(test_name, xfail_file, outdir, tokenized_expect) |
| 88 | + if not ok: |
| 89 | + xfails += 1 |
| 90 | + else: |
| 91 | + upass += 1 |
| 92 | + |
| 93 | + if fails == 0: |
| 94 | + if xfails != 0: |
| 95 | + if not compiled_files: |
| 96 | + status_line += '\033[33mXFAIL ({})\033[0m\n'.format(xfails) |
| 97 | + else: |
| 98 | + status_line += '\033[32mPASS ({})\033[33m + XFAIL ()\033[0m\n' \ |
| 99 | + .format(len(compiled_files), xfails) |
| 100 | + else: |
| 101 | + status_line += '\033[32mPASS ({})\033[0m\n'.format(len(compiled_files)) |
| 102 | + else: |
| 103 | + if xfails != 0: |
| 104 | + status_line += '\033[31mFAIL ({} of {})\033[33m + XFAIL ({})\033[0m\n' \ |
| 105 | + .format(fails, len(compiled_files), xfails) |
| 106 | + else: |
| 107 | + status_line += '\033[31mFAIL ({} of {})\033[0m\n'.format(fails, len(compiled_files)) |
| 108 | + |
| 109 | + return fails, [status_line] + errlines |
| 110 | + |
| 111 | + |
| 112 | +def main(): |
| 113 | + # For simpler invocation from CMake's check target, we also support setting |
| 114 | + # these parameters via environment variables. |
| 115 | + default_jobs = int(os.environ['JOBS']) if 'JOBS' in os.environ else multiprocessing.cpu_count() |
| 116 | + default_filter = os.environ['FILTER'] if 'FILTER' in os.environ else '' |
| 117 | + |
| 118 | + parser = argparse.ArgumentParser() |
| 119 | + parser.add_argument('--jobs', '-j', type=int, default=default_jobs, |
| 120 | + help='Number of tests to run in parallel (default: {})'.format(default_jobs)) |
| 121 | + parser.add_argument('--filter', type=str, default=default_filter, |
| 122 | + help='Run only test(s) matching the supplied filter') |
| 123 | + args = parser.parse_args() |
| 124 | + |
| 125 | + glob_pattern = '*{}*.txt'.format(args.filter) if args.filter else '*.txt' |
| 126 | + test_files = sorted(glob.iglob(os.path.join(TEST_DIR, 'tokenized', glob_pattern))) |
| 127 | + total_fails = 0 |
| 128 | + with multiprocessing.Pool(args.jobs) as pool: |
| 129 | + for fails, output in pool.imap(run_test, test_files): |
| 130 | + total_fails += fails |
| 131 | + sys.stdout.writelines(output) |
| 132 | + |
| 133 | + if total_fails: |
| 134 | + print('{} test(s) failed'.format(total_fails)) |
| 135 | + sys.exit(1) |
| 136 | + |
| 137 | +if __name__ == '__main__': |
| 138 | + main() |
0 commit comments