cli.py 4.05 KB
Newer Older
Johannes Bechberger's avatar
Johannes Bechberger committed
1
import logging
2
import os
Johannes Bechberger's avatar
Johannes Bechberger committed
3
4
import sys
import argparse
5
from typing import Dict
6
from mjtest.environment import TestMode, Environment, TEST_MODES
Johannes Bechberger's avatar
Johannes Bechberger committed
7
8
9
from mjtest.test.tests import TestSuite

# adapted from http://stackoverflow.com/a/8527629
10
11
12
from mjtest.util.utils import cprint, colored


Johannes Bechberger's avatar
Johannes Bechberger committed
13
14
class LogLevelChoices(argparse.Action):
    CHOICES = ["error", "warn", "info", "debug"]
15
16
17
18
19
20
21
    def __call__(self, parser, namespace, value, option_string=None):
        if value:
            if value not in self.CHOICES:
                message = ("invalid choice: {0!r} (choose from {1})"
                           .format(value,
                                   ', '.join([repr(action)
                                              for action in self.CHOICES])))
Johannes Bechberger's avatar
Johannes Bechberger committed
22

23
24
                raise argparse.ArgumentError(self, message)
            setattr(namespace, self.dest, value)
Johannes Bechberger's avatar
Johannes Bechberger committed
25
26
27
28

if True:#__name__ == '__main__':
    parser = argparse.ArgumentParser(description="MiniJava test runner", add_help=True)
    parser.add_argument("mode",
29
                        choices=["all"] + TEST_MODES,
Johannes Bechberger's avatar
Johannes Bechberger committed
30
                        help="What do you want to test?")
31
32
33
34
35
    if os.getenv("MJ_RUN", None) is None:
        parser.add_argument("mj_run",
                            metavar="MJ_RUN",
                            help="Command to run your MiniJava implementation, e.g. `mj/run`, "
                                 "can be omitted by assigning the environment variable MJ_RUN")
36
37
38
39
    #parser.add_argument("--tmp_dir", action="store_const", default="", const="tmp_dir",
    #                    help="Used temporary directory")
    #parser.add_argument("--test_dir", action="store_const", default="", const="test_dir",
    #                    help="Directory that contains all test cases, default is the 'tests' directory")
Johannes Bechberger's avatar
Johannes Bechberger committed
40
41
    parser.add_argument("--only_incorrect_tests", action="store_true", default=False,
                        help="Only run the tests that were incorrect the last run")
42
43
    parser.add_argument("--produce_no_reports", action="store_true", default=False,
                        help="Produce no long reports besides the command line output")
44
45
    parser.add_argument("--produce_all_reports", action="store_true", default=False,
                        help="Produce reports also for correct test cases")
Johannes Bechberger's avatar
Johannes Bechberger committed
46
47
    parser.add_argument("--parallel", action="store_true", default=False,
                        help="Run the tests in parallel")
48
    parser.add_argument("--output_no_incorrect_reports", action="store_true", default=False,
49
                        help="Output the long report for every incorrect test case")
50
51
52
53
    #parser.add_argument("--timeout", action="store_const", default=30, const="timeout",
    #                    help="Abort a program after TIMEOUT seconds")
    #parser.add_argument("--report_dir", action="store_const", default="", const="report_dir",
    #                    help="Directory to store the reports in, default is 'reports'")
Johannes Bechberger's avatar
Johannes Bechberger committed
54
55
    parser.add_argument("--log_level", action=LogLevelChoices, default="warn", const="log_level",
                        help="Logging level (error, warn, info or debug)")
56
    args = vars(parser.parse_args())
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    failed = 0
    count = 0

    def run(args: Dict[str, str]):
        global failed, count
        if os.getenv("MJ_RUN", None) is not None:
            args["mj_run"] = os.getenv("MJ_RUN")
        suite = TestSuite(Environment(**args))
        ret = None
        try:
            ret = suite.run()
        finally:
            suite.env.clean_up()
            suite.store()
        if ret is not None:
            failed += ret.failed
            count += ret.count
    if args["mode"] == "all":
        for mode in TEST_MODES:
            cprint("Run {} tests".format(mode), attrs=["bold"])
            args["mode"] = mode
            run(args)
        if failed > 0:  # some tests failed
            print(colored("Ran {} tests, of which ".format(count), "red") +
                  colored("{} failed.".format(failed), "red", attrs=["bold"]))
        else:
            cprint("All {} run tests succeeded".format(count), "green")
Johannes Bechberger's avatar
Johannes Bechberger committed
85
    else:
86
87
        run(args)
    sys.exit(1 if failed > 0 else 0)