cli.py 3.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 mjtest.environment import TestMode, Environment, TEST_MODES
Johannes Bechberger's avatar
Johannes Bechberger committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
from mjtest.test.tests import TestSuite

# adapted from http://stackoverflow.com/a/8527629
class LogLevelChoices(argparse.Action):
    CHOICES = ["error", "warn", "info", "debug"]
    def __call__(self, parser, namespace, values, option_string=None):
        if values:
            for value in values:
                if value not in self.CHOICES:
                    message = ("invalid choice: {0!r} (choose from {1})"
                               .format(value,
                                       ', '.join([repr(action)
                                                  for action in self.CHOICES])))

                    raise argparse.ArgumentError(self, message)
            setattr(namespace, self.dest, values)

if True:#__name__ == '__main__':
    parser = argparse.ArgumentParser(description="MiniJava test runner", add_help=True)
    parser.add_argument("mode",
                        choices=TEST_MODES,
                        help="What do you want to test?")
28 29 30 31 32
    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")
33 34 35 36
    #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
37 38
    parser.add_argument("--only_incorrect_tests", action="store_true", default=False,
                        help="Only run the tests that were incorrect the last run")
39 40
    parser.add_argument("--produce_no_reports", action="store_true", default=False,
                        help="Produce no long reports besides the command line output")
Johannes Bechberger's avatar
Johannes Bechberger committed
41 42
    parser.add_argument("--parallel", action="store_true", default=False,
                        help="Run the tests in parallel")
43 44 45 46
    #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
47 48
    parser.add_argument("--log_level", action=LogLevelChoices, default="warn", const="log_level",
                        help="Logging level (error, warn, info or debug)")
49 50 51 52
    args = vars(parser.parse_args())
    if os.getenv("MJ_RUN", None) is not None:
        args["mj_run"] = os.getenv("MJ_RUN")
    suite = TestSuite(Environment(**args))
Johannes Bechberger's avatar
Johannes Bechberger committed
53 54 55 56 57 58
    ret = None
    try:
        ret = suite.run()
    finally:
        suite.env.clean_up()
        suite.store()
59 60

    if ret is None or ret.failed > 0:
Johannes Bechberger's avatar
Johannes Bechberger committed
61 62 63
        sys.exit(1)
    else:
        sys.exit(0)