summaryrefslogtreecommitdiff
path: root/tests/config
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-05-22 17:46:16 +0200
committerGitHub <noreply@github.com>2022-05-22 17:46:16 +0200
commite5e4b213f163f7a55474db5dcb83bd8d5304fbba (patch)
tree742595b0725cd21525dfcc7eac686dc276254399 /tests/config
parent055c1920140ebf36501d30307d5ab86965ae185f (diff)
downloadpylint-git-e5e4b213f163f7a55474db5dcb83bd8d5304fbba.tar.gz
Allow specifying an output file when generating a configuration file (#6662)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'tests/config')
-rw-r--r--tests/config/pylint_config/test_pylint_config_generate.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/config/pylint_config/test_pylint_config_generate.py b/tests/config/pylint_config/test_pylint_config_generate.py
index 118aff0fa..4650ab1fb 100644
--- a/tests/config/pylint_config/test_pylint_config_generate.py
+++ b/tests/config/pylint_config/test_pylint_config_generate.py
@@ -5,7 +5,10 @@
"""Test for the 'pylint-config generate' command."""
+import os
+import tempfile
import warnings
+from pathlib import Path
import pytest
from pytest import CaptureFixture, MonkeyPatch
@@ -19,6 +22,10 @@ def test_generate_interactive_exitcode(monkeypatch: MonkeyPatch) -> None:
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)
@@ -33,6 +40,12 @@ 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))
@@ -67,3 +80,73 @@ def test_format_of_output(
# 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"
+ )
+
+ # 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