Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashnair1 committed Oct 31, 2022
1 parent 8eb921e commit 11894fe
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/poetry/utils/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def pip_install(
args.append("-e")

if subdirectory:
path = f"file:{str(path)}#subdirectory={subdirectory}"
path = f"file:{str(path)}#subdirectory={subdirectory}" # type: ignore[assignment] # noqa: E501

args.append(str(path))

Expand Down
40 changes: 40 additions & 0 deletions tests/console/commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,46 @@ def test_add_url_constraint_wheel_with_extras(
}


def test_add_url_constraint_zip_with_subdir(
app: PoetryTestApplication,
repo: TestRepository,
tester: CommandTester,
mocker: MockerFixture,
):
p = mocker.patch("pathlib.Path.cwd")
p.return_value = Path(__file__) / ".."

repo.add_package(get_package("pendulum", "1.4.4"))

tester.execute(
"https://python-poetry.org/distributions/demo-0.1.0.zip#subdirectory=subdir"
)

expected = """\
Updating dependencies
Resolving dependencies...
Writing lock file
Package operations: 2 installs, 0 updates, 0 removals
• Installing pendulum (1.4.4)
• Installing demo\
(0.1.0 https://python-poetry.org/distributions/demo-0.1.0.zip)
"""
assert tester.io.fetch_output() == expected
assert tester.command.installer.executor.installations_count == 2

content = app.poetry.file.read()["tool"]["poetry"]

assert "demo" in content["dependencies"]
assert content["dependencies"]["demo"] == {
"url": "https://python-poetry.org/distributions/demo-0.1.0.zip",
"subdirectory": "subdir",
}


def test_add_constraint_with_python(
app: PoetryTestApplication, repo: TestRepository, tester: CommandTester
):
Expand Down
Binary file added tests/fixtures/distributions/demo-0.1.0.zip
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/installation/test_pip_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from cleo.io.null_io import NullIO
from poetry.core.packages.package import Package
from poetry.core.packages.utils.link import Link

from poetry.installation.pip_installer import PipInstaller
from poetry.repositories.legacy_repository import LegacyRepository
Expand Down Expand Up @@ -52,6 +53,19 @@ def package_git_with_subdirectory() -> Package:
return package


@pytest.fixture
def package_url_zip_with_subdirectory() -> Package:
package = Package(
"subdirectories",
"2.0.0",
source_type="url",
source_url="/~https://github.com/demo/subdirectories.zip",
source_subdirectory="two",
)

return package


@pytest.fixture
def pool() -> RepositoryPool:
return RepositoryPool()
Expand Down Expand Up @@ -117,6 +131,27 @@ def test_requirement_git_subdirectory(
assert Path(cmd[-1]).parts[-3:] == ("demo", "subdirectories", "two")


def test_requirement_url_zip_subdirectory(
pool: RepositoryPool, package_url_zip_with_subdirectory: Package
) -> None:
null_env = NullEnv()
installer = PipInstaller(null_env, NullIO(), pool)
result = installer.requirement(package_url_zip_with_subdirectory)
expected = (
"/~https://github.com/demo/subdirectories.zip#egg=subdirectories&subdirectory=two"
)

assert result == expected
installer.install(package_url_zip_with_subdirectory)
assert len(null_env.executed) == 1
cmd = null_env.executed[0]

link = Link(cmd[-1])
assert link.filename == "subdirectories.zip"
assert link.subdirectory_fragment == "two"
assert link.is_sdist


def test_requirement_git_develop_false(installer: PipInstaller, package_git: Package):
package_git.develop = False
result = installer.requirement(package_git)
Expand Down
95 changes: 95 additions & 0 deletions tests/packages/test_locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def test_lock_file_data_is_ordered(locker: Locker, root: ProjectPackage):
source_type="url",
source_url="https://example.org/url-package-1.0-cp39-win_amd64.whl",
)
package_url_zip = Package(
"url-zip-subdir",
"1.0",
source_type="url",
source_url="https://example.org/archive/1.0.zip",
source_subdirectory="subdir",
)
packages = [
package_a2,
package_a,
Expand All @@ -87,6 +94,7 @@ def test_lock_file_data_is_ordered(locker: Locker, root: ProjectPackage):
package_git_with_subdirectory,
package_url_win32,
package_url_linux,
package_url_zip,
]

locker.set_lock_data(root, packages)
Expand Down Expand Up @@ -191,6 +199,20 @@ def test_lock_file_data_is_ordered(locker: Locker, root: ProjectPackage):
type = "url"
url = "https://example.org/url-package-1.0-cp39-win_amd64.whl"
[[package]]
name = "url-zip-subdir"
version = "1.0"
description = ""
category = "main"
optional = false
python-versions = "*"
files = []
[package.source]
type = "url"
url = "https://example.org/archive/1.0.zip"
subdirectory = "subdir"
[metadata]
lock-version = "2.0"
python-versions = "*"
Expand Down Expand Up @@ -411,6 +433,39 @@ def test_locker_properly_loads_subdir(locker: Locker) -> None:
assert package.source_subdirectory == "subdir"


def test_locker_properly_loads_url_zip_with_subdir(locker: Locker) -> None:
content = """\
[[package]]
name = "url-zip-subdir"
version = "1.2.0"
description = ""
category = "main"
optional = false
python-versions = "*"
files = []
[package.source]
type = "url"
url = "/~https://github.com/python-poetry/archive/1.2.0.zip"
subdirectory = "subdir"
[metadata]
lock-version = "2.0"
python-versions = "*"
content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8"
"""
locker.lock.write(tomlkit.parse(content))

repository = locker.locked_repository()
assert len(repository.packages) == 1

packages = repository.find_packages(get_dependency("url-zip-subdir", "1.2.0"))
assert len(packages) == 1

package = packages[0]
assert package.source_subdirectory == "subdir"


def test_locker_properly_assigns_metadata_files(locker: Locker) -> None:
"""
For multiple constraints dependencies, there is only one common entry in
Expand Down Expand Up @@ -895,6 +950,46 @@ def test_locker_dumps_subdir(locker: Locker, root: ProjectPackage) -> None:
assert content == expected


def test_locker_dumps_url_zip_with_subdir(locker: Locker, root: ProjectPackage) -> None:
package_git_with_subdirectory = Package(
"url-zip-subdir",
"1.2.0",
source_type="url",
source_url="/~https://github.com/python-poetry/archive/1.2.0.zip",
source_subdirectory="subdir",
)

locker.set_lock_data(root, [package_git_with_subdirectory])

with locker.lock.open(encoding="utf-8") as f:
content = f.read()

expected = f"""\
# {GENERATED_COMMENT}
[[package]]
name = "url-zip-subdir"
version = "1.2.0"
description = ""
category = "main"
optional = false
python-versions = "*"
files = []
[package.source]
type = "url"
url = "/~https://github.com/python-poetry/archive/1.2.0.zip"
subdirectory = "subdir"
[metadata]
lock-version = "2.0"
python-versions = "*"
content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8"
""" # noqa: E800

assert content == expected


def test_locker_dumps_dependency_extras_in_correct_order(
locker: Locker, root: ProjectPackage
):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Metadata-Version: 2.1
Name: url-pep-610-subdirectory
Version: 1.2.3
Summary: Foo
License: MIT
Requires-Python: >=3.6
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"url": "https://python-poetry.org/distributions/url-pep-610-subdirectory-1.2.3.zip",
"subdirectory": "subdir",
"archive_info": {}
}
19 changes: 19 additions & 0 deletions tests/repositories/test_installed_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
SITE_PURELIB / "git_pep_610_subdirectory-1.2.3.dist-info"
),
metadata.PathDistribution(SITE_PURELIB / "url_pep_610-1.2.3.dist-info"),
metadata.PathDistribution(
SITE_PURELIB / "url_pep_610_subdirectory-1.2.3.dist-info"
),
metadata.PathDistribution(SITE_PURELIB / "file_pep_610-1.2.3.dist-info"),
metadata.PathDistribution(SITE_PURELIB / "directory_pep_610-1.2.3.dist-info"),
metadata.PathDistribution(
Expand Down Expand Up @@ -268,6 +271,22 @@ def test_load_pep_610_compliant_url_packages(repository: InstalledRepository):
)


def test_load_pep_610_compliant_url_packages_with_subdirectory(
repository: InstalledRepository,
):
package = get_package_from_repository("url-pep-610-subdirectory", repository)

assert package is not None
assert package.name == "url-pep-610-subdirectory"
assert package.version.text == "1.2.3"
assert package.source_type == "url"
assert package.source_subdirectory == "subdir"
assert (
package.source_url
== "https://python-poetry.org/distributions/url-pep-610-subdirectory-1.2.3.zip"
)


def test_load_pep_610_compliant_file_packages(repository: InstalledRepository):
package = get_package_from_repository("file-pep-610", repository)

Expand Down
9 changes: 9 additions & 0 deletions tests/utils/test_dependency_specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
"subdirectory": "two",
},
),
(
"https://python-poetry.org/distributions/demo-0.1.0.zip"
+ "#subdirectory=subdir",
{
"url": "https://python-poetry.org/distributions/demo-0.1.0.zip",
"name": "demo",
"subdirectory": "subdir",
},
),
("demo", {"name": "demo"}),
("demo@1.0.0", {"name": "demo", "version": "1.0.0"}),
("demo@^1.0.0", {"name": "demo", "version": "^1.0.0"}),
Expand Down

0 comments on commit 11894fe

Please sign in to comment.