Skip to content

Commit

Permalink
Merge d51540b into 35f1b69
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Jan 28, 2022
2 parents 35f1b69 + d51540b commit fab55f8
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 152 deletions.
11 changes: 11 additions & 0 deletions README.rst
Expand Up @@ -208,6 +208,17 @@ javascript
* - ``py://nitpick/resources/javascript/package-json`` (`link <src/nitpick/resources/javascript/package-json.toml>`_)
- `package.json <https://github.com/yarnpkg/website/blob/master/lang/en/docs/package-json.md>`_

presets
~~~~~~~

.. list-table::
:header-rows: 1

* - Style URL
- Description
* - ``py://nitpick/resources/presets/nitpick`` (`link <src/nitpick/resources/presets/nitpick.toml>`_)
- `Default style file for Nitpick <https://nitpick.rtfd.io/>`_

python
~~~~~~

Expand Down
11 changes: 11 additions & 0 deletions docs/library.rst
Expand Up @@ -49,6 +49,17 @@ javascript
* - ``py://nitpick/resources/javascript/package-json`` (`link <https://github.com/andreoliwa/nitpick/blob/v0.31.0/src/nitpick/resources/javascript/package-json.toml>`_)
- `package.json <https://github.com/yarnpkg/website/blob/master/lang/en/docs/package-json.md>`_

presets
~~~~~~~

.. list-table::
:header-rows: 1

* - Style URL
- Description
* - ``py://nitpick/resources/presets/nitpick`` (`link <https://github.com/andreoliwa/nitpick/blob/v0.31.0/src/nitpick/resources/presets/nitpick.toml>`_)
- `Default style file for Nitpick <https://nitpick.rtfd.io/>`_

python
~~~~~~

Expand Down
39 changes: 3 additions & 36 deletions nitpick-style.toml
@@ -1,43 +1,10 @@
# Default style file for nitpick
# https://github.com/andreoliwa/nitpick/blob/v0.31.0/nitpick-style.toml

[nitpick]
minimum_version = "0.10.0"
# Kept here for compatibility, if someone is still accessing the file via GitHub URL.
# This style just includes the built-in preset shipped with Nitpick.

[nitpick.styles]
include = [
# Suggest the current stable Python version for projects
"py://nitpick/resources/python/39",

"py://nitpick/resources/python/absent",
"py://nitpick/resources/python/bandit",
"py://nitpick/resources/python/black",
"py://nitpick/resources/python/flake8",
"py://nitpick/resources/python/pre-commit-hooks",
"py://nitpick/resources/python/isort",
"py://nitpick/resources/python/mypy",
"py://nitpick/resources/python/poetry",
"py://nitpick/resources/python/pylint",
"py://nitpick/resources/python/radon",
"py://nitpick/resources/python/readthedocs",
"py://nitpick/resources/python/sonar-python",
"py://nitpick/resources/python/tox",
"py://nitpick/resources/python/github-workflow",
"py://nitpick/resources/python/autoflake",

"py://nitpick/resources/any/prettier",
"py://nitpick/resources/any/codeclimate",
"py://nitpick/resources/any/commitizen",
"py://nitpick/resources/any/editorconfig",
"py://nitpick/resources/any/git-legal",
"py://nitpick/resources/any/pre-commit-hooks",
"py://nitpick/resources/any/markdownlint",

"py://nitpick/resources/shell/bashate",
"py://nitpick/resources/shell/shellcheck",

"py://nitpick/resources/javascript/package-json",
"py://nitpick/resources/presets/nitpick"
]

[nitpick.files."setup.cfg"]
comma_separated_values = ["flake8.ignore", "flake8.exclude", "isort.skip", "isort.known_first_party"]
1 change: 1 addition & 0 deletions src/nitpick/resources/presets/__init__.py
@@ -0,0 +1 @@
"""Style presets."""
44 changes: 44 additions & 0 deletions src/nitpick/resources/presets/nitpick.toml
@@ -0,0 +1,44 @@
[nitpick.meta]
name = "Default style file for Nitpick"
url = "https://nitpick.rtfd.io/"

[nitpick]
minimum_version = "0.10.0"

[nitpick.styles]
include = [
# Suggest the current stable Python version for projects
"py://nitpick/resources/python/39",

"py://nitpick/resources/python/absent",
"py://nitpick/resources/python/bandit",
"py://nitpick/resources/python/black",
"py://nitpick/resources/python/flake8",
"py://nitpick/resources/python/pre-commit-hooks",
"py://nitpick/resources/python/isort",
"py://nitpick/resources/python/mypy",
"py://nitpick/resources/python/poetry",
"py://nitpick/resources/python/pylint",
"py://nitpick/resources/python/radon",
"py://nitpick/resources/python/readthedocs",
"py://nitpick/resources/python/sonar-python",
"py://nitpick/resources/python/tox",
"py://nitpick/resources/python/github-workflow",
"py://nitpick/resources/python/autoflake",

"py://nitpick/resources/any/prettier",
"py://nitpick/resources/any/codeclimate",
"py://nitpick/resources/any/commitizen",
"py://nitpick/resources/any/editorconfig",
"py://nitpick/resources/any/git-legal",
"py://nitpick/resources/any/pre-commit-hooks",
"py://nitpick/resources/any/markdownlint",

"py://nitpick/resources/shell/bashate",
"py://nitpick/resources/shell/shellcheck",

"py://nitpick/resources/javascript/package-json",
]

