Skip to content

Commit

Permalink
fix: only check files if they have configured styles
Browse files Browse the repository at this point in the history
.pre-commit-config.yaml was being checked without request
  • Loading branch information
andreoliwa committed Sep 23, 2019
1 parent bcfe698 commit 2cac863
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
3 changes: 2 additions & 1 deletion docs/config_files.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.. include:: targets.rst
.. _config_files:

Configuration files
Expand All @@ -12,7 +13,7 @@ Below are the currently supported configuration files.
pyproject.toml
--------------

Checker for the `pyproject.toml <https://poetry.eustace.io/docs/pyproject/>`_ config file.
Checker for the `pyproject.toml <pyproject-toml-poetry>`_ config file.

See also `PEP 518 <https://www.python.org/dev/peps/pep-0518/>`_.

Expand Down
2 changes: 1 addition & 1 deletion docs/targets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
.. _Bash: https://www.gnu.org/software/bash/
.. _commitlint: https://commitlint.js.org/
.. _pytest: https://pytest.org/
.. _pyproject-toml-poetry: https://poetry.eustace.io/docs/pyproject/
.. _pyproject-toml-poetry: https://github.com/sdispater/poetry/blob/master/docs/docs/pyproject.md
23 changes: 11 additions & 12 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/files/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def check_exists(self) -> YieldFlake8Error:
# Only display this message if the style is valid.
if not Nitpick.current_app().style_errors:
yield self.flake8_error(2, " should be deleted")
elif file_exists:
elif file_exists and config_data_exists:
yield from self.check_rules()

@abc.abstractmethod
Expand Down
2 changes: 2 additions & 0 deletions src/nitpick/files/pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def check_rules(self) -> YieldFlake8Error:
"""Check the rules for the pre-commit hooks."""
self.actual_yaml = YamlFormat(path=self.file_path)
if KEY_REPOS not in self.actual_yaml.as_data:
# TODO: if the 'repos' key doesn't exist, assume repos are in the root of the .yml file
# Having the 'repos' key is not actually a requirement. 'pre-commit-validate-config' works without it.
yield self.flake8_error(1, " doesn't have the {!r} root key".format(KEY_REPOS))
return

Expand Down
4 changes: 2 additions & 2 deletions src/nitpick/files/pyproject_toml.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Checker for the `pyproject.toml <https://poetry.eustace.io/docs/pyproject/>`_ config file."""
"""Checker for the `pyproject.toml <https://github.com/sdispater/poetry/blob/master/docs/docs/pyproject.md>`_ file."""
from nitpick import Nitpick
from nitpick.files.base import BaseFile
from nitpick.typedefs import YieldFlake8Error


class PyProjectTomlFile(BaseFile):
"""Checker for the `pyproject.toml <https://poetry.eustace.io/docs/pyproject/>`_ config file.
"""Checker for the `pyproject.toml <https://github.com/sdispater/poetry/blob/master/docs/docs/pyproject.md>`_ file.
See also `PEP 518 <https://www.python.org/dev/peps/pep-0518/>`_.
Expand Down
17 changes: 15 additions & 2 deletions tests/test_pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@


def test_pre_commit_has_no_configuration(request):
"""File should not be deleted unless explicitly asked."""
ProjectMock(request).style("").pre_commit("").flake8().assert_single_error(
"""No errors should be raised if pre-commit is not referenced in any style file.
Also the file should not be deleted unless explicitly asked.
"""
ProjectMock(request).style("").pre_commit("").flake8().assert_no_errors()


def test_pre_commit_referenced_in_style(request):
"""Only check files if they have configured styles."""
ProjectMock(request).style(
"""
["pre-commit-config.yaml"]
fail_fast = true
"""
).pre_commit("").flake8().assert_single_error(
"NIP331 File .pre-commit-config.yaml doesn't have the 'repos' root key"
)

Expand Down

0 comments on commit 2cac863

Please sign in to comment.