From 860369442ba92b1b65ced44671a9042b19435e44 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Fri, 29 Apr 2022 16:00:57 +0200 Subject: [PATCH] cli: add global --no-cache option --- docs/cli.md | 2 ++ src/poetry/console/application.py | 13 ++++++++- src/poetry/factory.py | 33 +++++++++++++++++++---- tests/console/commands/plugin/conftest.py | 6 ++++- tests/console/test_application.py | 22 +++++++++++++++ 5 files changed, 69 insertions(+), 7 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index 1ce86fcc149..ff543143ccf 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -27,6 +27,8 @@ then `--help` combined with any of those can give you more information. * `--no-ansi`: Disable ANSI output. * `--version (-V)`: Display this application version. * `--no-interaction (-n)`: Do not ask any interactive question. +* `--no-plugins`: Disables plugins. +* `--no-cache`: Disables Poetry source caches. ## new diff --git a/src/poetry/console/application.py b/src/poetry/console/application.py index 9d9c7ed581a..ba1cb2c4828 100644 --- a/src/poetry/console/application.py +++ b/src/poetry/console/application.py @@ -96,6 +96,7 @@ def __init__(self) -> None: self._poetry: Poetry | None = None self._io: IO | None = None self._disable_plugins = False + self._disable_cache = False self._plugins_loaded = False dispatcher = EventDispatcher() @@ -117,7 +118,10 @@ def poetry(self) -> Poetry: return self._poetry self._poetry = Factory().create_poetry( - Path.cwd(), io=self._io, disable_plugins=self._disable_plugins + Path.cwd(), + io=self._io, + disable_plugins=self._disable_plugins, + disable_cache=self._disable_cache, ) return self._poetry @@ -168,6 +172,7 @@ def render_error(self, error: Exception, io: IO) -> None: def _run(self, io: IO) -> int: self._disable_plugins = io.input.parameter_option("--no-plugins") + self._disable_cache = io.input.has_parameter_option("--no-cache") self._load_plugins(io) @@ -347,6 +352,12 @@ def _default_definition(self) -> Definition: Option("--no-plugins", flag=True, description="Disables plugins.") ) + definition.add_option( + Option( + "--no-cache", flag=True, description="Disables Poetry source caches." + ) + ) + return definition def _get_solution_provider_repository(self) -> SolutionProviderRepository: diff --git a/src/poetry/factory.py b/src/poetry/factory.py index e9ee9d5866b..376baf00ed6 100644 --- a/src/poetry/factory.py +++ b/src/poetry/factory.py @@ -1,5 +1,7 @@ from __future__ import annotations +import logging + from pathlib import Path from typing import TYPE_CHECKING from typing import Any @@ -27,6 +29,9 @@ from poetry.repositories.legacy_repository import LegacyRepository +logger = logging.getLogger(__name__) + + class Factory(BaseFactory): """ Factory class to create various elements needed by Poetry. @@ -37,6 +42,7 @@ def create_poetry( cwd: Path | None = None, io: IO | None = None, disable_plugins: bool = False, + disable_cache: bool = False, ) -> Poetry: if io is None: io = NullIO() @@ -79,7 +85,11 @@ def create_poetry( # Configuring sources self.configure_sources( - poetry, poetry.local_config.get("source", []), config, io + poetry, + poetry.local_config.get("source", []), + config, + io, + disable_cache=disable_cache, ) plugin_manager = PluginManager(Plugin.group, disable_plugins=disable_plugins) @@ -127,10 +137,20 @@ def create_config(cls, io: IO | None = None) -> Config: @classmethod def configure_sources( - cls, poetry: Poetry, sources: list[dict[str, str]], config: Config, io: IO + cls, + poetry: Poetry, + sources: list[dict[str, str]], + config: Config, + io: IO, + disable_cache: bool = False, ) -> None: + if disable_cache: + logger.debug("Disabling source caches") + for source in sources: - repository = cls.create_legacy_repository(source, config) + repository = cls.create_legacy_repository( + source, config, disable_cache=disable_cache + ) is_default = bool(source.get("default", False)) is_secondary = bool(source.get("secondary", False)) if io.is_debug(): @@ -154,11 +174,13 @@ def configure_sources( from poetry.repositories.pypi_repository import PyPiRepository default = not poetry.pool.has_primary_repositories() - poetry.pool.add_repository(PyPiRepository(), default, not default) + poetry.pool.add_repository( + PyPiRepository(disable_cache=disable_cache), default, not default + ) @classmethod def create_legacy_repository( - cls, source: dict[str, str], auth_config: Config + cls, source: dict[str, str], auth_config: Config, disable_cache: bool = False ) -> LegacyRepository: from poetry.repositories.legacy_repository import LegacyRepository from poetry.utils.helpers import get_cert @@ -179,6 +201,7 @@ def create_legacy_repository( config=auth_config, cert=get_cert(auth_config, name), client_cert=get_client_cert(auth_config, name), + disable_cache=disable_cache, ) @classmethod diff --git a/tests/console/commands/plugin/conftest.py b/tests/console/commands/plugin/conftest.py index 2c7f19f264c..025772c805f 100644 --- a/tests/console/commands/plugin/conftest.py +++ b/tests/console/commands/plugin/conftest.py @@ -37,7 +37,11 @@ def installed() -> InstalledRepository: def configure_sources_factory(repo: TestRepository) -> SourcesFactory: def _configure_sources( - poetry: Poetry, sources: Source, config: Config, io: IO + poetry: Poetry, + sources: Source, + config: Config, + io: IO, + disable_cache: bool = False, ) -> None: pool = Pool() pool.add_repository(repo) diff --git a/tests/console/test_application.py b/tests/console/test_application.py index 3833d809228..6e65b742592 100644 --- a/tests/console/test_application.py +++ b/tests/console/test_application.py @@ -4,6 +4,8 @@ from typing import TYPE_CHECKING +import pytest + from cleo.testers.application_tester import ApplicationTester from entrypoints import EntryPoint @@ -108,3 +110,23 @@ def test_application_execute_plugin_command_with_plugins_disabled( assert tester.io.fetch_output() == "" assert tester.io.fetch_error() == '\nThe command "foo" does not exist.\n' assert tester.status_code == 1 + + +@pytest.mark.parametrize("disable_cache", [True, False]) +def test_application_verify_source_cache_flag(disable_cache: bool): + app = Application() + + tester = ApplicationTester(app) + command = "debug info" + + if disable_cache: + command = f"{command} --no-cache" + + assert not app._poetry + + tester.execute(command) + + assert app.poetry.pool.repositories + + for repo in app.poetry.pool.repositories: + assert repo._disable_cache == disable_cache