[nitpick.files."setup.cfg"]
comma_separated_values = ["flake8.ignore", "flake8.exclude", "isort.skip", "isort.known_first_party"]
9 changes: 6 additions & 3 deletions src/nitpick/style/core.py
Expand Up @@ -36,6 +36,7 @@
from nitpick.style.config import ConfigValidator
from nitpick.style.fetchers import StyleFetcherManager
from nitpick.style.fetchers.github import GitHubURL
from nitpick.style.fetchers.pypackage import PythonPackageProtocol
from nitpick.typedefs import JsonDict, StrOrIterable, StrOrList, mypy_property
from nitpick.violations import Fuss, Reporter, StyleViolations

Expand Down Expand Up @@ -77,9 +78,11 @@ def cache_dir(self) -> Path:
return path

@staticmethod
def get_default_style_url():
"""Return the URL of the default style for the current version."""
return GitHubURL(PROJECT_OWNER, PROJECT_NAME, f"v{__version__}", NITPICK_STYLE_TOML, None).long_protocol_url
def get_default_style_url(github=False):
"""Return the URL of the default style/preset."""
if github:
return GitHubURL(PROJECT_OWNER, PROJECT_NAME, f"v{__version__}", NITPICK_STYLE_TOML, None).long_protocol_url
return f"{PythonPackageProtocol.SHORT.value}://{PROJECT_NAME}/resources/presets/{PROJECT_NAME}"

def find_initial_styles(self, configured_styles: StrOrIterable) -> Iterator[Fuss]:
"""Find the initial style(s) and include them."""
Expand Down
10 changes: 9 additions & 1 deletion src/nitpick/style/fetchers/pypackage.py
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from dataclasses import dataclass
from enum import Enum
from functools import lru_cache
from itertools import chain
from pathlib import Path
Expand Down Expand Up @@ -61,6 +62,13 @@ def raw_content_url(self) -> compat.Traversable:
return compat.files(self.import_path).joinpath(self.resource_name)


class PythonPackageProtocol(Enum):
"""Protocols for the Python package scheme."""

SHORT = "py"
LONG = "pypackage"


@dataclass(repr=True, unsafe_hash=True)
class PythonPackageFetcher(StyleFetcher): # pylint: disable=too-few-public-methods
"""
Expand All @@ -73,7 +81,7 @@ class PythonPackageFetcher(StyleFetcher): # pylint: disable=too-few-public-meth
E.g. ``py://some_package/path/nitpick.toml``.
"""

protocols: tuple[str, ...] = ("py", "pypackage")
protocols: tuple[str, ...] = (PythonPackageProtocol.SHORT.value, PythonPackageProtocol.LONG.value)

def _do_fetch(self, url):
package_url = PythonPackageURL.parse_url(url)
Expand Down
13 changes: 3 additions & 10 deletions tests/test_cli.py
@@ -1,10 +1,9 @@
"""CLI tests."""
import pytest
import responses

from nitpick.constants import DOT_NITPICK_TOML, PYPROJECT_TOML, READ_THE_DOCS_URL, TOOL_NITPICK_KEY
from nitpick.style import StyleManager
from nitpick.style.fetchers.github import GitHubProtocol
from nitpick.style.fetchers.pypackage import PythonPackageProtocol
from tests.helpers import XFAIL_ON_WINDOWS, ProjectMock


Expand Down Expand Up @@ -49,11 +48,8 @@ def test_config_file_already_has_tool_nitpick_section(tmp_path, config_file):
project.cli_init(f"The config file {config_file} already has a [{TOOL_NITPICK_KEY}] section.", exit_code=1)


@responses.activate
def test_create_basic_dot_nitpick_toml(tmp_path):
"""If no config file is found, create a basic .nitpick.toml."""
responses.add(responses.GET, "https://api.github.com/repos/andreoliwa/nitpick", '{"default_branch": "develop"}')

