|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import ast |
| 5 | +import difflib |
| 6 | +import tempfile |
| 7 | +import unittest |
| 8 | +import astpretty |
| 9 | +import subprocess |
| 10 | + |
| 11 | +def test_file(path): |
| 12 | + with open(path) as f: |
| 13 | + expected_ast = astpretty.pformat(ast.parse(f.read()), show_position=False) |
| 14 | + printer_output = subprocess.check_output(['cargo', 'run', path]) |
| 15 | + received_ast = astpretty.pformat(ast.parse(printer_output), show_position=False) |
| 16 | + if expected_ast == received_ast: |
| 17 | + print('{}: ok'.format(path)) |
| 18 | + return |
| 19 | + print('========================') |
| 20 | + print(path) |
| 21 | + print('------------------------') |
| 22 | + #for line in difflib.unified_diff(received_ast, expected_ast): # OMG so slow |
| 23 | + # print(line) |
| 24 | + with tempfile.NamedTemporaryFile('w+', prefix='expected-') as exp_file, \ |
| 25 | + tempfile.NamedTemporaryFile('w+', prefix='received-') as received_file: |
| 26 | + exp_file.write(expected_ast) |
| 27 | + exp_file.seek(0) |
| 28 | + received_file.write(received_ast) |
| 29 | + received_file.seek(0) |
| 30 | + try: |
| 31 | + subprocess.check_output( |
| 32 | + ['diff', '-u', received_file.name, exp_file.name], |
| 33 | + universal_newlines=True) |
| 34 | + except subprocess.CalledProcessError as e: |
| 35 | + print(e.output) |
| 36 | + else: |
| 37 | + assert False, 'diff did not detect a different, but should have.' |
| 38 | + print('========================') |
| 39 | + |
| 40 | +def test_dir(path): |
| 41 | + for (dirpath, dirname, filenames) in os.walk(path): |
| 42 | + if any(x.startswith('.') for x in dirpath.split(os.path.sep)): |
| 43 | + # dotfile |
| 44 | + continue |
| 45 | + for filename in filenames: |
| 46 | + if os.path.splitext(filename)[1] == '.py': |
| 47 | + test_file(os.path.join(dirpath, filename)) |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + for path in sys.argv[1:]: |
| 53 | + if os.path.isfile(path): |
| 54 | + test_file(path) |
| 55 | + elif os.path.isdir(path): |
| 56 | + test_dir(path) |
| 57 | + else: |
| 58 | + print('Error: no such file or directory: {}'.format(path)) |
| 59 | + exit(1) |
| 60 | + |
| 61 | +if __name__ == '__main__': |
| 62 | + main() |
0 commit comments