Skip to content

Commit

Permalink
feat: add config to check files that should be present
Browse files Browse the repository at this point in the history
Closes #74
  • Loading branch information
andreoliwa committed Aug 13, 2019
1 parent 31b13ea commit 408440f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ If a key is defined in more than one file, the value from the last file will pre
### Style file syntax

NOTE: The project is still experimental; the style file syntax might slightly change before the 1.0 stable release.
The `NIP*` error codes also might change.

A style file contains basically the configuration options you want to enforce in all your projects.

Expand Down Expand Up @@ -111,6 +112,15 @@ To enforce all your projects to ignore missing imports, add this to your `nitpic
["setup.cfg".mypy]
ignore_missing_imports = true

### Present files

To enforce that certain files should exist in the project, you can add them to the style file as a dictionary of "file name" and "extra message".
Use an empty string to not display any extra message.

[nitpick.files.present]
".editorconfig" = ""
"CHANGELOG.md" = "A project should have a changelog"

### Absent files

To enforce that certain files should not exist in the project, you can add them to the style file.
Expand Down
1 change: 1 addition & 0 deletions src/nitpick/files/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def check_exists(self) -> YieldFlake8Error:
if config_data_exists and not file_exists:
suggestion = self.suggest_initial_contents()
phrases = [" was not found"]
# TODO: add validation for missing_message on the BaseFileSchema
missing_message = self.nitpick_file_dict.get("missing_message", "")
if missing_message:
phrases.append(missing_message)
Expand Down
23 changes: 14 additions & 9 deletions src/nitpick/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,28 @@ def run(self) -> YieldFlake8Error:
return []
LOGGER.info("Nitpicking file: %s", self.filename)

yield from itertools.chain(self.config.merge_styles(), self.check_absent_files())
yield from itertools.chain(self.config.merge_styles(), self.check_files(True), self.check_files(False))

for checker_class in get_subclasses(BaseFile):
checker = checker_class()
yield from checker.check_exists()

return []

def check_absent_files(self) -> YieldFlake8Error:
"""Check absent files."""
for file_name, delete_message in self.config.files.get("absent", {}).items():
def check_files(self, present: bool) -> YieldFlake8Error:
"""Check files that should be present or absent."""
# TODO: validate with schemas
key = "present" if present else "absent"
message = "exist" if present else "be deleted"
absent = not present
for file_name, extra_message in self.config.files.get(key, {}).items():
file_path = self.config.root_dir / file_name # type: Path
if not file_path.exists():
exists = file_path.exists()
if (present and exists) or (absent and not exists):
continue

full_message = "File {} should be deleted".format(file_name)
if delete_message:
full_message += ": {}".format(delete_message)
full_message = "File {} should {}".format(file_name, message)
if extra_message:
full_message += ": {}".format(extra_message)

yield self.flake8_error(3, full_message)
yield self.flake8_error(3 if present else 4, full_message)
33 changes: 31 additions & 2 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,36 @@ def test_absent_files(request):
yyy = "Remove that"
"""
).touch_file("xxx").touch_file("yyy").lint().assert_errors_contain(
"NIP103 File xxx should be deleted: Remove this"
"NIP104 File xxx should be deleted: Remove this"
).assert_errors_contain(
"NIP103 File yyy should be deleted: Remove that"
"NIP104 File yyy should be deleted: Remove that"
)


def test_present_files(request):
"""Test present files from the style configuration."""
project = ProjectMock(request)
project.style(
"""
[nitpick.files.".editorconfig"]
missing_message = "Create this file"
"""
).lint().assert_errors_contain(
"""
NIP001 File nitpick-style.toml has an incorrect style. Invalid TOML:\x1b[92m
TomlDecodeError: Invalid group name \'editorconfig"\'. Try quoting it. (line 1 column 1 char 0)\x1b[0m
"""
)

project.style(
"""
[nitpick.files.present]
".editorconfig" = "Create this file"
".env" = ""
"another-file.txt" = ""
"""
).lint().assert_errors_contain("NIP103 File .editorconfig should exist: Create this file").assert_errors_contain(
"NIP103 File .env should exist"
).assert_errors_contain(
"NIP103 File another-file.txt should exist", 3
)

0 comments on commit 408440f

Please sign in to comment.