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
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
def test_run_encoding() -> None: with tempfile.TemporaryDirectory() as td: temp_dir = pathlib.Path(td) temp_path = temp_dir / "file" temp_path.write_bytes(TEST_UNICODE) reporter = Reporter("foo") expected_stdout = temp_path.read_text(sys.stdout.encoding, errors="ignore") expected_utf8 = temp_path.read_text("utf-8", errors="ignore") expected_ascii = temp_path.read_text("ascii", errors="ignore") ret, stdout, stderr = run(["cat", str(temp_path)], reporter) assert ret == 0 assert stdout == expected_stdout ret, stdout, _ = run(["cat", str(temp_path)], reporter, encoding="utf-8") assert ret == 0 assert stdout == expected_utf8 ret, stdout, _ = run(["cat", str(temp_path)], reporter, encoding="ascii") assert ret == 0 assert stdout == expected_ascii == "pysen12345"
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
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
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
def test_run() -> None: assert os.getenv("LANG", "C") == "C", "Did you run pytest through tox?" with tempfile.TemporaryDirectory() as td: temp_dir = pathlib.Path(td) temp_path = temp_dir / "file" temp_path.touch() reporter = Reporter("foo") handler = FakeHandler() reporter.process_output.setLevel(logging.INFO) reporter.process_output.handlers.clear() reporter.process_output.addHandler(handler) ret, stdout, stderr = run(["ls", str(temp_path)], reporter) assert ret == 0 assert "file" in stdout assert stderr == "" assert len(handler.messages) > 0 handler.messages.clear() ret, stdout, stderr = run( ["ls", str(temp_path)], reporter, stdout_loglevel=logging.NOTSET ) assert ret == 0 assert "file" in stdout assert stderr == "" assert len(handler.messages) == 0 handler.messages.clear() ret, stdout, stderr = run(["ls", str(temp_dir / "invalid")], reporter) assert ret != 0 assert stdout == "" assert "No such file or directory" in stderr assert len(handler.messages) > 0 handler.messages.clear() ret, stdout, stderr = run( ["ls", str(temp_dir / "invalid")], reporter, stderr_loglevel=logging.DEBUG ) assert ret != 0 assert stdout == "" assert "No such file or directory" in stderr assert len(handler.messages) == 0 # check if run method can handle large output and errors temp_path.write_text(SAMPLE_SCRIPT) ret, stdout, stderr = run(["bash", str(temp_path)], reporter) stdout_lines = stdout.splitlines() stderr_lines = stderr.splitlines() assert len(stdout_lines) == 102 assert len(stderr_lines) == 102 assert stdout_lines[0] == "Start" and stderr_lines[0] == "StartError" assert stdout_lines[-1] == "End" and stderr_lines[-1] == "EndError" for x in range(100): assert stdout_lines[x + 1] == f"out{x}" assert stderr_lines[x + 1] == f"err{x}" # exceptions encountered in sub-threads shall be raised reporter.process_output.addHandler(FailingHandler()) with pytest.raises(HandlerException): ret, stdout, stderr = run(["echo", "mashimashi"], reporter)