From cd64361a16d72e7ca1640abdfb3aa319d9a41d9a Mon Sep 17 00:00:00 2001 From: Augusto Wagner Andreoli Date: Sun, 3 Mar 2019 16:10:57 +0100 Subject: [PATCH] fix: move nitpick config to an exclusive section on the style file --- flake8_nitpick/files/base.py | 13 ++++----- nitpick-style.toml | 10 +++---- tests/{test_nitpick.py => test_config.py} | 29 -------------------- tests/test_pyproject_toml.py | 2 +- tests/test_setup_cfg.py | 32 +++++++++++++++++++++++ 5 files changed, 45 insertions(+), 41 deletions(-) rename tests/{test_nitpick.py => test_config.py} (52%) create mode 100644 tests/test_setup_cfg.py diff --git a/flake8_nitpick/files/base.py b/flake8_nitpick/files/base.py index ad07f3af..4afe479c 100644 --- a/flake8_nitpick/files/base.py +++ b/flake8_nitpick/files/base.py @@ -23,7 +23,7 @@ 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): @@ -31,11 +31,12 @@ def toml_key(self): 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: diff --git a/nitpick-style.toml b/nitpick-style.toml index c3adbb7a..d43eb1fd 100644 --- a/nitpick-style.toml +++ b/nitpick-style.toml @@ -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 @@ -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] @@ -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 diff --git a/tests/test_nitpick.py b/tests/test_config.py similarity index 52% rename from tests/test_nitpick.py rename to tests/test_config.py index 84b0e2f2..fa37772b 100644 --- a/tests/test_nitpick.py +++ b/tests/test_config.py @@ -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 - """ - ) diff --git a/tests/test_pyproject_toml.py b/tests/test_pyproject_toml.py index a3db90a7..22e11437 100644 --- a/tests/test_pyproject_toml.py +++ b/tests/test_pyproject_toml.py @@ -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") diff --git a/tests/test_setup_cfg.py b/tests/test_setup_cfg.py new file mode 100644 index 00000000..9cd04c69 --- /dev/null +++ b/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 + """ + )