Skip to content

Commit

Permalink
check for circular self-dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Apr 17, 2023
1 parent d699b24 commit a5326df
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 15 deletions.
15 changes: 15 additions & 0 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import cast

from cleo.io.null_io import NullIO
from packaging.utils import canonicalize_name
from poetry.core.factory import Factory as BaseFactory
from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.project_package import ProjectPackage
Expand Down Expand Up @@ -319,4 +320,18 @@ def validate(

results["errors"].extend(validate_object(config))

# A project should not depend on itself.
dependencies = set(config.get("dependencies", {}).keys())
dependencies.update(config.get("dev-dependencies", {}).keys())
groups = config.get("group", {}).values()
for group in groups:
dependencies.update(group.get("dependencies", {}).keys())

dependencies = {canonicalize_name(d) for d in dependencies}

if canonicalize_name(config["name"]) in dependencies:
results["errors"].append(
f"Project name ({config['name']}) is same as one of its dependencies"
)

return results
1 change: 1 addition & 0 deletions tests/console/commands/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_check_invalid(

expected = """\
Error: 'description' is a required property
Error: Project name (invalid) is same as one of its dependencies
Error: Unrecognized classifiers: ['Intended Audience :: Clowns'].
Warning: A wildcard Python dependency is ambiguous.\
Consider specifying a more explicit one.
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/invalid_pyproject/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ classifiers = [
[tool.poetry.dependencies]
python = "*"
pendulum = {"version" = "^2.0.5", allows-prereleases = true}
invalid = "1.0"
4 changes: 2 additions & 2 deletions tests/fixtures/sample_project/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
name = "my-package"
name = "sample-project"
version = "1.2.3"
description = "Some description."
authors = [
Expand Down Expand Up @@ -51,7 +51,7 @@ pytest = "~3.4"


[tool.poetry.scripts]
my-script = "my_package:main"
my-script = "sample_project:main"


[tool.poetry.plugins."blogtool.parsers"]
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/with_default_source/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
name = "my-package"
name = "with-default-source"
version = "1.2.3"
description = "Some description."
authors = [
Expand Down Expand Up @@ -48,7 +48,7 @@ pytest = "~3.4"


[tool.poetry.scripts]
my-script = "my_package:main"
my-script = "with_default_source:main"


[tool.poetry.plugins."blogtool.parsers"]
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/with_default_source_legacy/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
name = "my-package"
name = "default-source-legacy"
version = "1.2.3"
description = "Some description."
authors = [
Expand Down Expand Up @@ -48,7 +48,7 @@ pytest = "~3.4"


[tool.poetry.scripts]
my-script = "my_package:main"
my-script = "default_source_legacy:main"


[tool.poetry.plugins."blogtool.parsers"]
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/with_explicit_source/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
name = "my-package"
name = "with-explicit-source"
version = "1.2.3"
description = "Some description."
authors = [
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/with_local_config/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
name = "my-package"
name = "local-config"
version = "1.2.3"
description = "Some description."
authors = [
Expand Down Expand Up @@ -48,7 +48,7 @@ pytest = "~3.4"


[tool.poetry.scripts]
my-script = "my_package:main"
my-script = "local_config:main"


[tool.poetry.plugins."blogtool.parsers"]
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/with_two_default_sources/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
name = "my-package"
name = "two-default-sources"
version = "1.2.3"
description = "Some description."
authors = [
Expand Down Expand Up @@ -48,7 +48,7 @@ pytest = "~3.4"


[tool.poetry.scripts]
my-script = "my_package:main"
my-script = "two_default_sources:main"


[tool.poetry.plugins."blogtool.parsers"]
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/with_two_default_sources_legacy/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
name = "my-package"
name = "two-default-sources-legacy"
version = "1.2.3"
description = "Some description."
authors = [
Expand Down Expand Up @@ -48,7 +48,7 @@ pytest = "~3.4"


[tool.poetry.scripts]
my-script = "my_package:main"
my-script = "two_default_sources_legacy:main"


[tool.poetry.plugins."blogtool.parsers"]
Expand Down
4 changes: 3 additions & 1 deletion tests/publishing/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cleo.io.null_io import NullIO

from poetry.factory import Factory
from packaging.utils import canonicalize_name
from poetry.publishing.publisher import Publisher


Expand Down Expand Up @@ -72,7 +73,8 @@ def test_publish_can_publish_to_given_repository(
("http://foo.bar",),
{"cert": True, "client_cert": None, "dry_run": False, "skip_existing": False},
] == uploader_upload.call_args
assert "Publishing my-package (1.2.3) to foo" in io.fetch_output()
project_name = canonicalize_name(fixture_name)
assert f"Publishing {project_name} (1.2.3) to foo" in io.fetch_output()


def test_publish_raises_error_for_undefined_repository(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_create_poetry(fixture_dir: FixtureDirGetter) -> None:

package = poetry.package

assert package.name == "my-package"
assert package.name == "sample-project"
assert package.version.text == "1.2.3"
assert package.description == "Some description."
assert package.authors == ["Sébastien Eustace <sebastien@eustace.io>"]
Expand Down Expand Up @@ -418,6 +418,7 @@ def test_create_poetry_fails_on_invalid_configuration(
expected = """\
The Poetry configuration is invalid:
- 'description' is a required property
- Project name (invalid) is same as one of its dependencies
"""
assert str(e.value) == expected

Expand Down

0 comments on commit a5326df

Please sign in to comment.