Skip to content

Commit

Permalink
Fixed capitalized package bug on poetry init
Browse files Browse the repository at this point in the history
Used canonicalize_name to standardize the names of all packages in
poetry init with interactive dependencies. Given its importance, added a
test_canoncalize_name() test to tests/utils/test_helpers.py
  • Loading branch information
Evan Rittenhouse committed Jan 29, 2022
1 parent 773b4ec commit d3ffb97
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def _determine_requirements(
package = self.ask(
"Search for package to add (or leave blank to continue):"
)
while package is not None:
while package:
constraint = self._parse_requirements([package])[0]
if (
"git" in constraint
Expand All @@ -268,15 +268,14 @@ def _determine_requirements(
package = self.ask("\nAdd a package:")
continue

matches = self._get_pool().search(constraint["name"])

canonicalized_name = canonicalize_name(constraint["name"])
matches = self._get_pool().search(canonicalized_name)
if not matches:
self.line("<error>Unable to find package</error>")
package = False
else:
choices = []
matches_names = [p.name for p in matches]
canonicalized_name = canonicalize_name(constraint["name"])
exact_match = canonicalized_name in matches_names
if exact_match:
choices.append(
Expand All @@ -287,7 +286,7 @@ def _determine_requirements(
if len(choices) >= 10:
break

if found_package.name.lower() == constraint["name"].lower():
if found_package.name.lower() == canonicalized_name:
continue

choices.append(found_package.pretty_name)
Expand Down
5 changes: 5 additions & 0 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def test_interactive_with_dependencies(tester: CommandTester, repo: "TestReposit
repo.add_package(get_package("django-pendulum", "0.1.6-pre4"))
repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0"))
repo.add_package(get_package("flask", "2.0.0"))

inputs = [
"my-package", # Package name
Expand All @@ -135,6 +136,9 @@ def test_interactive_with_dependencies(tester: CommandTester, repo: "TestReposit
"pendulu", # Search for package
"1", # Second option is pendulum
"", # Do not set constraint
"Flask",
"0",
"",
"", # Stop searching for packages
"", # Interactive dev packages
"pytest", # Search for package
Expand All @@ -158,6 +162,7 @@ def test_interactive_with_dependencies(tester: CommandTester, repo: "TestReposit
[tool.poetry.dependencies]
python = "~2.7 || ^3.6"
pendulum = "^2.0.0"
flask = "^2.0.0"
[tool.poetry.group.dev.dependencies]
pytest = "^3.6.0"
Expand Down
15 changes: 15 additions & 0 deletions tests/utils/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from poetry.core.utils.helpers import parse_requires

from poetry.utils.helpers import canonicalize_name
from poetry.utils.helpers import get_cert
from poetry.utils.helpers import get_client_cert

Expand Down Expand Up @@ -79,3 +80,17 @@ def test_get_client_cert(config: "Config"):
config.merge({"certificates": {"foo": {"client-cert": client_cert}}})

assert get_client_cert(config, "foo") == Path(client_cert)


def test_canonicalize_name():
names = ["flask", "Flask", "FLASK", "FlAsK", "fLaSk57", "flask-57"]
canonicalized_names = [canonicalize_name(name) for name in names]

assert canonicalized_names == [
"flask",
"flask",
"flask",
"flask",
"flask57",
"flask-57",
]

0 comments on commit d3ffb97

Please sign in to comment.