| 1 | |
|---|
| 2 | import sys, os, os.path, types, traceback, pprint |
|---|
| 3 | |
|---|
| 4 | DATA = 'tests/data' |
|---|
| 5 | |
|---|
| 6 | def find_test_functions(collections): |
|---|
| 7 | if not isinstance(collections, list): |
|---|
| 8 | collections = [collections] |
|---|
| 9 | functions = [] |
|---|
| 10 | for collection in collections: |
|---|
| 11 | if not isinstance(collection, dict): |
|---|
| 12 | collection = vars(collection) |
|---|
| 13 | for key in sorted(collection): |
|---|
| 14 | value = collection[key] |
|---|
| 15 | if isinstance(value, types.FunctionType) and hasattr(value, 'unittest'): |
|---|
| 16 | functions.append(value) |
|---|
| 17 | return functions |
|---|
| 18 | |
|---|
| 19 | def find_test_filenames(directory): |
|---|
| 20 | filenames = {} |
|---|
| 21 | for filename in os.listdir(directory): |
|---|
| 22 | if os.path.isfile(os.path.join(directory, filename)): |
|---|
| 23 | base, ext = os.path.splitext(filename) |
|---|
| 24 | if base.endswith('-py2'): |
|---|
| 25 | continue |
|---|
| 26 | filenames.setdefault(base, []).append(ext) |
|---|
| 27 | filenames = sorted(filenames.items()) |
|---|
| 28 | return filenames |
|---|
| 29 | |
|---|
| 30 | def parse_arguments(args): |
|---|
| 31 | if args is None: |
|---|
| 32 | args = sys.argv[1:] |
|---|
| 33 | verbose = False |
|---|
| 34 | if '-v' in args: |
|---|
| 35 | verbose = True |
|---|
| 36 | args.remove('-v') |
|---|
| 37 | if '--verbose' in args: |
|---|
| 38 | verbose = True |
|---|
| 39 | if 'YAML_TEST_VERBOSE' in os.environ: |
|---|
| 40 | verbose = True |
|---|
| 41 | include_functions = [] |
|---|
| 42 | if args: |
|---|
| 43 | include_functions.append(args.pop(0)) |
|---|
| 44 | if 'YAML_TEST_FUNCTIONS' in os.environ: |
|---|
| 45 | include_functions.extend(os.environ['YAML_TEST_FUNCTIONS'].split()) |
|---|
| 46 | include_filenames = [] |
|---|
| 47 | include_filenames.extend(args) |
|---|
| 48 | if 'YAML_TEST_FILENAMES' in os.environ: |
|---|
| 49 | include_filenames.extend(os.environ['YAML_TEST_FILENAMES'].split()) |
|---|
| 50 | return include_functions, include_filenames, verbose |
|---|
| 51 | |
|---|
| 52 | def execute(function, filenames, verbose): |
|---|
| 53 | name = function.__name__ |
|---|
| 54 | if verbose: |
|---|
| 55 | sys.stdout.write('='*75+'\n') |
|---|
| 56 | sys.stdout.write('%s(%s)...\n' % (name, ', '.join(filenames))) |
|---|
| 57 | try: |
|---|
| 58 | function(verbose=verbose, *filenames) |
|---|
| 59 | except Exception as exc: |
|---|
| 60 | info = sys.exc_info() |
|---|
| 61 | if isinstance(exc, AssertionError): |
|---|
| 62 | kind = 'FAILURE' |
|---|
| 63 | else: |
|---|
| 64 | kind = 'ERROR' |
|---|
| 65 | if verbose: |
|---|
| 66 | traceback.print_exc(limit=1, file=sys.stdout) |
|---|
| 67 | else: |
|---|
| 68 | sys.stdout.write(kind[0]) |
|---|
| 69 | sys.stdout.flush() |
|---|
| 70 | else: |
|---|
| 71 | kind = 'SUCCESS' |
|---|
| 72 | info = None |
|---|
| 73 | if not verbose: |
|---|
| 74 | sys.stdout.write('.') |
|---|
| 75 | sys.stdout.flush() |
|---|
| 76 | return (name, filenames, kind, info) |
|---|
| 77 | |
|---|
| 78 | def display(results, verbose): |
|---|
| 79 | if results and not verbose: |
|---|
| 80 | sys.stdout.write('\n') |
|---|
| 81 | total = len(results) |
|---|
| 82 | failures = 0 |
|---|
| 83 | errors = 0 |
|---|
| 84 | for name, filenames, kind, info in results: |
|---|
| 85 | if kind == 'SUCCESS': |
|---|
| 86 | continue |
|---|
| 87 | if kind == 'FAILURE': |
|---|
| 88 | failures += 1 |
|---|
| 89 | if kind == 'ERROR': |
|---|
| 90 | errors += 1 |
|---|
| 91 | sys.stdout.write('='*75+'\n') |
|---|
| 92 | sys.stdout.write('%s(%s): %s\n' % (name, ', '.join(filenames), kind)) |
|---|
| 93 | if kind == 'ERROR': |
|---|
| 94 | traceback.print_exception(file=sys.stdout, *info) |
|---|
| 95 | else: |
|---|
| 96 | sys.stdout.write('Traceback (most recent call last):\n') |
|---|
| 97 | traceback.print_tb(info[2], file=sys.stdout) |
|---|
| 98 | sys.stdout.write('%s: see below\n' % info[0].__name__) |
|---|
| 99 | sys.stdout.write('~'*75+'\n') |
|---|
| 100 | for arg in info[1].args: |
|---|
| 101 | pprint.pprint(arg, stream=sys.stdout) |
|---|
| 102 | for filename in filenames: |
|---|
| 103 | sys.stdout.write('-'*75+'\n') |
|---|
| 104 | sys.stdout.write('%s:\n' % filename) |
|---|
| 105 | data = open(filename, 'r', errors='replace').read() |
|---|
| 106 | sys.stdout.write(data) |
|---|
| 107 | if data and data[-1] != '\n': |
|---|
| 108 | sys.stdout.write('\n') |
|---|
| 109 | sys.stdout.write('='*75+'\n') |
|---|
| 110 | sys.stdout.write('TESTS: %s\n' % total) |
|---|
| 111 | if failures: |
|---|
| 112 | sys.stdout.write('FAILURES: %s\n' % failures) |
|---|
| 113 | if errors: |
|---|
| 114 | sys.stdout.write('ERRORS: %s\n' % errors) |
|---|
| 115 | |
|---|
| 116 | def run(collections, args=None): |
|---|
| 117 | test_functions = find_test_functions(collections) |
|---|
| 118 | test_filenames = find_test_filenames(DATA) |
|---|
| 119 | include_functions, include_filenames, verbose = parse_arguments(args) |
|---|
| 120 | results = [] |
|---|
| 121 | for function in test_functions: |
|---|
| 122 | if include_functions and function.__name__ not in include_functions: |
|---|
| 123 | continue |
|---|
| 124 | if function.unittest: |
|---|
| 125 | for base, exts in test_filenames: |
|---|
| 126 | if include_filenames and base not in include_filenames: |
|---|
| 127 | continue |
|---|
| 128 | filenames = [] |
|---|
| 129 | for ext in function.unittest: |
|---|
| 130 | if ext not in exts: |
|---|
| 131 | break |
|---|
| 132 | filenames.append(os.path.join(DATA, base+ext)) |
|---|
| 133 | else: |
|---|
| 134 | skip_exts = getattr(function, 'skip', []) |
|---|
| 135 | for skip_ext in skip_exts: |
|---|
| 136 | if skip_ext in exts: |
|---|
| 137 | break |
|---|
| 138 | else: |
|---|
| 139 | result = execute(function, filenames, verbose) |
|---|
| 140 | results.append(result) |
|---|
| 141 | else: |
|---|
| 142 | result = execute(function, [], verbose) |
|---|
| 143 | results.append(result) |
|---|
| 144 | display(results, verbose=verbose) |
|---|
| 145 | |
|---|