|
| 1 | +# |
| 2 | +# Run the python-flint test-suite as |
| 3 | +# |
| 4 | +# python -m flint.test |
| 5 | +# |
| 6 | + |
| 7 | +import sys |
| 8 | +import doctest |
| 9 | +import traceback |
| 10 | +import argparse |
| 11 | + |
| 12 | +import flint |
| 13 | +from flint.test.test import all_tests |
| 14 | + |
| 15 | + |
| 16 | +def run_tests(verbose=None): |
| 17 | + """Run all tests |
| 18 | +
|
| 19 | + This is usually called by |
| 20 | +
|
| 21 | + $ python -m flint.flint |
| 22 | + """ |
| 23 | + # Show the limited output by default |
| 24 | + if verbose is None: |
| 25 | + verbose = True |
| 26 | + |
| 27 | + total = 0 |
| 28 | + failed = 0 |
| 29 | + |
| 30 | + for test in all_tests: |
| 31 | + |
| 32 | + if verbose: |
| 33 | + print(f'{test.__name__}...', end='', flush=True) |
| 34 | + |
| 35 | + try: |
| 36 | + test() |
| 37 | + except Exception as e: |
| 38 | + print(f'Error in {test.__name__}') |
| 39 | + if verbose: |
| 40 | + traceback.print_exc() |
| 41 | + failed += 1 |
| 42 | + else: |
| 43 | + if verbose: |
| 44 | + print('OK') |
| 45 | + |
| 46 | + total += 1 |
| 47 | + |
| 48 | + return failed, total |
| 49 | + |
| 50 | + |
| 51 | +def run_doctests(verbose=None): |
| 52 | + """Run the python-flint doctests""" |
| 53 | + # Here verbose=True shows a lot of output. |
| 54 | + failed, total = doctest.testmod(flint._flint, verbose=verbose) |
| 55 | + return failed, total |
| 56 | + |
| 57 | + |
| 58 | +def run_all_tests(tests=True, doctests=True, verbose=None): |
| 59 | + |
| 60 | + success = True |
| 61 | + |
| 62 | + if tests: |
| 63 | + print("Running tests...") |
| 64 | + t_failed, t_total = run_tests(verbose=verbose) |
| 65 | + |
| 66 | + if doctests: |
| 67 | + print("Running doctests...") |
| 68 | + d_failed, d_total = run_doctests(verbose=verbose) |
| 69 | + |
| 70 | + if tests: |
| 71 | + if t_failed: |
| 72 | + print(f'flint.test: {t_failed} of {t_total} tests failed') |
| 73 | + success = False |
| 74 | + else: |
| 75 | + print(f'flint.test: all {t_total} tests passed!') |
| 76 | + |
| 77 | + if doctests: |
| 78 | + if d_failed: |
| 79 | + print(f'flint.test: {d_failed} of {d_total} doctests failed') |
| 80 | + success = False |
| 81 | + else: |
| 82 | + print(f'flint.test: all {d_total} doctests passed!') |
| 83 | + |
| 84 | + return success |
| 85 | + |
| 86 | + |
| 87 | +def main(*args): |
| 88 | + """Run the python-flint test-suite""" |
| 89 | + |
| 90 | + parser = argparse.ArgumentParser(description="Run the python-flint test-suite") |
| 91 | + parser.add_argument("--quiet", "-q", action="store_true", help="less verbose output") |
| 92 | + parser.add_argument("--verbose", "-v", action="store_true", help="more verbose output") |
| 93 | + parser.add_argument("--tests", "-t", action="store_true", help="run tests") |
| 94 | + parser.add_argument("--doctests", "-d", action="store_true", help="run doctests") |
| 95 | + args = parser.parse_args(args) |
| 96 | + |
| 97 | + if not args.tests and not args.doctests: |
| 98 | + # Default is run all tests: |
| 99 | + tests = True |
| 100 | + doctests = True |
| 101 | + else: |
| 102 | + # Either --tests or --doctests was specified |
| 103 | + tests = args.tests |
| 104 | + doctests = args.doctests |
| 105 | + |
| 106 | + # Default is show output from tests but keep the doctests quiet. |
| 107 | + if args.verbose: |
| 108 | + verbose = True |
| 109 | + elif args.quiet: |
| 110 | + verbose = False |
| 111 | + else: |
| 112 | + verbose = None |
| 113 | + |
| 114 | + success = run_all_tests(tests=tests, doctests=doctests, verbose=verbose) |
| 115 | + |
| 116 | + if not success: |
| 117 | + print("----------------------------------------") |
| 118 | + print("!!!FAILED!!!: Something is wrong with your installation of python-flint!") |
| 119 | + print("----------------------------------------") |
| 120 | + return 1 |
| 121 | + else: |
| 122 | + print("----------------------------------------") |
| 123 | + print("OK: Your installation of python-flint seems to be working just fine!") |
| 124 | + print("----------------------------------------") |
| 125 | + return 0 |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + sys.exit(main(*sys.argv[1:])) |
0 commit comments