Skip to content

Commit

Permalink
Add subdirectory reference to locker.py
Browse files Browse the repository at this point in the history
Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com>
  • Loading branch information
ashnair1 and radoering committed Aug 10, 2022
1 parent 8ef2a64 commit ed88e34
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def locked_repository(self) -> Repository:
source_url=url,
source_reference=source.get("reference"),
source_resolved_reference=source.get("resolved_reference"),
source_subdirectory=source.get("subdirectory"),
)
package.description = info.get("description", "")
package.category = info.get("category", "main")
Expand Down Expand Up @@ -620,6 +621,9 @@ def _dump_package(self, package: Package) -> dict[str, Any]:
if package.source_resolved_reference:
data["source"]["resolved_reference"] = package.source_resolved_reference

if package.source_subdirectory:
data["source"]["subdirectory"] = package.source_subdirectory

if package.source_type in ["directory", "git"]:
data["develop"] = package.develop

Expand Down
110 changes: 110 additions & 0 deletions tests/packages/test_locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ def test_lock_file_data_is_ordered(locker: Locker, root: ProjectPackage):
source_reference="develop",
source_resolved_reference="123456",
)
package_git_with_subdirectory = Package(
"git-package-subdir",
"1.2.3",
source_type="git",
source_url="https://github.com/python-poetry/poetry.git",
source_reference="develop",
source_resolved_reference="123456",
source_subdirectory="subdir",
)
package_url_linux = Package(
"url-package",
"1.0",
Expand All @@ -74,6 +83,7 @@ def test_lock_file_data_is_ordered(locker: Locker, root: ProjectPackage):
package_a,
get_package("B", "1.2"),
package_git,
package_git_with_subdirectory,
package_url_win32,
package_url_linux,
]
Expand Down Expand Up @@ -126,6 +136,22 @@ def test_lock_file_data_is_ordered(locker: Locker, root: ProjectPackage):
reference = "develop"
resolved_reference = "123456"
[[package]]
name = "git-package-subdir"
version = "1.2.3"
description = ""
category = "main"
optional = false
python-versions = "*"
develop = false
[package.source]
type = "git"
url = "https://github.com/python-poetry/poetry.git"
reference = "develop"
resolved_reference = "123456"
subdirectory = "subdir"
[[package]]
name = "url-package"
version = "1.0"
Expand Down Expand Up @@ -163,6 +189,7 @@ def test_lock_file_data_is_ordered(locker: Locker, root: ProjectPackage):
]
B = []
git-package = []
git-package-subdir = []
url-package = []
"""

Expand Down Expand Up @@ -344,6 +371,44 @@ def test_locker_properly_loads_extras_legacy(locker: Locker):
assert dependency_b.name == "b"


def test_locker_properly_loads_subdir(locker: Locker) -> None:
content = """\
[[package]]
name = "git-package-subdir"
version = "1.2.3"
description = ""
category = "main"
optional = false
python-versions = "*"
develop = false
[package.source]
type = "git"
url = "https://github.com/python-poetry/poetry.git"
reference = "develop"
resolved_reference = "123456"
subdirectory = "subdir"
[metadata]
lock-version = "1.1"
python-versions = "*"
content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8"
[metadata.files]
git-package-subdir = []
"""
locker.lock.write(tomlkit.parse(content))

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

packages = repository.find_packages(get_dependency("git-package-subdir", "1.2.3"))
assert len(packages) == 1

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


def test_lock_packages_with_null_description(locker: Locker, root: ProjectPackage):
package_a = get_package("A", "1.0.0")
package_a.description = None
Expand Down Expand Up @@ -668,6 +733,51 @@ def test_locker_dumps_dependency_information_correctly(
assert content == expected


def test_locker_dumps_subdir(locker: Locker, root: ProjectPackage) -> None:
package_git_with_subdirectory = Package(
"git-package-subdir",
"1.2.3",
source_type="git",
source_url="https://github.com/python-poetry/poetry.git",
source_reference="develop",
source_resolved_reference="123456",
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 = """\
[[package]]
name = "git-package-subdir"
version = "1.2.3"
description = ""
category = "main"
optional = false
python-versions = "*"
develop = false
[package.source]
type = "git"
url = "https://github.com/python-poetry/poetry.git"
reference = "develop"
resolved_reference = "123456"
subdirectory = "subdir"
[metadata]
lock-version = "1.1"
python-versions = "*"
content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8"
[metadata.files]
git-package-subdir = []
"""

assert content == expected


def test_locked_repository_uses_root_dir_of_package(
locker: Locker, mocker: MockerFixture
):
Expand Down

0 comments on commit ed88e34

Please sign in to comment.