|
| 1 | +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 2 | +# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE |
| 3 | +# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt |
| 4 | + |
| 5 | +"""Test for the 'pylint-config generate' command.""" |
| 6 | + |
| 7 | + |
| 8 | +import warnings |
| 9 | + |
| 10 | +import pytest |
| 11 | +from pytest import CaptureFixture, MonkeyPatch |
| 12 | + |
| 13 | +from pylint.lint.run import _PylintConfigRun as Run |
| 14 | + |
| 15 | + |
| 16 | +def test_generate_interactive_exitcode(monkeypatch: MonkeyPatch) -> None: |
| 17 | + """Check that we exit correctly based on different parameters.""" |
| 18 | + # Monkeypatch everything we don't want to check in this test |
| 19 | + monkeypatch.setattr( |
| 20 | + "pylint.config._pylint_config.utils.get_and_validate_format", lambda: "toml" |
| 21 | + ) |
| 22 | + |
| 23 | + with warnings.catch_warnings(): |
| 24 | + warnings.filterwarnings("ignore", message="NOTE:.*", category=UserWarning) |
| 25 | + with pytest.raises(SystemExit) as ex: |
| 26 | + Run(["generate", "--interactive"]) |
| 27 | + assert ex.value.code == 0 |
| 28 | + |
| 29 | + Run(["generate", "--interactive"], exit=False) |
| 30 | + |
| 31 | + |
| 32 | +def test_format_of_output( |
| 33 | + monkeypatch: MonkeyPatch, capsys: CaptureFixture[str] |
| 34 | +) -> None: |
| 35 | + """Check that we output the correct format.""" |
| 36 | + # Set the answers needed for the input() calls |
| 37 | + answers = iter(["T", "toml", "TOML", "I", "INI", "TOMLINI"]) |
| 38 | + monkeypatch.setattr("builtins.input", lambda x: next(answers)) |
| 39 | + |
| 40 | + with warnings.catch_warnings(): |
| 41 | + warnings.filterwarnings("ignore", message="NOTE:.*", category=UserWarning) |
| 42 | + # Check 'T' |
| 43 | + Run(["generate", "--interactive"], exit=False) |
| 44 | + captured = capsys.readouterr() |
| 45 | + assert "[tool.pylint.main]" in captured.out |
| 46 | + |
| 47 | + # Check 'toml' |
| 48 | + Run(["generate", "--interactive"], exit=False) |
| 49 | + captured = capsys.readouterr() |
| 50 | + assert "[tool.pylint.main]" in captured.out |
| 51 | + |
| 52 | + # Check 'TOML' |
| 53 | + Run(["generate", "--interactive"], exit=False) |
| 54 | + captured = capsys.readouterr() |
| 55 | + assert "[tool.pylint.main]" in captured.out |
| 56 | + |
| 57 | + # Check 'I' |
| 58 | + Run(["generate", "--interactive"], exit=False) |
| 59 | + captured = capsys.readouterr() |
| 60 | + assert "[MAIN]" in captured.out |
| 61 | + |
| 62 | + # Check 'INI' |
| 63 | + Run(["generate", "--interactive"], exit=False) |
| 64 | + captured = capsys.readouterr() |
| 65 | + assert "[MAIN]" in captured.out |
| 66 | + |
| 67 | + # Check 'TOMLINI' |
| 68 | + with pytest.raises(ValueError, match="Format should be one.*"): |
| 69 | + Run(["generate", "--interactive"], exit=False) |
0 commit comments