def test_pylint_config_main_messages(capsys: CaptureFixture[str]) -> None: """Check that the help messages are displayed correctly.""" with warnings.catch_warnings(): warnings.filterwarnings("ignore", message="NOTE:.*", category=UserWarning) Run([], exit=False) captured = capsys.readouterr() assert captured.out.startswith("usage: pylint-config [options]") assert "--interactive" not in captured.out Run(["-h"], exit=False) captured_two = capsys.readouterr() assert captured_two.out == captured.out with pytest.raises(SystemExit): Run(["generate", "-h"]) captured = capsys.readouterr() assert captured.out.startswith( "usage: pylint-config [options] generate") assert "--interactive" in captured.out with pytest.raises(SystemExit) as ex: Run(["generate"]) captured_two = capsys.readouterr() assert captured_two.out == captured.out # This gets auto-raised by argparse to be 0. assert ex.value.code == 32 with pytest.raises(SystemExit) as ex: Run(["-h"]) assert ex.value.code == 32
def test_generate_interactive_exitcode(monkeypatch: MonkeyPatch) -> None: """Check that we exit correctly based on different parameters.""" # Monkeypatch everything we don't want to check in this test monkeypatch.setattr( "pylint.config._pylint_config.utils.get_and_validate_format", lambda: "toml") monkeypatch.setattr( "pylint.config._pylint_config.utils.get_and_validate_output_file", lambda: (False, Path()), ) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message="NOTE:.*", category=UserWarning) with pytest.raises(SystemExit) as ex: Run(["generate", "--interactive"]) assert ex.value.code == 0 Run(["generate", "--interactive"], exit=False)
def test_verbose_output_no_config(capsys: CaptureFixture) -> None: """Test that we print a log message in verbose mode with no file.""" with tempdir() as chroot: with fake_home(): chroot_path = Path(chroot) testutils.create_files(["a/b/c/d/__init__.py"]) os.chdir(chroot_path / "a/b/c") with pytest.raises(SystemExit): Run(["--verbose"]) out = capsys.readouterr() assert "No config file found, using default configuration" in out.err
def test_verbose_abbreviation(capsys: CaptureFixture) -> None: """Test that we correctly handle an abbreviated pre-processable option.""" with tempdir() as chroot: with fake_home(): chroot_path = Path(chroot) testutils.create_files(["a/b/c/d/__init__.py"]) os.chdir(chroot_path / "a/b/c") with pytest.raises(SystemExit): Run(["--ve"]) out = capsys.readouterr() # This output only exists when launched in verbose mode assert "No config file found, using default configuration" in out.err
def processFile(filename, compiledPackages="PyQt5,PyQt4"): jsonOut = {} myfile = '.'.join(filename.split('.')[1:]) ##### # Set up reporting for pylint functionality out = StringIO(None) reporter = AjsonReporter(out) with _patch_streams(out): Run([filename, "--extension-pkg-whitelist=" + compiledPackages], reporter=reporter) if reporter: jsonOut = reporter.get_messages() #self.acquiredData[filename] = jsonOut return jsonOut
def processFile(self, filename): ''' Process a file and aquire data from the pylint parser ''' jsonOut = {} myfile = '.'.join(filename.split('.')[1:]) ##### # Set up reporting for pylint functionality out = StringIO(None) reporter = self.AjsonReporter(out) with self._patch_streams(out): Run([filename] + self.args, reporter=reporter) if reporter: jsonOut = reporter.get_messages() #self.acquiredData[filename] = jsonOut return jsonOut
def test_format_of_output(monkeypatch: MonkeyPatch, capsys: CaptureFixture[str]) -> None: """Check that we output the correct format.""" # Monkeypatch everything we don't want to check in this test monkeypatch.setattr( "pylint.config._pylint_config.utils.get_and_validate_output_file", lambda: (False, Path()), ) # Set the answers needed for the input() calls answers = iter(["T", "toml", "TOML", "I", "INI", "TOMLINI", "exit()"]) monkeypatch.setattr("builtins.input", lambda x: next(answers)) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message="NOTE:.*", category=UserWarning) # Check 'T' Run(["generate", "--interactive"], exit=False) captured = capsys.readouterr() assert "[tool.pylint.main]" in captured.out # Check 'toml' Run(["generate", "--interactive"], exit=False) captured = capsys.readouterr() assert "[tool.pylint.main]" in captured.out # Check 'TOML' Run(["generate", "--interactive"], exit=False) captured = capsys.readouterr() assert "[tool.pylint.main]" in captured.out # Check 'I' Run(["generate", "--interactive"], exit=False) captured = capsys.readouterr() assert "[MAIN]" in captured.out # Check 'INI' Run(["generate", "--interactive"], exit=False) captured = capsys.readouterr() assert "[MAIN]" in captured.out # Check 'TOMLINI' and then 'exit()' with pytest.raises(SystemExit): Run(["generate", "--interactive"], exit=False)
from pylint.lint.parallel import check_parallel from pylint.lint.pylinter import PyLinter from pylint.lint.report_functions import ( report_messages_by_module_stats, report_messages_stats, report_total_messages_stats, ) from pylint.lint.run import Run from pylint.lint.utils import ( ArgumentPreprocessingError, _patch_sys_path, fix_import_path, preprocess_options, ) __all__ = [ "check_parallel", "PyLinter", "report_messages_by_module_stats", "report_messages_stats", "report_total_messages_stats", "Run", "ArgumentPreprocessingError", "_patch_sys_path", "fix_import_path", "preprocess_options", ] if __name__ == "__main__": Run(sys.argv[1:])
def test_writing_to_output_file(monkeypatch: MonkeyPatch, capsys: CaptureFixture[str]) -> None: """Check that we can write to an output file.""" # Monkeypatch everything we don't want to check in this test monkeypatch.setattr( "pylint.config._pylint_config.utils.get_and_validate_format", lambda: "toml") # Set up a temporary file to write to tempfile_name = Path(tempfile.gettempdir()) / "CONFIG" if tempfile_name.exists(): os.remove(tempfile_name) # Set the answers needed for the input() calls answers = iter([ # Don't write to file "no", # Write to file "yes", str(tempfile_name), # Don't overwrite file "yes", str(tempfile_name), "misspelled-no", "no", # Don't overwrite file with default "yes", str(tempfile_name), "", # Overwrite file "yes", str(tempfile_name), "yes", ]) monkeypatch.setattr("builtins.input", lambda x: next(answers)) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message="NOTE:.*", category=UserWarning) # Check no writing to file Run(["generate", "--interactive"], exit=False) captured = capsys.readouterr() assert "[tool.pylint.main]" in captured.out # Test writing to file assert not tempfile_name.exists() Run(["generate", "--interactive"], exit=False) captured = capsys.readouterr() assert tempfile_name.exists() last_modified = tempfile_name.stat().st_mtime # Test not overwriting file Run(["generate", "--interactive"], exit=False) captured = capsys.readouterr() assert last_modified == tempfile_name.stat().st_mtime # Test not overwriting file with default value Run(["generate", "--interactive"], exit=False) captured = capsys.readouterr() assert last_modified == tempfile_name.stat().st_mtime # Test overwriting Run(["generate", "--interactive"], exit=False) captured = capsys.readouterr() assert last_modified != tempfile_name.stat().st_mtime
def _set_verbose_mode(run: Run, value: str | None) -> None: assert value is None run.verbose = True
def _set_output(run: Run, value: str | None) -> None: """Set the output.""" assert value is not None run._output = value
def _set_rcfile(run: Run, value: str | None) -> None: """Set the rcfile.""" assert value is not None run._rcfile = value