def test_authenticator_with_implicit_repository_configuration( http: type[httpretty.httpretty], config: Config, repositories: dict[str, dict[str, str]], ) -> None: http.register_uri( http.GET, re.compile("^https?://foo.bar/(.+?)$"), ) config.merge( { "repositories": repositories, "http-basic": { "source": {"username": "******", "password": "******"}, "publish": {"username": "******", "password": "******"}, }, } ) repo = LegacyRepository(name="source", url="https://foo.bar/simple", config=config) repo._get_page("/foo") request = http.last_request() basic_auth = base64.b64encode(b"foo:bar").decode() assert request.headers["Authorization"] == f"Basic {basic_auth}"
def test_add_constraint_with_source(app, poetry, tester): repo = LegacyRepository(name="my-index", url="https://my-index.fake") repo.add_package(get_package("cachy", "0.2.0")) repo._cache.store("matches").put("cachy:0.2.0", [Version.parse("0.2.0")], 5) poetry.pool.add_repository(repo) tester.execute("cachy=0.2.0 --source my-index") expected = """\ Updating dependencies Resolving dependencies... Writing lock file Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.2.0) """ assert expected == tester.io.fetch_output() assert 1 == tester._command.installer.executor.installations_count content = app.poetry.file.read()["tool"]["poetry"] assert "cachy" in content["dependencies"] assert content["dependencies"]["cachy"] == { "version": "0.2.0", "source": "my-index", }
def test_install_with_non_pypi_default_repository(pool: Pool, installer: PipInstaller): default = LegacyRepository("default", "https://default.com") another = LegacyRepository("another", "https://another.com") pool.add_repository(default, default=True) pool.add_repository(another) foo = Package( "foo", "0.0.0", source_type="legacy", source_reference=default.name, source_url=default.url, ) bar = Package( "bar", "0.1.0", source_type="legacy", source_reference=another.name, source_url=another.url, ) installer.install(foo) installer.install(bar)
def test_install_with_certs(mocker: MockerFixture, key: str, option: str): client_path = "path/to/client.pem" mocker.patch( "poetry.utils.authenticator.Authenticator.get_certs_for_url", return_value={key: client_path}, ) default = LegacyRepository("default", "https://foo.bar") pool = Pool() pool.add_repository(default, default=True) null_env = NullEnv() installer = PipInstaller(null_env, NullIO(), pool) foo = Package( "foo", "0.0.0", source_type="legacy", source_reference=default.name, source_url=default.url, ) installer.install(foo) assert len(null_env.executed) == 1 cmd = null_env.executed[0] assert f"--{option}" in cmd cert_index = cmd.index(f"--{option}") # Need to do the str(Path()) bit because Windows paths get modified by Path assert cmd[cert_index + 1] == str(Path(client_path))
def use_current_devpi_index(event, event_name, _): # type: (PreHandleEvent, str, _) -> None # TODO: local import to avoid circular dependencies, clean-up once Poetry's plug-in systems arrives from poetry.repositories.legacy_repository import LegacyRepository # TODO: this hook is fired too late, we need to mutating Repository._url below. Ideally we would change # repository settings directly after parsing pyproject.toml before the pool is even initialized command = event.command.config.handler # type: EnvCommand devpi_args = _get_devpi_args([]) hub = Hub(devpi_args) current_devpi_index = "{}/+simple".format(hub.current.index) # replace any repository with the current devpi index, no need for caching as devpi already takes care # of this if hasattr(command, 'poetry'): command.poetry.pool.repositories[:] = [ LegacyRepository(r.name, current_devpi_index, disable_cache=True) for r in command.poetry.pool.repositories ] if command.poetry.locker: # if we got a lock file, change all package.source to point at the current index for info in command.poetry.locker.lock_data.get('package', []): source = info.get('source') if source and source.get( 'url', current_devpi_index) != current_devpi_index: event.io.write( "<warning>Package '{}' was installed from '{}' " "which is not the currently active index. " "Will override with current index..</warning>\n". format(info['name'], source['url'])) source['url'] = current_devpi_index
def test_pool_raises_connection_error_when_offline(): pool = Pool() pool.add_repository( LegacyRepository(url="http://fake.url/simple", name="fake")) with pytest.raises(ConnectionError): pool.package("foo", "1.0.0")
def test_exporter_exports_requirements_txt_with_legacy_packages( tmp_dir, poetry): poetry.pool.add_repository( LegacyRepository( "custom", "https://example.com/simple", )) poetry.locker.mock_lock_data({ "package": [ { "name": "foo", "version": "1.2.3", "category": "main", "optional": False, "python-versions": "*", }, { "name": "bar", "version": "4.5.6", "category": "dev", "optional": False, "python-versions": "*", "source": { "type": "legacy", "url": "https://example.com/simple", "reference": "", }, }, ], "metadata": { "python-versions": "*", "content-hash": "123456789", "hashes": { "foo": ["12345"], "bar": ["67890"] }, }, }) set_package_requires(poetry) exporter = Exporter(poetry) exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt", dev=True) with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = """\ --extra-index-url https://example.com/simple bar==4.5.6 \\ --hash=sha256:67890 foo==1.2.3 \\ --hash=sha256:12345 """ assert expected == content
def test_install_with_trusted_host(config: Config): config.merge({"certificates": {"default": {"cert": False}}}) default = LegacyRepository("default", "https://foo.bar") pool = Pool() pool.add_repository(default, default=True) null_env = NullEnv() installer = PipInstaller(null_env, NullIO(), pool) foo = Package( "foo", "0.0.0", source_type="legacy", source_reference=default.name, source_url=default.url, ) installer.install(foo) assert len(null_env.executed) == 1 cmd = null_env.executed[0] assert "--trusted-host" in cmd cert_index = cmd.index("--trusted-host") assert cmd[cert_index + 1] == "foo.bar"
def test_install_with_client_cert(): client_path = "path/to/client.pem" pool = Pool() default = LegacyRepository("default", "https://foo.bar", client_cert=Path(client_path)) pool.add_repository(default, default=True) null_env = NullEnv() installer = PipInstaller(null_env, NullIO(), pool) foo = Package( "foo", "0.0.0", source_type="legacy", source_reference=default.name, source_url=default.url, ) installer.install(foo) assert len(null_env.executed) == 1 cmd = null_env.executed[0] assert "--client-cert" in cmd cert_index = cmd.index("--client-cert") # Need to do the str(Path()) bit because Windows paths get modified by Path assert cmd[cert_index + 1] == str(Path(client_path))
def pool(): pool = Pool() pool.add_repository(PyPiRepository(disable_cache=True)) pool.add_repository( LegacyRepository("foo", "https://foo.bar/simple/", disable_cache=True)) return pool
def test_repository_with_normal_default_and_secondary_repositories(): secondary = LegacyRepository("secondary", "https://secondary.com") default = LegacyRepository("default", "https://default.com") repo1 = LegacyRepository("foo", "https://foo.bar") repo2 = LegacyRepository("bar", "https://bar.baz") pool = Pool() pool.add_repository(repo1) pool.add_repository(secondary, secondary=True) pool.add_repository(repo2) pool.add_repository(default, default=True) assert pool.repository("secondary") is secondary assert pool.repository("default") is default assert pool.repository("foo") is repo1 assert pool.repository("bar") is repo2 assert pool.has_default()
def test_add_constraint_not_found_with_source(app, poetry, mocker, tester): repo = LegacyRepository(name="my-index", url="https://my-index.fake") mocker.patch.object(repo, "find_packages", return_value=[]) poetry.pool.add_repository(repo) pypi = poetry.pool.repositories[0] pypi.add_package(get_package("cachy", "0.2.0")) with pytest.raises(ValueError) as e: tester.execute("cachy --source my-index") assert "Could not find a matching version of package cachy" == str(e.value)
def test_install_with_non_pypi_default_repository(): pool = Pool() default = LegacyRepository("default", "https://default.com") another = LegacyRepository("another", "https://another.com") pool.add_repository(default, default=True) pool.add_repository(another) installer = PipInstaller(NullEnv(), NullIO(), pool) foo = Package("foo", "0.0.0") foo.source_type = "legacy" foo.source_reference = default._name foo.source_url = default._url bar = Package("bar", "0.1.0") bar.source_type = "legacy" bar.source_reference = another._name bar.source_url = another._url installer.install(foo) installer.install(bar)
def test_add_constraint_with_source_old_installer( app: "PoetryTestApplication", poetry: "Poetry", installer: "NoopInstaller", old_tester: "CommandTester", ): repo = LegacyRepository(name="my-index", url="https://my-index.fake") repo.add_package(get_package("cachy", "0.2.0")) repo._cache.store("matches").put("cachy:0.2.0", [Version.parse("0.2.0")], 5) poetry.pool.add_repository(repo) old_tester.execute("cachy=0.2.0 --source my-index") expected = """\ Updating dependencies Resolving dependencies... Writing lock file Package operations: 1 install, 0 updates, 0 removals - Installing cachy (0.2.0) """ assert expected == old_tester.io.fetch_output() assert len(installer.installs) == 1 content = app.poetry.file.read()["tool"]["poetry"] assert "cachy" in content["dependencies"] assert content["dependencies"]["cachy"] == { "version": "0.2.0", "source": "my-index", }
def test_add_constraint_not_found_with_source_old_installer( app: "PoetryTestApplication", poetry: "Poetry", mocker: "MockerFixture", old_tester: "CommandTester", ): repo = LegacyRepository(name="my-index", url="https://my-index.fake") mocker.patch.object(repo, "find_packages", return_value=[]) poetry.pool.add_repository(repo) pypi = poetry.pool.repositories[0] pypi.add_package(get_package("cachy", "0.2.0")) with pytest.raises(ValueError) as e: old_tester.execute("cachy --source my-index") assert str(e.value) == "Could not find a matching version of package cachy"
def test_pool_fallback_through_repos(): pool = Pool() pool.add_repository( LegacyRepository(url="http://fake.url/simple", name="fake")) pool.add_repository(MockRepository()) package = pool.package("requests", "2.18.4") assert package.name == "requests" assert len(package.requires) == 4 assert len(package.extras["security"]) == 3 assert len(package.extras["socks"]) == 2 win_inet = package.extras["socks"][0] assert win_inet.name == "win-inet-pton" assert win_inet.python_versions == "~2.7 || ~2.6" assert str(win_inet.marker) == ( 'sys_platform == "win32" and (python_version == "2.7" ' 'or python_version == "2.6") and extra == "socks"')
def create_legacy_repository(cls, source: Dict[str, str], auth_config: Config) -> "LegacyRepository": from poetry.repositories.legacy_repository import LegacyRepository from poetry.utils.helpers import get_cert from poetry.utils.helpers import get_client_cert if "url" not in source: raise RuntimeError("Unsupported source specified") # PyPI-like repository if "name" not in source: raise RuntimeError("Missing [name] in source.") name = source["name"] url = source["url"] return LegacyRepository( name, url, config=auth_config, cert=get_cert(auth_config, name), client_cert=get_client_cert(auth_config, name), )
def test_repository_from_secondary_pool(): repo = LegacyRepository("foo", "https://foo.bar") pool = Pool() pool.add_repository(repo, secondary=True) assert pool.repository("foo") is repo
def test_repository_from_normal_pool(): repo = LegacyRepository("foo", "https://foo.bar") pool = Pool() pool.add_repository(repo) assert pool.repository("foo") is repo