Skip to content

Commit

Permalink
dependency: base_pep_508_name always posix + fix tests (#463)
Browse files Browse the repository at this point in the history
The tests for `FileDependency` and `DirectoryDependency` use the
following assert:

```
assert dep.to_pep_508() == pep_508_output or pep_508_input
```

which is equal to
```
assert (dep.to_pep_508() == pep_508_output) or pep_508_input
```

and thus, always true if there is an `pep_508_input`. Actually it should
be

```
assert dep.to_pep_508() == (pep_508_output or pep_508_input)
```

While fixing the tests, I noticed that the output for relative paths was
platform-dependent. I changed it to always return posix paths.
  • Loading branch information
radoering committed Sep 10, 2022
1 parent 0a1d5f1 commit 37cee90
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/poetry/core/packages/directory_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def base_pep_508_name(self) -> str:
extras = ",".join(sorted(self.extras))
requirement += f"[{extras}]"

path = path_to_url(self.path) if self.path.is_absolute() else self.path
path = (
path_to_url(self.path) if self.path.is_absolute() else self.path.as_posix()
)
requirement += f" @ {path}"

return requirement
4 changes: 3 additions & 1 deletion src/poetry/core/packages/file_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def base_pep_508_name(self) -> str:
extras = ",".join(sorted(self.extras))
requirement += f"[{extras}]"

path = path_to_url(self.path) if self.path.is_absolute() else self.path
path = (
path_to_url(self.path) if self.path.is_absolute() else self.path.as_posix()
)
requirement += f" @ {path}"

return requirement
19 changes: 11 additions & 8 deletions tests/packages/test_directory_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _test_directory_dependency_pep_508(
dep = cast(DirectoryDependency, dep)
assert dep.name == name
assert dep.path == path
assert dep.to_pep_508() == pep_508_output or pep_508_input
assert dep.to_pep_508() == (pep_508_output or pep_508_input)


def test_directory_dependency_pep_508_local_absolute() -> None:
Expand All @@ -38,11 +38,13 @@ def test_directory_dependency_pep_508_local_absolute() -> None:
/ "fixtures"
/ "project_with_multi_constraints_dependency"
)
expected = f"demo @ {path.as_uri()}"

requirement = f"demo @ file://{path.as_posix()}"
_test_directory_dependency_pep_508("demo", path, requirement)
_test_directory_dependency_pep_508("demo", path, requirement, expected)

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


def test_directory_dependency_pep_508_localhost() -> None:
Expand All @@ -52,8 +54,8 @@ def test_directory_dependency_pep_508_localhost() -> None:
/ "project_with_multi_constraints_dependency"
)
requirement = f"demo @ file://localhost{path.as_posix()}"
requirement_expected = f"demo @ file://{path.as_posix()}"
_test_directory_dependency_pep_508("demo", path, requirement, requirement_expected)
expected = f"demo @ {path.as_uri()}"
_test_directory_dependency_pep_508("demo", path, requirement, expected)


def test_directory_dependency_pep_508_local_relative() -> None:
Expand All @@ -64,7 +66,8 @@ def test_directory_dependency_pep_508_local_relative() -> None:
_test_directory_dependency_pep_508("demo", path, requirement)

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


def test_directory_dependency_pep_508_extras() -> None:
Expand All @@ -74,8 +77,8 @@ def test_directory_dependency_pep_508_extras() -> None:
/ "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)
expected = f"demo[bar,foo] @ {path.as_uri()}"
_test_directory_dependency_pep_508("demo", path, requirement, expected)


@pytest.mark.parametrize(
Expand Down
19 changes: 10 additions & 9 deletions tests/packages/test_file_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,25 @@ def _test_file_dependency_pep_508(
dep = cast(FileDependency, dep)
assert dep.name == name
assert dep.path == path
assert dep.to_pep_508() == pep_508_output or pep_508_input
assert dep.to_pep_508() == (pep_508_output or pep_508_input)


def test_file_dependency_pep_508_local_file_absolute(mocker: MockerFixture) -> None:
path = DIST_PATH / "demo-0.2.0.tar.gz"
expected = f"demo @ {path.as_uri()}"

requirement = f"demo @ file://{path.as_posix()}"
_test_file_dependency_pep_508(mocker, "demo", path, requirement)
_test_file_dependency_pep_508(mocker, "demo", path, requirement, expected)

requirement = f"demo @ {path}"
_test_file_dependency_pep_508(mocker, "demo", path, requirement)
_test_file_dependency_pep_508(mocker, "demo", path, requirement, expected)


def test_file_dependency_pep_508_local_file_localhost(mocker: MockerFixture) -> None:
path = DIST_PATH / "demo-0.2.0.tar.gz"
requirement = f"demo @ file://localhost{path.as_posix()}"
requirement_expected = f"demo @ file://{path.as_posix()}"
_test_file_dependency_pep_508(
mocker, "demo", path, requirement, requirement_expected
)
expected = f"demo @ {path.as_uri()}"
_test_file_dependency_pep_508(mocker, "demo", path, requirement, expected)


def test_file_dependency_pep_508_local_file_relative_path(
Expand All @@ -145,14 +145,15 @@ def test_file_dependency_pep_508_local_file_relative_path(
_test_file_dependency_pep_508(mocker, "demo", path, requirement)

requirement = f"demo @ {path}"
_test_file_dependency_pep_508(mocker, "demo", path, requirement)
expected = f"demo @ {path.as_posix()}"
_test_file_dependency_pep_508(mocker, "demo", path, requirement, expected)


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

abs_path = DIST_PATH / wheel
requirement = f'demo @ file://{abs_path.as_posix()} ; sys_platform == "linux"'
requirement = f'demo @ {abs_path.as_uri()} ; sys_platform == "linux"'
_test_file_dependency_pep_508(
mocker,
"demo",
Expand Down

0 comments on commit 37cee90

Please sign in to comment.