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