Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for git subdirectories #5172

Merged
merged 6 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/poetry/installation/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,9 @@ def _install_directory(self, operation: Install | Update) -> int:
else:
req = Path(package.source_url).resolve(strict=False)

if package.source_subdirectory:
req /= package.source_subdirectory

pyproject = PyProjectTOML(os.path.join(req, "pyproject.toml"))

if pyproject.is_poetry_project():
Expand Down Expand Up @@ -762,6 +765,8 @@ def _create_git_url_reference(self, package: Package) -> dict[str, Any]:
"commit_id": package.source_resolved_reference,
},
}
if package.source_subdirectory:
reference["subdirectory"] = package.source_subdirectory
ashnair1 marked this conversation as resolved.
Show resolved Hide resolved

return reference

Expand Down
18 changes: 14 additions & 4 deletions src/poetry/installation/pip_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ def requirement(self, package: Package, formatted: bool = False) -> str | list[s
f"#egg={package.name}"
)

if package.source_subdirectory:
req += f"&subdirectory={package.source_subdirectory}"

if package.develop:
return ["-e", req]

Expand Down Expand Up @@ -217,6 +220,9 @@ def install_directory(self, package: Package) -> str | int:
else:
req = Path(package.source_url).resolve(strict=False)

if package.source_subdirectory:
req /= package.source_subdirectory

pyproject = PyProjectTOML(os.path.join(req, "pyproject.toml"))

if pyproject.is_poetry_project():
Expand Down Expand Up @@ -278,9 +284,13 @@ def install_git(self, package: Package) -> None:
)

# Now we just need to install from the source directory
pkg = Package(package.name, package.version)
pkg._source_type = "directory"
pkg._source_url = str(source.path)
pkg.develop = package.develop
pkg = Package(
name=package.name,
version=package.version,
source_type="directory",
source_url=str(source.path),
source_subdirectory=package.source_subdirectory,
develop=package.develop,
)

