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 test_reporter() -> None: r = Reporter("fuga") with pytest.raises(AssertionError): r.success with pytest.raises(AssertionError): r.elapsed_time with mock.patch("time.time") as m: m.return_value = 100.0 with r: assert r.name == "fuga" r.report_command("command --flag") r.set_result(True, 0) m.return_value = 123.4 assert r.elapsed_time == pytest.approx(23.4) assert r.success r = Reporter("piyo") with r: assert r.name == "piyo" r.set_result(False, 128) assert not r.success
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_single_file_lint_command_base() -> None: with TemporaryDirectory() as t: base_dir = pathlib.Path(t) for file_path in {"foo.py", "bar.pyi", "baz.txt"}: (base_dir / file_path).touch() command = FakeSingleFileLintCommand(base_dir, FakeSource()) with mock.patch.object(command, "check", return_value=True) as check: reporter = Reporter("fake") handler = FakeHandler() reporter.process_output.addHandler(handler) assert command(reporter) == 0 assert check.call_count == 2 assert len(handler.messages) == 0 assert len(reporter.diagnostics) == 0 with mock.patch.object(command, "check") as check: check.side_effect = [True, False] reporter = Reporter("fake") handler = FakeHandler() reporter.process_output.addHandler(handler) assert command(reporter) == 1 assert check.call_count == 2 assert len(handler.messages) == 0 assert len(reporter.diagnostics) == 0
def test__read_stream(sample_str: str) -> None: # NOTE(igarashi): Since BytesIO does not inherit RawIOBase but BufferedIOBase, # we cannot instantiate BufferedReader from bytes directly. # We use FileIOBase instead. with tempfile.TemporaryDirectory() as td: temp_dir = pathlib.Path(td) temp_path = temp_dir / "file" temp_path.write_text(sample_str) reporter = Reporter("foo") handler = FakeHandler() reporter.process_output.setLevel(logging.INFO) reporter.process_output.handlers.clear() reporter.process_output.addHandler(handler) ret = _read_stream(io.StringIO(temp_path.read_text()), reporter, logging.INFO) expected = sample_str assert ret == expected assert handler.messages == expected.splitlines() handler.messages.clear() ret = _read_stream(io.StringIO(temp_path.read_text()), reporter, logging.DEBUG) assert ret == expected assert handler.messages == []
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_reporter_report_diagnostics() -> None: r = Reporter("foo") d1 = Diagnostic(pathlib.Path("hoge").resolve(), 1, 2, 3, message="hoge") d2 = Diagnostic(pathlib.Path("fuga").resolve(), 4, 5, 6, message="fuga") d3 = Diagnostic(pathlib.Path("piyo").resolve(), 7, 8, 9, message="piyo") with r: r.report_diagnostics([d1]) assert r.diagnostics == [d1] r.report_diagnostics([d2, d3]) assert r.diagnostics == [d1, d2, d3]
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 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)
def reporter() -> Iterator[Reporter]: r = Reporter("") with r: yield r
def test_single_file_format_command_base() -> None: with TemporaryDirectory() as t: base_dir = pathlib.Path(t) for file_path in {"foo.py", "bar.pyi", "baz.txt"}: (base_dir / file_path).touch() command = FakeSingleFileFormatCommand( base_dir, FakeSource(), inplace_edit=False ) with mock.patch.object(command, "format", return_value="") as format_method: reporter = Reporter("fake") handler = FakeHandler() reporter.process_output.addHandler(handler) assert command(reporter) == 0 assert format_method.call_count == 2 assert len(handler.messages) == 0 assert len(reporter.diagnostics) == 0 with mock.patch.object(command, "format", return_value="diff") as format_method: reporter = Reporter("fake") handler = FakeHandler() reporter.process_output.addHandler(handler) assert command(reporter) == 1 assert format_method.call_count == 2 assert len(handler.messages) == 2 assert len(reporter.diagnostics) == 2 for file_path in {"foo.py", "bar.pyi"}: assert ( f"--- {base_dir / file_path}\n" f"+++ {base_dir / file_path}\n" "@@ -0,0 +1 @@\n" "+diff" ) in handler.messages assert ( Diagnostic( start_line=1, end_line=1, start_column=1, file_path=base_dir / file_path, diff="+diff", ) in reporter.diagnostics ) command = FakeSingleFileFormatCommand(base_dir, FakeSource(), inplace_edit=True) with mock.patch.object(command, "format", return_value=None) as format_method: reporter = Reporter("fake") handler = FakeHandler() reporter.process_output.addHandler(handler) assert command(reporter) == 1 assert format_method.call_count == 2 assert len(handler.messages) == 0 assert len(reporter.diagnostics) == 0 with (base_dir / "foo.py").open() as f: assert f.read() == "" with (base_dir / "bar.pyi").open() as f: assert f.read() == "" with mock.patch.object(command, "format", return_value="diff") as format_method: reporter = Reporter("fake") handler = FakeHandler() reporter.process_output.addHandler(handler) assert command(reporter) == 0 assert format_method.call_count == 2 assert len(handler.messages) == 0 assert len(reporter.diagnostics) == 0 with (base_dir / "foo.py").open() as f: assert f.read() == "diff" with (base_dir / "bar.pyi").open() as f: assert f.read() == "diff"
def test_run_zero_source() -> None: reporter = Reporter("mypy") assert run(reporter, BASE_DIR, BASE_DIR, MypyTarget([]), True) == 0