Skip to content

Commit

Permalink
feat(source/show): notify when PyPI is implicit
Browse files Browse the repository at this point in the history
Add a notice when all sources are listed and PyPI is implicitly enabled.
  • Loading branch information
abn committed Jan 10, 2025
1 parent 0e4f0e7 commit 353dc1f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/poetry/console/commands/source/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@ class SourceShowCommand(Command):
),
]

def notify_implicit_pypi(self) -> None:
if not self.poetry.pool.has_repository("pypi"):
return

self.line(
"<c1><b>PyPI</> is implicitly enabled as a <b>primary</> source. "
"If you wish to disable it, or alter its priority please refer to "
"<b>https://python-poetry.org/docs/repositories/#package-sources</>.</>"
)
self.line("")

def handle(self) -> int:
sources = self.poetry.get_sources()
names = self.argument("source")
lower_names = [name.lower() for name in names]

if not sources:
self.line("No sources configured for this project.")
self.line("No sources configured for this project.\n")
self.notify_implicit_pypi()
return 0

if names and not any(s.name.lower() in lower_names for s in sources):
Expand All @@ -42,10 +54,15 @@ def handle(self) -> int:
)
return 1

is_pypi_implicit = True

for source in sources:
if names and source.name.lower() not in lower_names:
continue

if source.name.lower() == "pypi":
is_pypi_implicit = False

table = self.table(style="compact")
rows: Rows = [["<info>name</>", f" : <c1>{source.name}</>"]]
if source.url:
Expand All @@ -55,4 +72,7 @@ def handle(self) -> int:
table.render()
self.line("")

if not names and is_pypi_implicit:
self.notify_implicit_pypi()

return 0
16 changes: 16 additions & 0 deletions tests/console/commands/source/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pytest

from poetry.repositories.pypi_repository import PyPiRepository


if TYPE_CHECKING:
from cleo.testers.command_tester import CommandTester
Expand Down Expand Up @@ -178,6 +180,20 @@ def test_source_show_no_sources(tester_no_sources: CommandTester) -> None:
assert tester_no_sources.status_code == 0


def test_source_show_no_sources_implicit_pypi(
tester_no_sources: CommandTester, poetry_without_source: Poetry
) -> None:
poetry_without_source.pool.add_repository(PyPiRepository())
tester_no_sources.execute("")

output = tester_no_sources.io.fetch_output().strip()

assert "No sources configured for this project." in output
assert "PyPI is implicitly enabled as a primary source." in output

assert tester_no_sources.status_code == 0


def test_source_show_error(tester: CommandTester) -> None:
tester.execute("error")
assert tester.io.fetch_error().strip() == "No source found with name(s): error"
Expand Down

0 comments on commit 353dc1f

Please sign in to comment.