Skip to content

Commit

Permalink
fix: consider any Python file (thanks @tolusonaike)
Browse files Browse the repository at this point in the history
Fixes #138

Signed-off-by: Augusto W. Andreoli <andreoliwa@gmail.com>
  • Loading branch information
andreoliwa committed Mar 26, 2020
1 parent 3650575 commit 55c0965
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 19 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ This style will assert that:

## Quick setup

To try the package, simply install it (in a virtualenv or globally, wherever) and run `flake8`:
To try the package, simply install it (in a virtualenv or globally) and run `flake8` on a project with at least one Python (`.py`) file:

# Install using pip:
$ pip install -U nitpick
$ flake8

# Or using Poetry:
$ poetry add --dev nitpick

$ flake8 .

Nitpick will download and use the opinionated [default style file](https://raw.githubusercontent.com/andreoliwa/nitpick/v0.21.4/nitpick-style.toml).

Expand Down
11 changes: 9 additions & 2 deletions docs/installation_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ Installation guide
Quick setup
-----------

To try the package, simply install it (in a virtualenv or globally, wherever) and run ``flake8``:
To try the package, simply install it (in a virtualenv or globally) and run flake8_ on a project with at least one Python (``.py``) file:

.. code-block:: shell
$ cd /path/to/my/python/project
# Install using pip:
$ pip install -U nitpick
$ flake8
# Or using Poetry:
$ poetry add --dev nitpick
$ flake8 .
Nitpick_ will download and use the opinionated `default style file`_.

Expand Down
13 changes: 4 additions & 9 deletions poetry.lock

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

7 changes: 6 additions & 1 deletion src/nitpick/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,16 @@ def find_main_python_file(self) -> Path:
The search order is:
1. Python files that belong to the root dir of the project (e.g.: ``setup.py``, ``autoapp.py``).
2. ``manage.py``: they can be on the root or on a subdir (Django projects).
3. Any other ``*.py`` Python file.
3. Any other ``*.py`` Python file on the root dir and subdir.
This avoid long recursions when there is a ``node_modules`` subdir for instance.
"""
for the_file in itertools.chain(
# 1.
[self.root_dir / root_file for root_file in ROOT_PYTHON_FILES],
# 2.
self.root_dir.glob("*/{}".format(MANAGE_PY)),
# 3.
self.root_dir.glob("*.py"),
self.root_dir.glob("*/*.py"),
):
if the_file.exists():
Expand Down
4 changes: 2 additions & 2 deletions src/nitpick/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class NoRootDir(PluginError):


class NoPythonFile(PluginError):
"""No Python file under the root dir."""
"""No Python file was found."""

number = 2
message = "No Python file was found under the root dir {!r}"
message = "No Python file was found on the root dir and subdir of {!r}"

def __init__(self, root_dir: Path, *args: object) -> None:
self.message = self.message.format(str(root_dir))
Expand Down
40 changes: 37 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Config tests."""
import pytest

from tests.helpers import ProjectMock


Expand All @@ -16,10 +18,42 @@ def test_multiple_root_dirs(request):
).style("").flake8().assert_no_errors()


def test_no_main_python_file_root_dir(request):
"""No main Python file on the root dir."""
def test_no_python_file_root_dir(request):
"""No Python file on the root dir."""
project = ProjectMock(request, setup_py=False).pyproject_toml("").save_file("whatever.sh", "", lint=True).flake8()
project.assert_single_error("NIP102 No Python file was found under the root dir {!r}".format(str(project.root_dir)))
project.assert_single_error(
"NIP102 No Python file was found on the root dir and subdir of {!r}".format(str(project.root_dir))
)


@pytest.mark.parametrize(
"python_file,error", [("depth1.py", False), ("subdir/depth2.py", False), ("subdir/another/depth3.py", True)]
)
def test_at_least_one_python_file(python_file, error, request):
"""At least one Python file on the root dir, even if it's not a main file."""
project = (
ProjectMock(request, setup_py=False)
.style(
"""
["pyproject.toml".tool.black]
lines = 100
"""
)
.pyproject_toml(
"""
[tool.black]
lines = 100
"""
)
.save_file(python_file, "", lint=True)
.flake8()
)
if error:
project.assert_single_error(
"NIP102 No Python file was found on the root dir and subdir of {!r}".format(str(project.root_dir))
)
else:
project.assert_no_errors()


def test_django_project_structure(request):
Expand Down

0 comments on commit 55c0965

Please sign in to comment.