Skip to content

Commit

Permalink
[refs #485] Added unit test to check how pylint loads configuration f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
carlio committed Feb 25, 2022
1 parent b0b4f03 commit 0760774
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 1 deletion.
Empty file.
3 changes: 3 additions & 0 deletions tests/tools/pylint/pylint_configs/multiple/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[tool.pylint.'FORMAT']
max-line-length = 40
2 changes: 2 additions & 0 deletions tests/tools/pylint/pylint_configs/multiple/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pylint.VARIABLES]
bad-names=this_is_a_bad_name
3 changes: 3 additions & 0 deletions tests/tools/pylint/pylint_configs/multiple/testcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def name_longer_than_40_chars_but_max_is_40_so_should_error():
this_is_a_bad_name = 5
print(this_is_a_bad_name)
2 changes: 2 additions & 0 deletions tests/tools/pylint/pylint_configs/pylintrc/.pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[FORMAT]
max-line-length=40
Empty file.
2 changes: 2 additions & 0 deletions tests/tools/pylint/pylint_configs/pylintrc/testcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def name_longer_than_40_chars_but_max_is_40_so_should_error():
print("hi")
Empty file.
2 changes: 2 additions & 0 deletions tests/tools/pylint/pylint_configs/pylintrc2/pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[FORMAT]
max-line-length=40
2 changes: 2 additions & 0 deletions tests/tools/pylint/pylint_configs/pylintrc2/testcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def name_longer_than_40_chars_but_max_is_40_so_should_error():
print("hi")
Empty file.
3 changes: 3 additions & 0 deletions tests/tools/pylint/pylint_configs/pyproject/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[tool.pylint.'FORMAT']
max-line-length = 40
2 changes: 2 additions & 0 deletions tests/tools/pylint/pylint_configs/pyproject/testcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def name_longer_than_40_chars_but_max_is_40_so_should_error():
print("hi")
Empty file.
2 changes: 2 additions & 0 deletions tests/tools/pylint/pylint_configs/setup.cfg/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pylint.FORMAT]
max-line-length = 40
2 changes: 2 additions & 0 deletions tests/tools/pylint/pylint_configs/setup.cfg/testcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def name_longer_than_40_chars_but_max_is_40_so_should_error():
print("hi")
28 changes: 27 additions & 1 deletion tests/tools/pylint/test_pylint_tool.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
from pathlib import Path
from unittest import TestCase
from unittest.mock import patch

from prospector.config import ProspectorConfig
from prospector.finder import find_python
from prospector.tools.pylint import PylintTool

THIS_DIR = Path(__file__).parent


def _get_pylint_tool_and_prospector_config(argv_patch=None):
if argv_patch is None:
Expand All @@ -17,6 +20,29 @@ def _get_pylint_tool_and_prospector_config(argv_patch=None):


class TestPylintTool(TestCase):
def test_pylint_config(self):
"""Verifies that prospector will configure pylint with any pylint-specific configuration if found"""

def _has_message(msg_list, code):
return any([message.code == code and message.source == "pylint" for message in msg_list])

for config_type in ("pylintrc", "pylintrc2", "pyproject", "setup.cfg", "multiple"):
root = THIS_DIR / "pylint_configs" / config_type

with patch("os.getcwd", return_value=root.absolute()):
pylint_tool, config = _get_pylint_tool_and_prospector_config()
self.assertEqual(Path(config.workdir).absolute(), root.absolute())

found_files = find_python([], [str(root)], explicit_file_mode=False, workdir=str(root))
pylint_tool.configure(config, found_files)

messages = pylint_tool.run(found_files)
self.assertTrue(_has_message(messages, "line-too-long"), msg=config_type)
if config_type == "multiple":
self.assertTrue(_has_message(messages, "pylint-disallowed_name"), msg=config_type)
else:
self.assertFalse(_has_message(messages, "pylint-disallowed_name"), msg=config_type)

def test_absolute_path_is_computed_correctly(self):
pylint_tool, config = _get_pylint_tool_and_prospector_config()
root = os.path.join(os.path.dirname(__file__), "testpath", "test.py")
Expand Down Expand Up @@ -56,7 +82,7 @@ def test_use_pylint_default_path_finder(self):
found_files = find_python([], [root], False, workdir)
pylint_tool.configure(config, found_files)
messages = pylint_tool.run(found_files)
self.assertListEqual(messages, [])
self.assertEqual(messages[0].code, "no-name-in-module")

def test_use_prospector_default_path_finder(self):
workdir = "tests/tools/pylint/testpath/absolute-import/"
Expand Down

0 comments on commit 0760774

Please sign in to comment.