Skip to content

Commit

Permalink
cli: add global --no-cache option
Browse files Browse the repository at this point in the history
  • Loading branch information
abn committed May 5, 2022
1 parent f67c9e1 commit be56203
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
33 changes: 28 additions & 5 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import logging

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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():
Expand All @@ -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

Expand All @@ -175,6 +197,7 @@ def create_legacy_repository(
name,
url,
config=auth_config,
disable_cache=disable_cache,
)

@classmethod
Expand Down
6 changes: 5 additions & 1 deletion tests/console/commands/plugin/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions tests/console/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from typing import TYPE_CHECKING

import pytest

from cleo.testers.application_tester import ApplicationTester
from entrypoints import EntryPoint

Expand Down Expand Up @@ -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

0 comments on commit be56203

Please sign in to comment.