Skip to content

Commit

Permalink
test: more tests for setup.cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Mar 11, 2019
1 parent 5a584ac commit e8fe15c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 25 deletions.
11 changes: 5 additions & 6 deletions flake8_nitpick/files/setup_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ def get_missing_output(self, actual_sections: Set[str] = None) -> str:

def check_rules(self) -> YieldFlake8Error:
"""Check missing sections and missing key/value pairs in setup.cfg."""
if not self.file_path.exists():
return

setup_cfg = ConfigParser()
setup_cfg.read_file(self.file_path.open())

actual_sections = set(setup_cfg.sections())
missing = self.get_missing_output(actual_sections)
if missing:
yield self.flake8_error(1, f"Missing sections:\n{missing}")
yield self.flake8_error(1, f" has some missing sections. Use this:\n{missing}")

generators = []
for section in self.expected_sections - self.missing_sections:
Expand Down Expand Up @@ -93,15 +90,17 @@ def compare_different_keys(self, section, key, raw_expected: Any, raw_actual: An
expected = raw_expected
if actual != expected:
yield self.flake8_error(
3, f"Expected value {raw_expected!r} in key, got {raw_actual!r}\n[{section}]\n{key} = {raw_expected}"
3,
f": [{section}]{key} is {raw_actual} but it should be like this:"
+ f"\n[{section}]\n{key} = {raw_expected}",
)

def show_missing_keys(self, section, key, values: List[Tuple[str, Any]]) -> YieldFlake8Error:
"""Show the keys that are not present in a section."""
missing_cfg = ConfigParser()
missing_cfg[section] = dict(values)
output = self.get_example_cfg(missing_cfg)
yield self.flake8_error(4, f"Missing keys in section:\n{output}")
yield self.flake8_error(4, f": section [{section}] has some missing key/value pairs. Use this:\n{output}")

@staticmethod
def get_example_cfg(config_parser: ConfigParser) -> str:
Expand Down
102 changes: 83 additions & 19 deletions tests/test_setup_cfg.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
"""setup.cfg tests."""
from flake8_nitpick.files.setup_cfg import SetupCfgFile
from tests.helpers import ProjectMock


def test_setup_cfg_should_be_deleted(request):
"""File should be deleted."""
ProjectMock(request).style("").setup_cfg("").lint().assert_errors_contain(
f"NIP322 File {SetupCfgFile.file_name} should be deleted"
)
ProjectMock(request).style("").setup_cfg("").lint().assert_single_error(f"NIP322 File setup.cfg should be deleted")


def test_comma_separated_keys_on_style_file(request):
"""Comma separated keys on the style file."""
project = (
ProjectMock(request)
.style(
f"""
[nitpick.files."{SetupCfgFile.file_name}"]
"""
[nitpick.files."setup.cfg"]
comma_separated_values = ["food.eat"]
["{SetupCfgFile.file_name}".food]
["setup.cfg".food]
eat = "salt,ham,eggs"
"""
)
Expand All @@ -31,34 +28,34 @@ def test_comma_separated_keys_on_style_file(request):
)
.lint()
)
project.assert_errors_contain(
f"""
NIP322 File {SetupCfgFile.file_name} has missing values in the 'eat' key. Include those values:
project.assert_single_error(
"""
NIP322 File setup.cfg has missing values in the 'eat' key. Include those values:
[food]
eat = (...),ham,salt
"""
)


def test_missing_setup_cfg(request):
"""Suggest contents when {SetupCfgFile.file_name} does not exist."""
"""Suggest contents when setup.cfg does not exist."""
ProjectMock(request).style(
f"""
[nitpick.files."{SetupCfgFile.file_name}"]
"""
[nitpick.files."setup.cfg"]
"missing_message" = "Do something here"
["{SetupCfgFile.file_name}".mypy]
["setup.cfg".mypy]
ignore_missing_imports = true
["{SetupCfgFile.file_name}".isort]
["setup.cfg".isort]
line_length = 120
["{SetupCfgFile.file_name}".flake8]
["setup.cfg".flake8]
max-line-length = 120
"""
).lint().assert_errors_contain(
f"""
NIP321 File {SetupCfgFile.file_name} was not found. Do something here. Create it with this content:
).lint().assert_single_error(
"""
NIP321 File setup.cfg was not found. Do something here. Create it with this content:
[flake8]
max-line-length = 120
Expand All @@ -69,3 +66,70 @@ def test_missing_setup_cfg(request):
ignore_missing_imports = True
"""
)


def test_missing_sections(request):
"""Test missing sections."""
ProjectMock(request).setup_cfg(
"""
[mypy]
ignore_missing_imports = true
"""
).style(
"""
["setup.cfg".mypy]
ignore_missing_imports = true
["setup.cfg".isort]
line_length = 120
["setup.cfg".flake8]
max-line-length = 120
"""
).lint().assert_single_error(
"""
NIP321 File setup.cfg has some missing sections. Use this:
[flake8]
max-line-length = 120
[isort]
line_length = 120
"""
)


def test_different_missing_keys(request):
"""Test different and missing keys."""
ProjectMock(request).setup_cfg(
"""
[mypy]
ignore_missing_imports = true
[isort]
line_length = 30
[flake8]
xxx = "aaa"
"""
).style(
"""
["setup.cfg".mypy]
ignore_missing_imports = true
["setup.cfg".isort]
line_length = 110
["setup.cfg".flake8]
max-line-length = 112
"""
).lint().assert_errors_contain(
"""
NIP323 File setup.cfg: [isort]line_length is 30 but it should be like this:
[isort]
line_length = 110
"""
).assert_errors_contain(
"""
NIP324 File setup.cfg: section [flake8] has some missing key/value pairs. Use this:
[flake8]
max-line-length = 112
"""
)

0 comments on commit e8fe15c

Please sign in to comment.