self.install_directory(pkg)
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
ashnair1 marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
5 changes: 5 additions & 0 deletions src/poetry/repositories/installed_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def create_package_from_distribution(
source_url = None
source_reference = None
source_resolved_reference = None
source_subdirectory = None
if is_standard_package:
if path.name.endswith(".dist-info"):
paths = cls.get_package_paths(
Expand Down Expand Up @@ -169,6 +170,7 @@ def create_package_from_distribution(
source_url=source_url,
source_reference=source_reference,
source_resolved_reference=source_resolved_reference,
source_subdirectory=source_subdirectory,
)

package.description = distribution.metadata.get( # type: ignore[attr-defined]
Expand All @@ -185,6 +187,7 @@ def create_package_from_pep610(cls, distribution: metadata.Distribution) -> Pack
source_url = None
source_reference = None
source_resolved_reference = None
source_subdirectory = None
develop = False

url_reference = json.loads(
Expand Down Expand Up @@ -213,6 +216,7 @@ def create_package_from_pep610(cls, distribution: metadata.Distribution) -> Pack
source_reference = url_reference["vcs_info"].get(
"requested_revision", source_resolved_reference
)
source_subdirectory = url_reference.get("subdirectory")

package = Package(
distribution.metadata["name"],
Expand All @@ -221,6 +225,7 @@ def create_package_from_pep610(cls, distribution: metadata.Distribution) -> Pack
source_url=source_url,
source_reference=source_reference,
source_resolved_reference=source_resolved_reference,
source_subdirectory=source_subdirectory,
develop=develop,
)

Expand Down
47 changes: 47 additions & 0 deletions tests/console/commands/self/test_add_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,53 @@ def test_add_with_git_constraint_with_extras(
)


@pytest.mark.parametrize(
"url, rev",
[
("git+https://github.com/demo/poetry-plugin2.git#subdirectory=subdir", None),
(
"git+https://github.com/demo/poetry-plugin2.git@master#subdirectory=subdir",
"master",
neersighted marked this conversation as resolved.
Show resolved Hide resolved
),
],
)
def test_add_with_git_constraint_with_subdirectory(
url: str,
rev: str | None,
tester: CommandTester,
repo: TestRepository,
):
repo.add_package(Package("pendulum", "2.0.5"))

tester.execute(url)

expected = """
Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 2 installs, 0 updates, 0 removals

• Installing pendulum (2.0.5)
• Installing poetry-plugin (0.1.2 9cf87a2)
"""

constraint = {
"git": "https://github.com/demo/poetry-plugin2.git",
"subdirectory": "subdir",
}

if rev:
constraint["rev"] = rev

assert_plugin_add_result(
tester,
expected,
constraint,
)


def test_add_existing_plugin_warns_about_no_operation(
tester: CommandTester,
repo: TestRepository,
Expand Down
47 changes: 47 additions & 0 deletions tests/console/commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,53 @@ def test_add_git_constraint_with_extras(
}


@pytest.mark.parametrize(
"url, rev",
[
("git+https://github.com/demo/subdirectories.git#subdirectory=two", None),
(
"git+https://github.com/demo/subdirectories.git@master#subdirectory=two",
"master",
),
],
)
def test_add_git_constraint_with_subdirectory(
url: str,
rev: str | None,
app: PoetryTestApplication,
repo: TestRepository,
tester: CommandTester,
env: MockEnv,
):
tester.execute(url)

expected = """\
Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 1 install, 0 updates, 0 removals

• Installing two (2.0.0 9cf87a2)
"""
assert tester.io.fetch_output().strip() == expected.strip()
assert tester.command.installer.executor.installations_count == 1

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

constraint = {
"git": "https://github.com/demo/subdirectories.git",
"subdirectory": "two",
}

if rev:
constraint["rev"] = rev

assert "two" in content["dependencies"]
assert content["dependencies"]["two"] == constraint


@pytest.mark.parametrize("editable", [False, True])
def test_add_git_ssh_constraint(
editable: bool,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[tool.poetry]
name = "poetry-plugin"
version = "0.1.2"
description = "Some description."
authors = [
"Sébastien Eustace <sebastien@eustace.io>"
]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.6"
pendulum = "^2.0"
tomlkit = {version = "^0.7.0", optional = true}

[tool.poetry.extras]
foo = ["tomlkit"]

[tool.poetry.dev-dependencies]
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ license = "MIT"

[tool.poetry.dependencies]
python = "^3.7"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ license = "MIT"

[tool.poetry.dependencies]
python = "^3.7"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ authors = []
license = "MIT"

[tool.poetry.dependencies]
python = "^3.7"
python = "~2.7 || ^3.4"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
34 changes: 34 additions & 0 deletions tests/installation/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,40 @@ def test_executor_should_write_pep610_url_references_for_git(
)


def test_executor_should_write_pep610_url_references_for_git_with_subdirectories(
tmp_venv: VirtualEnv,
pool: Pool,
config: Config,
io: BufferedIO,
mock_file_downloads: None,
):
package = Package(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go ahead and hoist this out into a fixture. I'm not sure if there's a clean place to put it, but also please check if we can share this with test_pip_installer.py without things getting too ugly.

"two",
"2.0.0",
source_type="git",
source_reference="master",
source_resolved_reference="123456",
source_url="https://github.com/demo/subdirectories.git",
source_subdirectory="two",
)

executor = Executor(tmp_venv, pool, config, io)
executor.execute([Install(package)])
verify_installed_distribution(
tmp_venv,
package,
{
"vcs_info": {
"vcs": "git",
"requested_revision": "master",
"commit_id": "123456",
},
"url": package.source_url,
"subdirectory": package.source_subdirectory,
},
)


def test_executor_should_use_cached_link_and_hash(
tmp_venv: VirtualEnv,
pool: Pool,
Expand Down
32 changes: 32 additions & 0 deletions tests/installation/test_pip_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ def package_git() -> Package:
return package


@pytest.fixture
def package_git_with_subdirectory() -> Package:
package = Package(
"subdirectories",
"2.0.0",
source_type="git",
source_url="https://github.com/demo/subdirectories.git",
source_reference="master",
source_subdirectory="two",
)

return package


@pytest.fixture
def pool() -> Pool:
return Pool()
Expand Down Expand Up @@ -85,6 +99,24 @@ def test_requirement_source_type_url():
assert result == expected


def test_requirement_git_subdirectory(
pool: Pool, package_git_with_subdirectory: Package
) -> None:
null_env = NullEnv()
installer = PipInstaller(null_env, NullIO(), pool)
result = installer.requirement(package_git_with_subdirectory)
expected = (
"git+https://github.com/demo/subdirectories.git"
"@master#egg=subdirectories&subdirectory=two"
)

assert result == expected
installer.install(package_git_with_subdirectory)
assert len(null_env.executed) == 1
cmd = null_env.executed[0]
assert Path(cmd[-1]).parts[-3:] == ("demo", "subdirectories", "two")


def test_requirement_git_develop_false(installer: PipInstaller, package_git: Package):
package_git.develop = False
result = installer.requirement(package_git)
Expand Down
Loading