Skip to content

Commit 99b9d80

Browse files
committed
Add PyPI registry correctly to pool depending on other sources
In the event where we defined sources that were set as secondary = True, we would end up with PyPI being after this source when it should have acted as default in that case. The main issue stems from the fact that it's not because you have sources configured that PyPI should not be a default. Instead, PyPI should be default if there are no sources with secondary = False and not default if there are sources with secondary = True.
1 parent 639d5e0 commit 99b9d80

File tree

6 files changed

+136
-6
lines changed

6 files changed

+136
-6
lines changed

poetry/factory.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ def create_poetry(
8888

8989
poetry.pool.add_repository(repository, is_default, secondary=is_secondary)
9090

91-
# Always put PyPI last to prefer private repositories
92-
# but only if we have no other default source
93-
if not poetry.pool.has_default():
94-
has_sources = bool(sources)
95-
poetry.pool.add_repository(PyPiRepository(), not has_sources, has_sources)
96-
else:
91+
# Put PyPI last to prefer private repositories
92+
# unless we have no default source AND no primary sources
93+
# (default = false, secondary = false)
94+
if poetry.pool.has_default():
9795
if io.is_debug():
9896
io.write_line("Deactivating the PyPI repository")
97+
else:
98+
default = not poetry.pool.has_primary_repositories()
99+
poetry.pool.add_repository(PyPiRepository(), default, not default)
99100

100101
return poetry
101102

poetry/repositories/pool.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(
2222
self._lookup = {} # type: Dict[str, int]
2323
self._repositories = [] # type: List[Repository]
2424
self._default = False
25+
self._has_primary_repositories = False
2526
self._secondary_start_idx = None
2627

2728
for repository in repositories:
@@ -38,6 +39,9 @@ def repositories(self): # type: () -> List[Repository]
3839
def has_default(self): # type: () -> bool
3940
return self._default
4041

42+
def has_primary_repositories(self): # type: () -> bool
43+
return self._has_primary_repositories
44+
4145
def has_repository(self, name): # type: (str) -> bool
4246
name = name.lower() if name is not None else None
4347

@@ -81,6 +85,7 @@ def add_repository(
8185
self._repositories.append(repository)
8286
self._lookup[repository_name] = len(self._repositories) - 1
8387
else:
88+
self._has_primary_repositories = True
8489
if self._secondary_start_idx is None:
8590
self._repositories.append(repository)
8691
self._lookup[repository_name] = len(self._repositories) - 1
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[tool.poetry]
2+
name = "my-package"
3+
version = "1.2.3"
4+
description = "Some description."
5+
authors = [
6+
"Your Name <[email protected]>"
7+
]
8+
license = "MIT"
9+
10+
# Requirements
11+
[tool.poetry.dependencies]
12+
python = "~2.7 || ^3.6"
13+
14+
[tool.poetry.dev-dependencies]
15+
16+
[[tool.poetry.source]]
17+
name = "foo"
18+
url = "https://foo.bar/simple/"
19+
secondary = true
20+
21+
[[tool.poetry.source]]
22+
name = "bar"
23+
url = "https://bar.baz/simple/"
24+
secondary = true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[tool.poetry]
2+
name = "my-package"
3+
version = "1.2.3"
4+
description = "Some description."
5+
authors = [
6+
"Your Name <[email protected]>"
7+
]
8+
license = "MIT"
9+
10+
# Requirements
11+
[tool.poetry.dependencies]
12+
python = "~2.7 || ^3.6"
13+
14+
[tool.poetry.dev-dependencies]
15+
16+
[[tool.poetry.source]]
17+
name = "foo"
18+
url = "https://foo.bar/simple/"
19+
secondary = true
20+
21+
[[tool.poetry.source]]
22+
name = "bar"
23+
url = "https://bar.baz/simple/"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[tool.poetry]
2+
name = "my-package"
3+
version = "1.2.3"
4+
description = "Some description."
5+
authors = [
6+
"Your Name <[email protected]>"
7+
]
8+
license = "MIT"
9+
10+
# Requirements
11+
[tool.poetry.dependencies]
12+
python = "~2.7 || ^3.6"
13+
14+
[tool.poetry.dev-dependencies]
15+
16+
[[tool.poetry.source]]
17+
name = "foo"
18+
url = "https://foo.bar/simple/"
19+
secondary = true

tests/test_factory.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,64 @@ def test_poetry_with_non_default_source():
166166
assert isinstance(poetry.pool.repositories[1], PyPiRepository)
167167

168168

169+
def test_poetry_with_non_default_secondary_source():
170+
poetry = Factory().create_poetry(fixtures_dir / "with_non_default_secondary_source")
171+
172+
assert len(poetry.pool.repositories) == 2
173+
174+
assert poetry.pool.has_default()
175+
176+
repository = poetry.pool.repositories[0]
177+
assert repository.name == "PyPI"
178+
assert isinstance(repository, PyPiRepository)
179+
180+
repository = poetry.pool.repositories[1]
181+
assert repository.name == "foo"
182+
assert isinstance(repository, LegacyRepository)
183+
184+
185+
def test_poetry_with_non_default_multiple_secondary_sources():
186+
poetry = Factory().create_poetry(
187+
fixtures_dir / "with_non_default_multiple_secondary_sources"
188+
)
189+
190+
assert len(poetry.pool.repositories) == 3
191+
192+
assert poetry.pool.has_default()
193+
194+
repository = poetry.pool.repositories[0]
195+
assert repository.name == "PyPI"
196+
assert isinstance(repository, PyPiRepository)
197+
198+
repository = poetry.pool.repositories[1]
199+
assert repository.name == "foo"
200+
assert isinstance(repository, LegacyRepository)
201+
202+
repository = poetry.pool.repositories[2]
203+
assert repository.name == "bar"
204+
assert isinstance(repository, LegacyRepository)
205+
206+
207+
def test_poetry_with_non_default_multiple_sources():
208+
poetry = Factory().create_poetry(fixtures_dir / "with_non_default_multiple_sources")
209+
210+
assert len(poetry.pool.repositories) == 3
211+
212+
assert not poetry.pool.has_default()
213+
214+
repository = poetry.pool.repositories[0]
215+
assert repository.name == "bar"
216+
assert isinstance(repository, LegacyRepository)
217+
218+
repository = poetry.pool.repositories[1]
219+
assert repository.name == "foo"
220+
assert isinstance(repository, LegacyRepository)
221+
222+
repository = poetry.pool.repositories[2]
223+
assert repository.name == "PyPI"
224+
assert isinstance(repository, PyPiRepository)
225+
226+
169227
def test_poetry_with_no_default_source():
170228
poetry = Factory().create_poetry(fixtures_dir / "sample_project")
171229

0 commit comments

Comments
 (0)