Skip to content

Commit 8603694

Browse files
abnneersighted
authored andcommitted
cli: add global --no-cache option
1 parent bd58398 commit 8603694

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

docs/cli.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ then `--help` combined with any of those can give you more information.
2727
* `--no-ansi`: Disable ANSI output.
2828
* `--version (-V)`: Display this application version.
2929
* `--no-interaction (-n)`: Do not ask any interactive question.
30+
* `--no-plugins`: Disables plugins.
31+
* `--no-cache`: Disables Poetry source caches.
3032

3133

3234
## new

src/poetry/console/application.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def __init__(self) -> None:
9696
self._poetry: Poetry | None = None
9797
self._io: IO | None = None
9898
self._disable_plugins = False
99+
self._disable_cache = False
99100
self._plugins_loaded = False
100101

101102
dispatcher = EventDispatcher()
@@ -117,7 +118,10 @@ def poetry(self) -> Poetry:
117118
return self._poetry
118119

119120
self._poetry = Factory().create_poetry(
120-
Path.cwd(), io=self._io, disable_plugins=self._disable_plugins
121+
Path.cwd(),
122+
io=self._io,
123+
disable_plugins=self._disable_plugins,
124+
disable_cache=self._disable_cache,
121125
)
122126

123127
return self._poetry
@@ -168,6 +172,7 @@ def render_error(self, error: Exception, io: IO) -> None:
168172

169173
def _run(self, io: IO) -> int:
170174
self._disable_plugins = io.input.parameter_option("--no-plugins")
175+
self._disable_cache = io.input.has_parameter_option("--no-cache")
171176

172177
self._load_plugins(io)
173178

@@ -347,6 +352,12 @@ def _default_definition(self) -> Definition:
347352
Option("--no-plugins", flag=True, description="Disables plugins.")
348353
)
349354

355+
definition.add_option(
356+
Option(
357+
"--no-cache", flag=True, description="Disables Poetry source caches."
358+
)
359+
)
360+
350361
return definition
351362

352363
def _get_solution_provider_repository(self) -> SolutionProviderRepository:

src/poetry/factory.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import logging
4+
35
from pathlib import Path
46
from typing import TYPE_CHECKING
57
from typing import Any
@@ -27,6 +29,9 @@
2729
from poetry.repositories.legacy_repository import LegacyRepository
2830

2931

32+
logger = logging.getLogger(__name__)
33+
34+
3035
class Factory(BaseFactory):
3136
"""
3237
Factory class to create various elements needed by Poetry.
@@ -37,6 +42,7 @@ def create_poetry(
3742
cwd: Path | None = None,
3843
io: IO | None = None,
3944
disable_plugins: bool = False,
45+
disable_cache: bool = False,
4046
) -> Poetry:
4147
if io is None:
4248
io = NullIO()
@@ -79,7 +85,11 @@ def create_poetry(
7985

8086
# Configuring sources
8187
self.configure_sources(
82-
poetry, poetry.local_config.get("source", []), config, io
88+
poetry,
89+
poetry.local_config.get("source", []),
90+
config,
91+
io,
92+
disable_cache=disable_cache,
8393
)
8494

8595
plugin_manager = PluginManager(Plugin.group, disable_plugins=disable_plugins)
@@ -127,10 +137,20 @@ def create_config(cls, io: IO | None = None) -> Config:
127137

128138
@classmethod
129139
def configure_sources(
130-
cls, poetry: Poetry, sources: list[dict[str, str]], config: Config, io: IO
140+
cls,
141+
poetry: Poetry,
142+
sources: list[dict[str, str]],
143+
config: Config,
144+
io: IO,
145+
disable_cache: bool = False,
131146
) -> None:
147+
if disable_cache:
148+
logger.debug("Disabling source caches")
149+
132150
for source in sources:
133-
repository = cls.create_legacy_repository(source, config)
151+
repository = cls.create_legacy_repository(
152+
source, config, disable_cache=disable_cache
153+
)
134154
is_default = bool(source.get("default", False))
135155
is_secondary = bool(source.get("secondary", False))
136156
if io.is_debug():
@@ -154,11 +174,13 @@ def configure_sources(
154174
from poetry.repositories.pypi_repository import PyPiRepository
155175

156176
default = not poetry.pool.has_primary_repositories()
157-
poetry.pool.add_repository(PyPiRepository(), default, not default)
177+
poetry.pool.add_repository(
178+
PyPiRepository(disable_cache=disable_cache), default, not default
179+
)
158180

159181
@classmethod
160182
def create_legacy_repository(
161-
cls, source: dict[str, str], auth_config: Config
183+
cls, source: dict[str, str], auth_config: Config, disable_cache: bool = False
162184
) -> LegacyRepository:
163185
from poetry.repositories.legacy_repository import LegacyRepository
164186
from poetry.utils.helpers import get_cert
@@ -179,6 +201,7 @@ def create_legacy_repository(
179201
config=auth_config,
180202
cert=get_cert(auth_config, name),
181203
client_cert=get_client_cert(auth_config, name),
204+
disable_cache=disable_cache,
182205
)
183206

184207
@classmethod

tests/console/commands/plugin/conftest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ def installed() -> InstalledRepository:
3737

3838
def configure_sources_factory(repo: TestRepository) -> SourcesFactory:
3939
def _configure_sources(
40-
poetry: Poetry, sources: Source, config: Config, io: IO
40+
poetry: Poetry,
41+
sources: Source,
42+
config: Config,
43+
io: IO,
44+
disable_cache: bool = False,
4145
) -> None:
4246
pool = Pool()
4347
pool.add_repository(repo)

tests/console/test_application.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from typing import TYPE_CHECKING
66

7+
import pytest
8+
79
from cleo.testers.application_tester import ApplicationTester
810
from entrypoints import EntryPoint
911

@@ -108,3 +110,23 @@ def test_application_execute_plugin_command_with_plugins_disabled(
108110
assert tester.io.fetch_output() == ""
109111
assert tester.io.fetch_error() == '\nThe command "foo" does not exist.\n'
110112
assert tester.status_code == 1
113+
114+
115+
@pytest.mark.parametrize("disable_cache", [True, False])
116+
def test_application_verify_source_cache_flag(disable_cache: bool):
117+
app = Application()
118+
119+
tester = ApplicationTester(app)
120+
command = "debug info"
121+
122+
if disable_cache:
123+
command = f"{command} --no-cache"
124+
125+
assert not app._poetry
126+
127+
tester.execute(command)
128+
129+
assert app.poetry.pool.repositories
130+
131+
for repo in app.poetry.pool.repositories:
132+
assert repo._disable_cache == disable_cache

0 commit comments

Comments
 (0)