Exemple #1
0
def run(
    reporter: Reporter,
    base_dir: pathlib.Path,
    setting_path: pathlib.Path,
    sources: Iterable[pathlib.Path],
    inplace_edit: bool,
) -> int:
    check_command_installed(
        *process_utils.add_python_executable("isort", "--version"))
    version = _get_isort_version()

    targets = [str(d) for d in sources]
    if len(targets) == 0:
        return 0

    cmd = ["isort", "--settings-path", str(setting_path)]
    if version.major == 4:
        cmd.append("--recursive")
    if not inplace_edit:
        cmd += ["--diff", "--check-only"]
    cmd += targets

    with change_dir(base_dir):
        ret, stdout, _ = process_utils.run(
            process_utils.add_python_executable(*cmd), reporter)

    diagnostics = parse_error_diffs(stdout,
                                    _parse_file_path,
                                    logger=reporter.logger)
    reporter.report_diagnostics(list(diagnostics))

    return ret
Exemple #2
0
def run(
    reporter: Reporter,
    base_dir: pathlib.Path,
    setting_path: pathlib.Path,
    sources: Iterable[pathlib.Path],
    inplace_edit: bool,
) -> int:
    check_command_installed(*process_utils.add_python_executable("black", "--version"))
    _check_black_version()

    targets = [str(d) for d in sources]
    if len(targets) == 0:
        return 0

    cmd = (
        ["black", "--config", str(setting_path)]
        + (["--diff", "--check"] if not inplace_edit else [])
        + targets
    )
    with change_dir(base_dir):
        ret, stdout, _ = process_utils.run(
            process_utils.add_python_executable(*cmd), reporter
        )

    diagnostics = parse_error_diffs(stdout, _parse_file_path, logger=reporter.logger)
    reporter.report_diagnostics(list(diagnostics))

    return ret
Exemple #3
0
    def _run(
        self,
        reporter: Reporter,
        base_dir: pathlib.Path,
        setting: "AutoflakeSetting",
        sources: Iterable[pathlib.Path],
    ) -> int:
        check_command_installed("autoflake", "--version")

        targets = [str(d) for d in sources]
        if len(targets) == 0:
            return 0

        cmd = ["autoflake"]
        if self._in_place:
            cmd += ["--in-place"]

        if setting.imports:
            cmd += ["--imports", ",".join(setting.imports)]

        if setting.expand_star_imports:
            cmd += ["--expand-star-imports"]

        if setting.remove_unused_variables:
            cmd += ["--remove-unused-variables"]

        if setting.ignore_init_module_imports:
            cmd += ["--ignore-init-module-imports"]

        if setting.remove_duplicate_keys:
            cmd += ["--remove-duplicate-keys"]

        if setting.remove_all_unused_imports:
            cmd += ["--remove-all-unused-imports"]

        cmd += targets

        with change_dir(base_dir):
            ret, stdout, _ = process_utils.run(cmd, reporter)

        diagnostics = parse_error_diffs(stdout,
                                        self._parse_file_path,
                                        logger=reporter.logger)
        reporter.report_diagnostics(list(diagnostics))
        return ret
Exemple #4
0
def run(
    reporter: Reporter,
    base_dir: pathlib.Path,
    setting_path: pathlib.Path,
    target: MypyTarget,
    require_diagnostics: bool,
) -> int:
    check_command_installed(
        *process_utils.add_python_executable("mypy", "--version"))
    _check_mypy_version()

    target_paths = [str(resolve_path(base_dir, x)) for x in target.paths]
    if len(target_paths) == 0:
        return 0

    extra_options: List[str] = ["--show-absolute-path"]
    if require_diagnostics:
        extra_options += [
            "--no-color-output",
            "--show-column-numbers",
            "--no-error-summary",
        ]
    else:
        extra_options += [
            "--pretty",
        ]

    if target.namespace_packages:
        extra_options.append("--namespace-packages")

    cmd = ["mypy"] + extra_options + ["--config-file",
                                      str(setting_path)] + target_paths
    with change_dir(base_dir):
        ret, stdout, _ = process_utils.run(
            process_utils.add_python_executable(*cmd), reporter)

    if require_diagnostics:
        diagnostics = parse_error_lines(stdout, logger=reporter.logger)
        reporter.report_diagnostics(list(diagnostics))

    return ret
Exemple #5
0
def run(
    reporter: Reporter,
    base_dir: pathlib.Path,
    setting_path: pathlib.Path,
    sources: Iterable[pathlib.Path],
) -> int:
    check_command_installed(
        *process_utils.add_python_executable("flake8", "--version"))
    _check_flake8_version()
    targets = [str(d) for d in sources]
    if len(targets) == 0:
        return 0

    cmd = ["flake8", "--config", str(setting_path)] + targets
    with change_dir(base_dir):
        ret, stdout, _ = process_utils.run(
            process_utils.add_python_executable(*cmd), reporter)

    diagnostics = parse_error_lines(stdout, logger=reporter.logger)
    reporter.report_diagnostics(list(diagnostics))

    return ret
Exemple #6
0
def test_check_command_installed(invalid_command: List[str]) -> None:
    check_command_installed("echo", "a")

    with pytest.raises(CommandNotFoundError):
        check_command_installed(*invalid_command)
    with unittest.mock.patch("subprocess.call", return_value=127):
        with pytest.raises(CommandNotFoundError):
            assert subprocess.call("hoge") == 127
            check_command_installed("hoge")