From cc78cfcbd68b66cf15c93152e113ddf6640c88a0 Mon Sep 17 00:00:00 2001 From: Martijn Pieters Date: Mon, 28 Mar 2022 14:28:43 +0100 Subject: [PATCH] fix: actually show chosen styles in log output There were two issues with the log output: - It always claimed the configured styles came from pyproject.toml even when .nitpick.toml is actually used. - The chosen styles were shown as `` --- src/nitpick/project.py | 3 +-- src/nitpick/style/core.py | 5 +++-- tests/test_project.py | 13 +++++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/nitpick/project.py b/src/nitpick/project.py index 5bce22e5..61affc9f 100644 --- a/src/nitpick/project.py +++ b/src/nitpick/project.py @@ -10,7 +10,6 @@ from autorepr import autorepr from loguru import logger from marshmallow_polyfield import PolyField -from more_itertools import peekable from more_itertools.more import always_iterable from pluggy import PluginManager from tomlkit.items import KeyType, SingleKey @@ -182,7 +181,7 @@ def merge_styles(self, offline: bool) -> Iterator[Fuss]: style = StyleManager(self, offline, config.cache) base = config.file.expanduser().resolve().as_uri() if config.file else None - style_errors = list(style.find_initial_styles(peekable(always_iterable(config.styles)), base)) + style_errors = list(style.find_initial_styles(list(always_iterable(config.styles)), base)) if style_errors: raise QuitComplainingError(style_errors) diff --git a/src/nitpick/style/core.py b/src/nitpick/style/core.py index 51b8c8dc..ae909950 100644 --- a/src/nitpick/style/core.py +++ b/src/nitpick/style/core.py @@ -93,10 +93,12 @@ def find_initial_styles(self, configured_styles: Sequence[str], base: str | None """ project_root = self.project.root + base_url = furl(base or project_root.resolve().as_uri()) if configured_styles: chosen_styles = configured_styles - logger.info(f"Using styles configured in {PYPROJECT_TOML}: {chosen_styles}") + config_file = base_url.path.segments[-1] if base else PYPROJECT_TOML + logger.info(f"Using styles configured in {config_file}: {', '.join(chosen_styles)}") else: paths = glob_files(project_root, [NITPICK_STYLE_TOML]) if paths: @@ -107,7 +109,6 @@ def find_initial_styles(self, configured_styles: Sequence[str], base: str | None log_message = "Using default remote Nitpick style" logger.info(f"{log_message}: {chosen_styles[0]}") - base_url = furl(base or project_root.resolve().as_uri()) yield from self.include_multiple_styles( self._style_fetcher_manager.normalize_url(ref, base_url) for ref in chosen_styles ) diff --git a/tests/test_project.py b/tests/test_project.py index 2f119253..a43c1e98 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -20,6 +20,7 @@ from nitpick.core import Nitpick from nitpick.exceptions import QuitComplainingError from nitpick.project import Configuration, confirm_project_root, find_main_python_file +from nitpick.style import StyleManager from nitpick.violations import ProjectViolations from tests.helpers import ProjectMock @@ -124,8 +125,10 @@ def test_django_project_structure(tmp_path): def test_when_no_config_file_the_default_style_is_requested(tmp_path, caplog): """There is a root dir (setup.py), but no config file.""" project = ProjectMock(tmp_path, pyproject_toml=False, setup_py=True).api_check(offline=True) + style_url = StyleManager.get_default_style_url() assert project.nitpick_instance.project.read_configuration() == Configuration(None, [], "") - assert "Config file: none found" in caplog.text + assert "Config file: none found {}" in caplog.messages + assert f"Using default remote Nitpick style: {style_url} {{}}" in caplog.messages @pytest.mark.parametrize("config_file", [DOT_NITPICK_TOML, PYPROJECT_TOML]) @@ -142,7 +145,8 @@ def test_has_one_config_file(tmp_path, config_file, caplog): ).api_check(offline=True) path = project.root_dir / config_file assert project.nitpick_instance.project.read_configuration() == Configuration(path, ["local.toml"], "forever") - assert f"Config file: reading from {path}" in caplog.text + assert f"Config file: reading from {path} {{}}" in caplog.messages + assert f"Using styles configured in {config_file}: local.toml {{}}" in caplog.messages def test_has_multiple_config_files(tmp_path, caplog): @@ -168,8 +172,9 @@ def test_has_multiple_config_files(tmp_path, caplog): assert project.nitpick_instance.project.read_configuration() == Configuration( project.root_dir / DOT_NITPICK_TOML, ["local_nit.toml"], "never" ) - assert f"Config file: reading from {project.root_dir / DOT_NITPICK_TOML}" in caplog.text - assert f"Config file: ignoring existing {project.root_dir / PYPROJECT_TOML}" in caplog.text + assert f"Config file: reading from {project.root_dir / DOT_NITPICK_TOML} {{}}" in caplog.messages + assert f"Config file: ignoring existing {project.root_dir / PYPROJECT_TOML} {{}}" in caplog.messages + assert f"Using styles configured in {DOT_NITPICK_TOML}: local_nit.toml {{}}" in caplog.messages @pytest.mark.parametrize(