Skip to content

Commit

Permalink
sort package name in extras to make it reproducible (python-poetry#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
ali5h authored and DavidVujic committed Mar 26, 2022
1 parent 6ebc6a1 commit daa4078
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def base_pep_508_name(self) -> str:
requirement = self.pretty_name

if self.extras:
extras = ",".join(self.extras)
extras = ",".join(sorted(self.extras))
requirement += f"[{extras}]"

if isinstance(self.constraint, VersionUnion):
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/packages/directory_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def base_pep_508_name(self) -> str:
requirement = self.pretty_name

if self.extras:
extras = ",".join(self.extras)
extras = ",".join(sorted(self.extras))
requirement += f"[{extras}]"

path = path_to_url(self.path) if self.path.is_absolute() else self.path
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/packages/file_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def base_pep_508_name(self) -> str:
requirement = self.pretty_name

if self.extras:
extras = ",".join(self.extras)
extras = ",".join(sorted(self.extras))
requirement += f"[{extras}]"

path = path_to_url(self.path) if self.path.is_absolute() else self.path
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/packages/url_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def base_pep_508_name(self) -> str:
requirement = self.pretty_name

if self.extras:
extras = ",".join(self.extras)
extras = ",".join(sorted(self.extras))
requirement += f"[{extras}]"

requirement += f" @ {self._url}"
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/packages/vcs_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def base_pep_508_name(self) -> str:
parsed_url = git.ParsedUrl.parse(self._source)

if self.extras:
extras = ",".join(self.extras)
extras = ",".join(sorted(self.extras))
requirement += f"[{extras}]"

if parsed_url.protocol is not None:
Expand Down
6 changes: 3 additions & 3 deletions tests/packages/test_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ def test_to_pep_508_in_extras():

def test_to_pep_508_in_extras_parsed():
dependency = Dependency.create_from_pep_508(
'foo[bar] (>=1.23,<2.0) ; extra == "baz"'
'foo[baz,bar] (>=1.23,<2.0) ; extra == "baz"'
)

result = dependency.to_pep_508()
assert result == 'foo[bar] (>=1.23,<2.0); extra == "baz"'
assert result == 'foo[bar,baz] (>=1.23,<2.0); extra == "baz"'

result = dependency.to_pep_508(with_extras=False)
assert result == "foo[bar] (>=1.23,<2.0)"
assert result == "foo[bar,baz] (>=1.23,<2.0)"


def test_to_pep_508_with_single_version_excluded():
Expand Down
11 changes: 11 additions & 0 deletions tests/packages/test_directory_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,14 @@ def test_directory_dependency_pep_508_local_relative():

requirement = f"demo @ {path}"
_test_directory_dependency_pep_508("demo", path, requirement)


def test_directory_dependency_pep_508_extras():
path = (
Path(__file__).parent.parent
/ "fixtures"
/ "project_with_multi_constraints_dependency"
)
requirement = f"demo[foo,bar] @ file://{path.as_posix()}"
requirement_expected = f"demo[bar,foo] @ file://{path.as_posix()}"
_test_directory_dependency_pep_508("demo", path, requirement, requirement_expected)
14 changes: 14 additions & 0 deletions tests/packages/test_file_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,17 @@ def test_relative_file_dependency_to_pep_508_with_marker(mocker: "MockerFixture"
requirement,
marker=SingleMarker("sys.platform", "linux"),
)


def test_file_dependency_pep_508_extras(mocker: "MockerFixture"):
wheel = "demo-0.1.0-py2.py3-none-any.whl"

rel_path = Path("..") / "fixtures" / "distributions" / wheel
requirement = f'demo[foo,bar] @ {rel_path.as_posix()} ; sys_platform == "linux"'
_test_file_dependency_pep_508(
mocker,
"demo",
rel_path,
requirement,
f'demo[bar,foo] @ {rel_path.as_posix()} ; sys_platform == "linux"',
)
14 changes: 14 additions & 0 deletions tests/packages/test_url_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ def test_to_pep_508():
assert dependency.to_pep_508() == expected


def test_to_pep_508_with_extras():
dependency = URLDependency(
"pytorch",
"https://download.pytorch.org/whl/cpu/torch-1.5.1%2Bcpu-cp38-cp38-linux_x86_64.whl",
extras=["foo", "bar"],
)

expected = (
"pytorch[bar,foo] @"
" https://download.pytorch.org/whl/cpu/torch-1.5.1%2Bcpu-cp38-cp38-linux_x86_64.whl"
)
assert expected == dependency.to_pep_508()


def test_to_pep_508_with_marker():
dependency = URLDependency(
"pytorch",
Expand Down
7 changes: 5 additions & 2 deletions tests/packages/test_vcs_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ def test_to_pep_508_ssh():

def test_to_pep_508_with_extras():
dependency = VCSDependency(
"poetry", "git", "https://github.com/python-poetry/poetry.git", extras=["foo"]
"poetry",
"git",
"https://github.com/python-poetry/poetry.git",
extras=["foo", "bar"],
)

expected = "poetry[foo] @ git+https://github.com/python-poetry/poetry.git"
expected = "poetry[bar,foo] @ git+https://github.com/python-poetry/poetry.git"

assert dependency.to_pep_508() == expected

Expand Down

0 comments on commit daa4078

Please sign in to comment.