Debian Codegraph @

tests/config/pylint_config/test_pylint_config_generate.py

raw
1 # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
2 # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
3 # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
4
5 """Test for the 'pylint-config generate' command."""
6
7
8 import os
9 import tempfile
10 import warnings
11 from pathlib import Path
12
13 import pytest
14 from pytest import CaptureFixture, MonkeyPatch
15
16 from pylint.lint.run import _PylintConfigRun as Run
17
18
19 def test_generate_interactive_exitcode(monkeypatch: MonkeyPatch) -> None:
20 """Check that we exit correctly based on different parameters."""
21 # Monkeypatch everything we don't want to check in this test
22 monkeypatch.setattr(
23 "pylint.config._pylint_config.utils.get_and_validate_format", lambda: "toml"
24 )
25 monkeypatch.setattr(
26 "pylint.config._pylint_config.utils.get_minimal_setting", lambda: False
27 )
28 monkeypatch.setattr(
29 "pylint.config._pylint_config.utils.get_and_validate_output_file",
30 lambda: (False, Path()),
31 )
32
33 with warnings.catch_warnings():
34 warnings.filterwarnings("ignore", message="NOTE:.*", category=UserWarning)
35 with pytest.raises(SystemExit) as ex:
36 Run(["generate", "--interactive"])
37 assert ex.value.code == 0
38
39 Run(["generate", "--interactive"], exit=False)
40
41
42 def test_format_of_output(
43 monkeypatch: MonkeyPatch, capsys: CaptureFixture[str]
44 ) -> None:
45 """Check that we output the correct format."""
46 # Monkeypatch everything we don't want to check in this test
47 monkeypatch.setattr(
48 "pylint.config._pylint_config.utils.get_minimal_setting", lambda: False
49 )
50 monkeypatch.setattr(
51 "pylint.config._pylint_config.utils.get_and_validate_output_file",
52 lambda: (False, Path()),
53 )
54
55 # Set the answers needed for the input() calls
56 answers = iter(["T", "toml", "TOML", "I", "INI", "TOMLINI", "exit()"])
57 monkeypatch.setattr("builtins.input", lambda x: next(answers))
58
59 with warnings.catch_warnings():
60 warnings.filterwarnings("ignore", message="NOTE:.*", category=UserWarning)
61 # Check 'T'
62 Run(["generate", "--interactive"], exit=False)
63 captured = capsys.readouterr()
64 assert "[tool.pylint.main]" in captured.out
65
66 # Check 'toml'
67 Run(["generate", "--interactive"], exit=False)
68 captured = capsys.readouterr()
69 assert "[tool.pylint.main]" in captured.out
70
71 # Check 'TOML'
72 Run(["generate", "--interactive"], exit=False)
73 captured = capsys.readouterr()
74 assert "[tool.pylint.main]" in captured.out
75
76 # Check 'I'
77 Run(["generate", "--interactive"], exit=False)
78 captured = capsys.readouterr()
79 assert "[MAIN]" in captured.out
80
81 # Check 'INI'
82 Run(["generate", "--interactive"], exit=False)
83 captured = capsys.readouterr()
84 assert "[MAIN]" in captured.out
85
86 # Check 'TOMLINI' and then 'exit()'
87 with pytest.raises(SystemExit):
88 Run(["generate", "--interactive"], exit=False)
89
90
91 def test_writing_to_output_file(
92 monkeypatch: MonkeyPatch, capsys: CaptureFixture[str]
93 ) -> None:
94 """Check that we can write to an output file."""
95 # Monkeypatch everything we don't want to check in this test
96 monkeypatch.setattr(
97 "pylint.config._pylint_config.utils.get_and_validate_format", lambda: "toml"
98 )
99 monkeypatch.setattr(
100 "pylint.config._pylint_config.utils.get_minimal_setting", lambda: False
101 )
102
103 # Set up a temporary file to write to
104 tempfile_name = Path(tempfile.gettempdir()) / "CONFIG"
105 if tempfile_name.exists():
106 os.remove(tempfile_name)
107
108 # Set the answers needed for the input() calls
109 answers = iter(
110 [
111 # Don't write to file
112 "no",
113 # Write to file
114 "yes",
115 str(tempfile_name),
116 # Don't overwrite file
117 "yes",
118 str(tempfile_name),
119 "misspelled-no",
120 "no",
121 # Don't overwrite file with default
122 "yes",
123 str(tempfile_name),
124 "",
125 # Overwrite file
126 "yes",
127 str(tempfile_name),
128 "yes",
129 ]
130 )
131 monkeypatch.setattr("builtins.input", lambda x: next(answers))
132
133 with warnings.catch_warnings():
134 warnings.filterwarnings("ignore", message="NOTE:.*", category=UserWarning)
135 # Check no writing to file
136 Run(["generate", "--interactive"], exit=False)
137 captured = capsys.readouterr()
138 assert "[tool.pylint.main]" in captured.out
139
140 # Test writing to file
141 assert not tempfile_name.exists()
142 Run(["generate", "--interactive"], exit=False)
143 captured = capsys.readouterr()
144 assert tempfile_name.exists()
145
146 last_modified = tempfile_name.stat().st_mtime
147
148 # Test not overwriting file
149 Run(["generate", "--interactive"], exit=False)
150 captured = capsys.readouterr()
151 assert last_modified == tempfile_name.stat().st_mtime
152
153 # Test not overwriting file with default value
154 Run(["generate", "--interactive"], exit=False)
155 captured = capsys.readouterr()
156 assert last_modified == tempfile_name.stat().st_mtime
157
158 # Test overwriting
159 Run(["generate", "--interactive"], exit=False)
160 captured = capsys.readouterr()
161 assert last_modified != tempfile_name.stat().st_mtime
162
163
164 def test_writing_minimal_file(
165 monkeypatch: MonkeyPatch, capsys: CaptureFixture[str]
166 ) -> None:
167 """Check that we can write a minimal file."""
168 # Monkeypatch everything we don't want to check in this test
169 monkeypatch.setattr(
170 "pylint.config._pylint_config.utils.get_and_validate_format", lambda: "toml"
171 )
172 monkeypatch.setattr(
173 "pylint.config._pylint_config.utils.get_and_validate_output_file",
174 lambda: (False, Path()),
175 )
176
177 # Set the answers needed for the input() calls
178 answers = iter(["no", "yes"])
179 monkeypatch.setattr("builtins.input", lambda x: next(answers))
180
181 with warnings.catch_warnings():
182 warnings.filterwarnings("ignore", message="NOTE:.*", category=UserWarning)
183 # Check not minimal has comments
184 Run(["generate", "--interactive"], exit=False)
185 captured = capsys.readouterr()
186 assert any(line.startswith("#") for line in captured.out.splitlines())
187
188 # Check minimal doesn't have comments and no default values
189 Run(
190 [
191 "--load-plugins=pylint.extensions.docparams",
192 "--accept-no-return-doc=y",
193 "generate",
194 "--interactive",
195 ],
196 exit=False,
197 )
198 captured = capsys.readouterr()
199 assert not any(i.startswith("#") for i in captured.out.split("\n"))
200 assert "accept-no-return-doc" not in captured.out