From 4c447eea25fd03904fb12ad14060cba1de6ad0ff Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 1 Jul 2023 12:54:49 +0100 Subject: [PATCH] tolerate missing project name in validation Fixes #8122 --- src/poetry/factory.py | 5 ++-- .../nameless_pyproject/pyproject.toml | 8 ++++++ tests/test_factory.py | 27 ++++++++++++------- 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 tests/fixtures/nameless_pyproject/pyproject.toml diff --git a/src/poetry/factory.py b/src/poetry/factory.py index 15bd004e747..f4013cc2baf 100644 --- a/src/poetry/factory.py +++ b/src/poetry/factory.py @@ -370,9 +370,10 @@ def validate( dependencies = {canonicalize_name(d) for d in dependencies} - if canonicalize_name(config["name"]) in dependencies: + project_name = config.get("name") + if project_name is not None and canonicalize_name(project_name) in dependencies: results["errors"].append( - f"Project name ({config['name']}) is same as one of its dependencies" + f"Project name ({project_name}) is same as one of its dependencies" ) return results diff --git a/tests/fixtures/nameless_pyproject/pyproject.toml b/tests/fixtures/nameless_pyproject/pyproject.toml new file mode 100644 index 00000000000..213d2646749 --- /dev/null +++ b/tests/fixtures/nameless_pyproject/pyproject.toml @@ -0,0 +1,8 @@ +[tool.poetry] +version = "0.1.0" +description = "" +authors = ["Foo "] +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.10" diff --git a/tests/test_factory.py b/tests/test_factory.py index b1895d25fea..25ea9a89f16 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -524,20 +524,27 @@ def test_create_poetry_fails_on_invalid_configuration( with pytest.raises(RuntimeError) as e: Factory().create_poetry(fixture_dir("invalid_pyproject") / "pyproject.toml") - jsonschema_error = "'description' is a required property" - fastjsonschema_error = "data must contain ['description'] properties" - - expected_template = """\ + expected = """\ The Poetry configuration is invalid: - - {schema_error} + - data must contain ['description'] properties - Project name (invalid) is same as one of its dependencies """ - expected = { - expected_template.format(schema_error=schema_error) - for schema_error in (jsonschema_error, fastjsonschema_error) - } - assert str(e.value) in expected + assert str(e.value) == expected + + +def test_create_poetry_fails_on_nameless_project( + fixture_dir: FixtureDirGetter, +) -> None: + with pytest.raises(RuntimeError) as e: + Factory().create_poetry(fixture_dir("nameless_pyproject") / "pyproject.toml") + + expected = """\ +The Poetry configuration is invalid: + - data must contain ['name'] properties +""" + + assert str(e.value) == expected def test_create_poetry_with_local_config(fixture_dir: FixtureDirGetter) -> None: