示例#1
0
def simple_nok_check(code, check_name, line=2, **kwargs):
    extra_disable = [] if "disable" not in kwargs else kwargs["disable"]
    config_w_disable = DEFAULT_CONFIG.copy()
    config_w_disable.update({"disable": [check_name] + extra_disable})
    assert len(lint_code(code, config_w_disable)) == 0

    config = DEFAULT_CONFIG.copy()
    config.update({"disable": extra_disable})
    outcome = lint_code(code, config)
    assert len(outcome) == 1
    assert outcome[0].name == check_name
    assert outcome[0].line == line
示例#2
0
def main():  # pylint: disable=too-many-branches
    CONFIG_FILE_NAME = "gdlintrc"

    arguments = docopt(
        __doc__,
        version="gdlint {}".format(
            pkg_resources.get_distribution("gdtoolkit").version),
    )

    if not isinstance(arguments, dict):
        print(arguments)  # stderr
        sys.exit(0)

    if arguments["--verbose"]:
        logging.basicConfig(stream=sys.stdout, level=logging.INFO)

    if arguments["--dump-default-config"]:  # TODO: error handling
        assert not os.path.isfile(CONFIG_FILE_NAME)
        with open(CONFIG_FILE_NAME, "w") as fh:
            fh.write(yaml.dump(DEFAULT_CONFIG.copy()))
        sys.exit(0)

    # TODO: add opt-based config-file providing
    # TODO: extract the algorithm
    search_dir = Path(os.getcwd())
    found_config_file_path = None
    while search_dir != Path(os.path.abspath(os.sep)):
        file_path = os.path.join(search_dir, CONFIG_FILE_NAME)
        if os.path.isfile(file_path):
            found_config_file_path = file_path
            break
        file_path = os.path.join(search_dir, ".{}".format(CONFIG_FILE_NAME))
        if os.path.isfile(file_path):
            found_config_file_path = file_path
            break
        search_dir = search_dir.parent

    if found_config_file_path is None:
        logging.info(
            """No 'gdlintrc' nor '.gdlintrc' found. Using default config...""")
        config = DEFAULT_CONFIG
    else:
        logging.info("Config file found: '%s'", found_config_file_path)
        with open(found_config_file_path, "r") as fh:
            config = yaml.load(fh.read(), Loader=yaml.Loader)

    logging.info("Loaded config:")
    for entry in config.items():
        logging.info(entry)

    for key in DEFAULT_CONFIG:
        if key not in config:
            logging.info("Adding missing entry from defaults: %s",
                         (key, DEFAULT_CONFIG[key]))
            config[key] = DEFAULT_CONFIG[key]

    problems_total = 0
    files: List[str] = find_files_from(arguments["<path>"], config)

    for file_path in files:
        with open(file_path, "r") as fh:  # TODO: handle exception
            content = fh.read()
            problems = lint_code(content, config)
            problems_total += len(problems)
            if len(problems) > 0:  # TODO: friendly frontend like in halint
                for problem in problems:
                    print_problem(problem, file_path)

    if problems_total > 0:
        print(
            "Failure: {} problem{} found".format(
                problems_total, "" if problems_total == 1 else "s"),
            file=sys.stderr,
        )
        sys.exit(1)

    print("Success: no problems found")
def test_newlineless_code():
    code = """func foo():
    pass"""
    lint_code(code)
def test_empty_code_linting():
    lint_code("")
示例#5
0
def simple_ok_check(code, **kwargs):
    extra_disable = [] if "disable" not in kwargs else kwargs["disable"]
    config = DEFAULT_CONFIG.copy()
    config.update({"disable": extra_disable})
    outcome = lint_code(code, config)
    assert len(outcome) == 0, outcome