Skip to content

Commit

Permalink
fix: move nitpick config to an exclusive section on the style file
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Mar 3, 2019
1 parent fd053aa commit cd64361
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
13 changes: 7 additions & 6 deletions flake8_nitpick/files/base.py
Expand Up @@ -23,19 +23,20 @@ def __init__(self) -> None:
self.file_toml = self.config.style_toml.get(self.toml_key, {})

# Nitpick configuration for this file as a TOML dict, taken from the style file.
self.nitpick_toml = self.file_toml.get("nitpick", {})
self.nitpick_toml = self.config.style_toml.get("nitpick", {}).get("files", {}).get(self.file_name, {})

@property
def toml_key(self):
"""Remove the dot in the beginning of the file name, otherwise it's an invalid TOML key."""
return self.file_name.lstrip(".")

def check_exists(self) -> YieldFlake8Error:
"""Check if the file should exist; if there is style configuration for the file, then it should exist."""
# The file should exist when there is any rule configured for it in the style file,
# or when this flag is manually set
# TODO: add this to the docs
should_exist: bool = self.config.files.get(self.toml_key, bool(self.file_toml))
"""Check if the file should exist; if there is style configuration for the file, then it should exist.
The file should exist when there is any rule configured for it in the style file,
TODO: add this to the docs
"""
should_exist: bool = self.config.files.get(self.toml_key, bool(self.file_toml or self.nitpick_toml))
file_exists = self.file_path.exists()

if should_exist and not file_exists:
Expand Down
10 changes: 5 additions & 5 deletions nitpick-style.toml
Expand Up @@ -24,8 +24,8 @@ file = ".venv"
file = ".pyup.yml"
message = "Configure .travis.yml with safety instead: https://github.com/pyupio/safety#using-safety-with-a-ci-service"

["pyproject.toml".nitpick]
"missing_message" = "Install poetry and run 'poetry init' to create it"
[nitpick.files."pyproject.toml"]
missing_message = "Install poetry and run 'poetry init' to create it"

["pyproject.toml".tool.black]
line-length = 120
Expand All @@ -49,7 +49,7 @@ ipdb = "*"
pylint = "*"
mypy = "*"

["setup.cfg"]
[nitpick.files."setup.cfg"]
comma_separated_values = ["flake8.ignore", "flake8.exclude", "isort.skip", "isort.known_first_party"]

["setup.cfg".flake8]
Expand Down Expand Up @@ -91,8 +91,8 @@ warn_no_return = true
warn_redundant_casts = true
warn_unused_ignores = true

["pre-commit-config.yaml".nitpick]
"missing_message" = "Create the file with the contents below, then run 'pre-commit install'"
[nitpick.files."pre-commit-config.yaml"]
missing_message = "Create the file with the contents below, then run 'pre-commit install'"

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
Expand Down
29 changes: 0 additions & 29 deletions tests/test_nitpick.py → tests/test_config.py
Expand Up @@ -17,32 +17,3 @@ def test_no_main_python_file_root_dir(request):
"NIP102 None of those Python files was found in the root dir "
+ f"{project.root_dir}: {', '.join(ROOT_PYTHON_FILES)}"
}


def test_comma_separated_keys_on_style_file(request):
"""Comma separated keys on the style file."""
project = (
ProjectMock(request)
.style(
"""
["setup.cfg".nitpick]
comma_separated_values = ["food.eat"]
["setup.cfg".food]
eat = "salt,ham,eggs"
"""
)
.setup_cfg(
"""
[food]
eat = spam,eggs,cheese
"""
)
.lint()
)
project.assert_errors_contain(
"""
NIP322 File: setup.cfg: Missing values in key
[food]
eat = ham,salt
"""
)
2 changes: 1 addition & 1 deletion tests/test_pyproject_toml.py
Expand Up @@ -7,7 +7,7 @@ def test_missing_pyproject_toml(request):
"""Suggest poetry init when pyproject.toml does not exist."""
ProjectMock(request, pyproject_toml=False).style(
"""
["pyproject.toml".nitpick]
[nitpick.files."pyproject.toml"]
"missing_message" = "Do something"
"""
).lint().assert_errors_contain(f"NIP311 File: {PyProjectTomlFile.file_name}: Missing file. Do something")
32 changes: 32 additions & 0 deletions tests/test_setup_cfg.py
@@ -0,0 +1,32 @@
"""setup.cfg tests."""
from tests.helpers import ProjectMock


def test_comma_separated_keys_on_style_file(request):
"""Comma separated keys on the style file."""
project = (
ProjectMock(request)
.style(
"""
[nitpick.files."setup.cfg"]
comma_separated_values = ["food.eat"]
["setup.cfg".food]
eat = "salt,ham,eggs"
"""
)
.setup_cfg(
"""
[food]
eat = spam,eggs,cheese
"""
)
.lint()
)
project.assert_errors_contain(
"""
NIP322 File: setup.cfg: Missing values in key
[food]
eat = ham,salt
"""
)

0 comments on commit cd64361

Please sign in to comment.