Skip to content

Commit

Permalink
feat: Suggest poetry init when pyproject.toml does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Jan 12, 2019
1 parent 80f7e24 commit 366c2b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
13 changes: 8 additions & 5 deletions flake8_nitpick/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
# Constants
NAME = "flake8-nitpick"
ERROR_PREFIX = "NIP"
PYPROJECT_TOML = "pyproject.toml"
NITPICK_STYLE_TOML = "nitpick-style.toml"
DEFAULT_NITPICK_STYLE_URL = "https://raw.githubusercontent.com/andreoliwa/flake8-nitpick/master/nitpick-style.toml"
ROOT_PYTHON_FILES = ("setup.py", "manage.py", "autoapp.py")
ROOT_FILES = (PYPROJECT_TOML, "setup.cfg", "requirements*.txt", "Pipfile") + ROOT_PYTHON_FILES
ROOT_FILES = ("requirements*.txt", "Pipfile") + ROOT_PYTHON_FILES

LOG = logging.getLogger("flake8.nitpick")

Expand Down Expand Up @@ -111,9 +110,11 @@ def __init__(self, root_dir: Path) -> None:

def load_toml(self) -> YieldFlake8Error:
"""Load TOML configuration from files."""
self.pyproject_path: Path = self.root_dir / PYPROJECT_TOML
self.pyproject_path: Path = self.root_dir / PyProjectTomlChecker.file_name
if not self.pyproject_path.exists():
yield self.flake8_error(1, f"{PYPROJECT_TOML} does not exist")
yield self.flake8_error(
1, f"{PyProjectTomlChecker.file_name} does not exist. Run 'poetry init' to create one."
)
return

self.pyproject_toml = toml.load(str(self.pyproject_path))
Expand Down Expand Up @@ -241,7 +242,9 @@ def find_root_dir(python_file: str) -> Optional[Path]:
LOG.info("Loading cached root dir: %s", root_dir)
return root_dir

found_files = climb_directory_tree(python_file, ROOT_FILES)
found_files = climb_directory_tree(
python_file, ROOT_FILES + (PyProjectTomlChecker.file_name, SetupCfgChecker.file_name)
)
if not found_files:
LOG.error("No files found while climbing directory tree from %s", python_file)
return None
Expand Down
9 changes: 8 additions & 1 deletion tests/test_nitpick.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Nitpick tests."""
from flake8_nitpick import ROOT_PYTHON_FILES
from flake8_nitpick import ROOT_PYTHON_FILES, PyProjectTomlChecker
from tests.helpers import ProjectMock


Expand Down Expand Up @@ -45,3 +45,10 @@ def test_comma_separated_keys_on_style_file(request):
[food]
eat = ham,salt"""
)


def test_suggest_poetry_init(request):
"""Suggest poetry init when pyproject.toml does not exist."""
assert ProjectMock(request, pyproject_toml=False).lint().errors == {
f"NIP201 {PyProjectTomlChecker.file_name} does not exist. Run 'poetry init' to create one."
}

0 comments on commit 366c2b6

Please sign in to comment.