Skip to content

Commit

Permalink
Use configured repositories as sources by default
Browse files Browse the repository at this point in the history
This change adds globally configured extra repositories from config.toml
as dependency sources. These are added after project specific sources
and before PyPI.

Resolves: python-poetry#625
  • Loading branch information
abn committed Nov 30, 2018
1 parent 7018bd3 commit 7c00dd9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/docs/repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ with the `--username` and `--password` options.
Now that you can publish to your private repository, you need to be able to
install dependencies from it.

For that, you have to edit your `pyproject.toml` file, like so
By default, Poetry uses all repositories configured as shown above for package
installation.

If you would like to use a repository that was not configured like above, you have to
edit your `pyproject.toml` file, like so

```toml
[[tool.poetry.source]]
Expand Down
11 changes: 11 additions & 0 deletions poetry/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def __init__(
for source in self._local_config.get("source", []):
self._pool.add_repository(self.create_legacy_repository(source))

# Add any additional repositories configured globally as source
# This is done after local config sources and before PyPI
extra_repositories = self._config.setting("repositories", default={})
for repository_name, repository_config in extra_repositories.items():
if "url" in repository_config:
self._pool.add_repository(
self.create_legacy_repository(
{"name": repository_name, "url": repository_config["url"]}
)
)

# Always put PyPI last to prefer private repositories
self._pool.add_repository(PyPiRepository())

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/config_with_repositories/auth.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[http-basic]
5 changes: 5 additions & 0 deletions tests/fixtures/config_with_repositories/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[settings]

[repositories]
[repositories.foo]
url = "https://baz.com"
5 changes: 5 additions & 0 deletions tests/fixtures/simple_project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ classifiers = [
# Requirements
[tool.poetry.dependencies]
python = "~2.7 || ^3.6"

# Extra Source
[[tool.poetry.source]]
name = "foo"
url = "https://bar.com"
26 changes: 26 additions & 0 deletions tests/test_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
from __future__ import unicode_literals

from poetry.poetry import Poetry
from poetry.repositories.pypi_repository import PyPiRepository
from poetry.utils._compat import PY2
from poetry.utils._compat import Path
from poetry.utils.toml_file import TomlFile

if PY2:
from mock import patch
else:
from unittest.mock import patch


fixtures_dir = Path(__file__).parent / "fixtures"

Expand Down Expand Up @@ -152,3 +158,23 @@ def test_check_fails():
)

assert Poetry.check(content) == {"errors": [expected], "warnings": []}


@patch("poetry.config.CONFIG_DIR", fixtures_dir / "config_with_repositories")
def test_sources():
poetry = Poetry.create(str(fixtures_dir / "simple_project"))
repositories = [
r._url for r in poetry.pool.repositories if isinstance(r, PyPiRepository)
]

source_local = "https://bar.com"
source_global = "https://baz.com"
source_pypi = "https://pypi.org/"

assert source_local in repositories
assert source_global in repositories
assert source_pypi in repositories

assert repositories.index(source_local) < repositories.index(source_global)
assert repositories.index(source_global) < repositories.index(source_pypi)
assert repositories.index(source_pypi) == len(repositories) - 1

0 comments on commit 7c00dd9

Please sign in to comment.