Example #1
0
    def execute_test(self, test_name, test_body, expect_list):
        outfile = io.StringIO()
        global_ctx = lint_util.GlobalContext(outfile)
        local_ctx = global_ctx.get_file_ctx(test_name, self.config)
        __main__.process_file(self.config, local_ctx, test_body)
        for actual, expected in overzip(local_ctx.get_lint(), expect_list):
            if expected is None:
                raise AssertionError(
                    "More lint than expected, starting with {}".format(actual))

            if "@" in expected:
                expect_id, expect_locstr = expected.split("@")
                expect_loc = tuple(
                    int(val) for val in expect_locstr.split(":"))
            else:
                expect_id = expected
                expect_loc = ()

            if actual is None:
                if expect_loc:
                    raise AssertionError(
                        "Missing expected lint {} at {}".format(
                            expect_id, expect_loc))
                else:
                    raise AssertionError(
                        "Missing expected lint {}".format(expect_id))

            if expect_id != actual.spec.idstr:
                raise AssertionError("Expected lint {} but got lint {}".format(
                    expect_id, actual))

            actual_loc = actual.location
            if actual_loc is None:
                actual_loc = ()
            for expect_val, actual_val in zip(expect_loc, actual_loc):
                if expect_val != actual_val:
                    raise AssertionError(
                        "Expected lint {}@{} but got it at {}".format(
                            expect_id, ":".join(str(x) for x in expect_loc),
                            actual_loc))
Example #2
0
def inner_main():
    """Parse arguments, open files, start work."""
    logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")

    argparser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
        usage=USAGE_STRING)

    setup_argparse(argparser)
    args = argparser.parse_args()
    logging.getLogger().setLevel(getattr(logging, args.log_level.upper()))

    if args.dump_config:
        config_dict = __main__.get_config(os.getcwd(), args.config_files)
        __main__.dump_config(args, config_dict, sys.stdout)
        sys.exit(0)

    if args.outfile_path is None:
        args.outfile_path = '-'

    if '-' in args.infilepaths:
        assert len(args.infilepaths) == 1, \
            "You cannot mix stdin as an input with other input files"

    if args.outfile_path == '-':
        outfile = io.open(os.dup(sys.stdout.fileno()),
                          mode='w',
                          encoding="utf-8",
                          newline='')
    else:
        outfile = io.open(args.outfile_path, 'w', encoding="utf-8", newline='')

    global_ctx = lint_util.GlobalContext(outfile)
    returncode = 0
    argdict = __main__.get_argdict(args)

    for infile_path in args.infilepaths:
        # NOTE(josh): have to load config once for every file, because we may pick
        # up a new config file location for each path
        if infile_path == '-':
            config_dict = __main__.get_config(os.getcwd(), args.config_files)
        else:
            config_dict = __main__.get_config(infile_path, args.config_files)
        config_dict.update(argdict)

        cfg = configuration.Configuration(**config_dict)
        if infile_path == '-':
            infile_path = os.dup(sys.stdin.fileno())

        try:
            infile = io.open(infile_path,
                             mode='r',
                             encoding=cfg.encode.input_encoding,
                             newline='')
        except (IOError, OSError):
            logger.error("Failed to open %s for read", infile_path)
            returncode = 1
            continue

        try:
            with infile:
                intext = infile.read()
        except UnicodeDecodeError:
            logger.error("Unable to read %s as %s", infile_path,
                         cfg.encode.input_encoding)
            returncode = 1
            continue

        local_ctx = global_ctx.get_file_ctx(infile_path, cfg)
        process_file(cfg, local_ctx, intext)
        outfile.write("{}\n{}\n".format(infile_path, "=" * len(infile_path)))
        local_ctx.writeout(outfile)
        outfile.write("\n")
        if local_ctx.has_lint():
            returncode = 1

    global_ctx.write_summary(outfile)
    outfile.close()
    return returncode