project = ProjectMock(tmp_path, pyproject_toml=False, setup_py=True)
url = StyleManager.get_default_style_url()
project.cli_init(
Expand All @@ -67,14 +63,11 @@ def test_create_basic_dot_nitpick_toml(tmp_path):
style = ["{url}"]
""",
)
assert url.startswith(GitHubProtocol.LONG.value)
assert url.startswith(PythonPackageProtocol.SHORT.value)


@responses.activate
def test_add_tool_nitpick_section_to_pyproject_toml(tmp_path):
"""Add a [tool.nitpick] section to pyproject.toml."""
responses.add(responses.GET, "https://api.github.com/repos/andreoliwa/nitpick", '{"default_branch": "develop"}')

project = ProjectMock(tmp_path).pyproject_toml(
"""
[tool.black]
Expand All @@ -96,4 +89,4 @@ def test_add_tool_nitpick_section_to_pyproject_toml(tmp_path):
style = ["{url}"]
""",
)
assert url.startswith(GitHubProtocol.LONG.value)
assert url.startswith(PythonPackageProtocol.SHORT.value)
29 changes: 2 additions & 27 deletions tests/test_ini.py
Expand Up @@ -4,42 +4,17 @@

from configupdater import ConfigUpdater

from nitpick.constants import EDITOR_CONFIG, PYLINTRC, SETUP_CFG, TOX_INI
from nitpick.constants import EDITOR_CONFIG, SETUP_CFG
from nitpick.plugins.ini import IniPlugin, Violations
from nitpick.violations import Fuss, SharedViolations
from tests.helpers import XFAIL_ON_WINDOWS, ProjectMock
from tests.helpers import ProjectMock


def test_setup_cfg_has_no_configuration(tmp_path):
"""File should not be deleted unless explicitly asked."""
ProjectMock(tmp_path).style("").setup_cfg("").api_check_then_fix()


@XFAIL_ON_WINDOWS
def test_default_style_is_applied(project_default, datadir):
"""Test if the default style is applied on an empty project."""
expected_setup_cfg = (datadir / "1-expected-setup.cfg").read_text()
expected_editor_config = (datadir / "1-expected-editorconfig.ini").read_text()
expected_tox_ini = (datadir / "1-expected-tox.ini").read_text()
expected_pylintrc = (datadir / "1-expected-pylintrc.ini").read_text()
project_default.api_check_then_fix(
Fuss(True, SETUP_CFG, 321, " was not found. Create it with this content:", expected_setup_cfg),
Fuss(True, EDITOR_CONFIG, 321, " was not found. Create it with this content:", expected_editor_config),
Fuss(True, TOX_INI, 321, " was not found. Create it with this content:", expected_tox_ini),
Fuss(True, PYLINTRC, 321, " was not found. Create it with this content:", expected_pylintrc),
partial_names=[SETUP_CFG, EDITOR_CONFIG, TOX_INI, PYLINTRC],
).assert_file_contents(
SETUP_CFG,
expected_setup_cfg,
EDITOR_CONFIG,
expected_editor_config,
TOX_INI,
expected_tox_ini,
PYLINTRC,
expected_pylintrc,
)


def test_comma_separated_keys_on_style_file(tmp_path):
"""Comma separated keys on the style file."""
ProjectMock(tmp_path).style(
Expand Down
21 changes: 0 additions & 21 deletions tests/test_ini/1-expected-editorconfig.ini

This file was deleted.

24 changes: 0 additions & 24 deletions tests/test_ini/1-expected-pylintrc.ini

This file was deleted.

19 changes: 0 additions & 19 deletions tests/test_ini/1-expected-tox.ini

This file was deleted.

17 changes: 14 additions & 3 deletions tests/test_plugin.py
Expand Up @@ -9,6 +9,7 @@
from nitpick.constants import READ_THE_DOCS_URL
from nitpick.core import Nitpick
from nitpick.enums import _OptionMixin
from nitpick.style import StyleManager
from nitpick.violations import Fuss
from tests.helpers import ProjectMock

Expand Down Expand Up @@ -104,19 +105,29 @@ def test_offline_flag_env_variable(tmpdir):
assert Nitpick.singleton().offline is True


def project_github(tmp_path):
"""Project using a style from the Nitpick GitHub repo."""
github_url = StyleManager.get_default_style_url(True)
return ProjectMock(tmp_path).pyproject_toml(
f"""
[tool.nitpick]
style = "{github_url}"
"""
)


@responses.activate
def test_offline_doesnt_raise_connection_error(tmp_path):
"""On offline mode, no requests are made, so no connection errors should be raised."""
responses.add(responses.GET, "https://api.github.com/repos/andreoliwa/nitpick", '{"default_branch": "develop"}')
ProjectMock(tmp_path).flake8(offline=True)
project_github(tmp_path).flake8(offline=True)


@responses.activate
def test_offline_recommend_using_flag(tmp_path, capsys):
"""Recommend using the flag on a connection error."""
responses.add(responses.GET, "https://api.github.com/repos/andreoliwa/nitpick", '{"default_branch": "develop"}')

ProjectMock(tmp_path).flake8()
project_github(tmp_path).flake8()
out, err = capsys.readouterr()
assert out == ""
assert (
Expand Down

0 comments on commit fab55f8

Please sign in to comment.