Skip to content

Commit

Permalink
fix: validate sections in comma_separated_values (fix andreoliwa#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Nov 12, 2020
1 parent b086a24 commit f1be98f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 19 deletions.
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ $(shell mkdir -p .cache/make)

.PHONY: Makefile

build: .remove-old-cache .cache/make/long-pre-commit .cache/make/long-poetry .cache/make/lint .cache/make/test-latest .cache/make/doc # Build the project faster (only latest Python), for local development (default target if you simply run `make` without targets)
build: .remove-old-cache .cache/make/lint .cache/make/test-latest .cache/make/doc # Simple build: no upgrades (pre-commit/Poetry), test only latest Python. For local development and bug fixes (default target)
.PHONY: build

full-build: .remove-old-cache .cache/make/long-pre-commit .cache/make/long-poetry .cache/make/lint .cache/make/test .cache/make/doc # Build the project fully, like in CI
.PHONY: full-build

help:
@echo 'Choose one of the following targets:'
@cat Makefile | egrep '^[a-z0-9 ./-]*:.*#' | sed -E -e 's/:.+# */@ /g' -e 's/ .+@/@/g' | sort | awk -F@ '{printf " \033[1;34m%-10s\033[0m %s\n", $$1, $$2}'
@echo
@echo 'Run 'make -B' or 'make --always-make' to force a rebuild of all targets'
.PHONY: help

full-build: .remove-old-cache .cache/make/long-pre-commit .cache/make/long-poetry .cache/make/lint .cache/make/test .cache/make/doc # Build the project fully, like in CI
.PHONY: full-build

clean: clean-test # Clean all build output (cache, tox, coverage)
rm -rf .cache .mypy_cache docs/_build src/*.egg-info
.PHONY: clean
Expand Down Expand Up @@ -50,6 +50,9 @@ poetry .cache/make/long-poetry: pyproject.toml # Update dependencies
touch .cache/make/long-poetry
.PHONY: poetry

upgrade: .remove-old-cache .cache/make/long-pre-commit .cache/make/long-poetry # Upgrade pre-commit and Poetry
.PHONY: upgrade

lint .cache/make/lint: .github/*/* .travis/* docs/*.py src/*/* styles/*/* tests/*/* nitpick-style.toml .cache/make/long-poetry # Lint the project (tox running pre-commit, flake8)
tox -e lint
touch .cache/make/lint
Expand Down
20 changes: 10 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/nitpick/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def string_or_list_field(object_dict, parent_object_dict): # pylint: disable=un


def validate_section_dot_field(section_field: str) -> bool:
"""Validate if the combinatio section/field has a dot separating them."""
"""Validate if the combination section/field has a dot separating them."""
common = "Use <section_name>.<field_name>"
if "." not in section_field:
raise ValidationError("Dot is missing. {}".format(common))
Expand Down
34 changes: 30 additions & 4 deletions src/nitpick/plugins/setup_cfg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Checker for the `setup.cfg <https://docs.python.org/3/distutils/configfile.html>` config file."""
from configparser import ConfigParser
from enum import IntEnum
from io import StringIO
from typing import Any, Dict, List, Optional, Set, Tuple, Type

Expand All @@ -10,6 +11,16 @@
from nitpick.typedefs import YieldFlake8Error


class ErrorCodes(IntEnum):
"""Setup.cfg error codes."""

MissingSections = 1
MissingValues = 2
ActualExpected = 3
MissingKeyValuePairs = 4
InvalidCommaSeparatedValuesSection = 5


class SetupCfgPlugin(NitpickPlugin):
"""Checker for the `setup.cfg <https://docs.python.org/3/distutils/configfile.html>`_ config file.
Expand All @@ -19,6 +30,7 @@ class SetupCfgPlugin(NitpickPlugin):
file_name = "setup.cfg"
error_base_number = 320
COMMA_SEPARATED_VALUES = "comma_separated_values"
SECTION_SEPARATOR = "."

expected_sections = set() # type: Set[str]
missing_sections = set() # type: Set[str]
Expand Down Expand Up @@ -57,7 +69,17 @@ def check_rules(self) -> YieldFlake8Error:
actual_sections = set(setup_cfg.sections())
missing = self.get_missing_output(actual_sections)
if missing:
yield self.flake8_error(1, " has some missing sections. Use this:", missing)
yield self.flake8_error(ErrorCodes.MissingSections, " has some missing sections. Use this:", missing)

csv_sections = {v.split(".")[0] for v in self.comma_separated_values}
missing_csv = csv_sections.difference(actual_sections)
if missing_csv:
yield self.flake8_error(
ErrorCodes.InvalidCommaSeparatedValuesSection,
": invalid sections on {}:".format(self.COMMA_SEPARATED_VALUES),
", ".join(sorted(missing_csv)),
)
return

for section in self.expected_sections - self.missing_sections:
expected_dict = self.file_dict[section]
Expand All @@ -79,7 +101,7 @@ def compare_different_keys(self, section, key, raw_actual: Any, raw_expected: An
missing = expected_set - actual_set
if missing:
yield self.flake8_error(
2,
ErrorCodes.MissingValues,
" has missing values in the {!r} key. Include those values:".format(key),
"[{}]\n{} = (...),{}".format(section, key, ",".join(sorted(missing))),
)
Expand All @@ -94,7 +116,7 @@ def compare_different_keys(self, section, key, raw_actual: Any, raw_expected: An
expected = raw_expected
if actual != expected:
yield self.flake8_error(
3,
ErrorCodes.ActualExpected,
": [{}]{} is {} but it should be like this:".format(section, key, raw_actual),
"[{}]\n{} = {}".format(section, key, raw_expected),
)
Expand All @@ -106,7 +128,11 @@ def show_missing_keys( # pylint: disable=unused-argument
missing_cfg = ConfigParser()
missing_cfg[section] = dict(values)
output = self.get_example_cfg(missing_cfg)
yield self.flake8_error(4, ": section [{}] has some missing key/value pairs. Use this:".format(section), output)
yield self.flake8_error(
ErrorCodes.MissingKeyValuePairs,
": section [{}] has some missing key/value pairs. Use this:".format(section),
output,
)

@staticmethod
def get_example_cfg(config_parser: ConfigParser) -> str:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_setup_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,27 @@ def test_invalid_section_dot_fields(request):
nitpick.files."setup.cfg".comma_separated_values.3: Empty field name. Use <section_name>.<field_name>\x1b[0m
"""
)


def test_invalid_sections_comma_separated_values(request):
"""Test invalid sections on comma_separated_values."""
ProjectMock(request).style(
"""
["setup.cfg".flake8]
ignore = "W503,E203,FI58,PT003,C408"
exclude = "venv*,**/migrations/"
per-file-ignores = "tests/**.py:FI18,setup.py:FI18"
[nitpick.files."setup.cfg"]
comma_separated_values = ["flake8.ignore", "flake8.exclude", "falek8.per-file-ignores", "aaa.invalid-section"]
"""
).setup_cfg(
"""
[flake8]
exclude = venv*,**/migrations/
ignore = W503,E203,FI12,FI15,FI16,FI17,FI18,FI50,FI51,FI53,FI54,FI55,FI58,PT003,C408
per-file-ignores = tests/**.py:FI18,setup.py:FI18,tests/**.py:BZ01
"""
).flake8().assert_single_error(
"NIP325 File setup.cfg: invalid sections on comma_separated_values:\x1b[32m\naaa, falek8\x1b[0m"
)

0 comments on commit f1be98f

Please sign in to comment.