Esempio n. 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("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
Esempio n. 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("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
Esempio n. 3
0
def test_cli_run(example_dir: pathlib.Path) -> None:
    target = example_dir / TARGET_EXAMPLE
    with change_dir(target):
        subprocess.run(["pysen", "run", "lint"], check=True)
        subprocess.run(["pysen", "run", "format"], check=True)
        subprocess.run(["pysen", "run", "--error-format", "gnu", "lint"],
                       check=True)
Esempio n. 4
0
def test_change_dir() -> None:
    current = pathlib.Path.cwd()

    with change_dir(BASE_DIR / "fakes"):
        assert pathlib.Path.cwd() == BASE_DIR / "fakes"

    assert pathlib.Path.cwd() == current
Esempio n. 5
0
def test_find_pyproject() -> None:
    with change_dir(BASE_DIR):
        assert pyproject.find_pyproject() == PYPROJECT_PATH

    with change_dir(BASE_DIR / "fakes/configs"):
        assert pyproject.find_pyproject() == PYPROJECT_PATH

    with change_dir(EXAMPLE_DIR):
        assert pyproject.find_pyproject() == EXAMPLE_PYPROJECT_PATH

    with pytest.raises(FileNotFoundError):
        pyproject.find_pyproject(BASE_DIR)

    with pytest.raises(FileNotFoundError):
        pyproject.find_pyproject(BASE_DIR / "no_such_file")

    assert pyproject.find_pyproject(FILE_PATH) == FILE_PATH
Esempio n. 6
0
 def __call__(self, reporter: Reporter) -> int:
     with change_dir(self._base_dir):
         try:
             ret = subprocess.run(self._cmd)
             reporter.logger.info(f"{self._cmd} returns {ret.returncode}")
             return ret.returncode
         except BaseException as e:
             reporter.logger.info(
                 f"an error occurred while executing: {self._cmd}\n{e}"
             )
             return 255
Esempio n. 7
0
def create_git_repository(
    tracked_files: Set[str], untracked_files: Set[str]
) -> Iterator[pathlib.Path]:
    files = tracked_files.union(untracked_files)
    assert len(files) == len(tracked_files) + len(
        untracked_files
    )  # assert intersection is empty

    with tempfile.TemporaryDirectory() as d:
        base_dir = pathlib.Path(d)
        with change_dir(base_dir):
            repo = git.Repo.init()
            for f in files:
                touch_file(pathlib.Path(f))

            repo.index.add(list(tracked_files))
            yield base_dir
Esempio n. 8
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
Esempio n. 9
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
Esempio n. 10
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