Commit 86036944 by Arun Babu Neelicattu Committed by Bjorn Neergaard

cli: add global --no-cache option

parent bd583981
......@@ -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
......
......@@ -96,6 +96,7 @@ class Application(BaseApplication):
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 @@ class Application(BaseApplication):
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 @@ class Application(BaseApplication):
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 @@ class Application(BaseApplication):
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:
......
from __future__ import annotations
import logging
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
......@@ -27,6 +29,9 @@ if TYPE_CHECKING:
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 @@ class Factory(BaseFactory):
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 @@ class Factory(BaseFactory):
# 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 @@ class Factory(BaseFactory):
@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 @@ class Factory(BaseFactory):
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 @@ class Factory(BaseFactory):
config=auth_config,
cert=get_cert(auth_config, name),
client_cert=get_client_cert(auth_config, name),
disable_cache=disable_cache,
)
@classmethod
......
......@@ -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)
......
......@@ -4,6 +4,8 @@ import re
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment