From bf20d1e2e54604925bee207f992fc5d0a497f639 Mon Sep 17 00:00:00 2001 From: Bartosz Sokorski Date: Thu, 6 Jun 2024 13:39:13 +0200 Subject: [PATCH 1/2] fix(check): don't raise error on pypi reference --- src/poetry/console/commands/check.py | 2 +- tests/console/commands/test_check.py | 69 ++++++++++++++++++- .../fixtures/invalid_pyproject/pyproject.toml | 3 +- .../invalid_pyproject_dep_name/pyproject.toml | 28 ++++++++ tests/fixtures/pypi_reference/pyproject.toml | 16 +++++ tests/test_factory.py | 2 +- 6 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/invalid_pyproject_dep_name/pyproject.toml create mode 100644 tests/fixtures/pypi_reference/pyproject.toml diff --git a/src/poetry/console/commands/check.py b/src/poetry/console/commands/check.py index bd672409a81..d6b6517d616 100644 --- a/src/poetry/console/commands/check.py +++ b/src/poetry/console/commands/check.py @@ -94,7 +94,7 @@ def _validate_readme(self, readme: str | list[str], poetry_file: Path) -> list[s def _validate_dependencies_source(self, config: dict[str, Any]) -> list[str]: """Check dependencies's source are valid""" - sources = {k["name"] for k in config.get("source", [])} + sources = {repository.name for repository in self.poetry.pool.all_repositories} dependency_declarations: list[ dict[str, str | dict[str, str] | list[dict[str, str]]] diff --git a/tests/console/commands/test_check.py b/tests/console/commands/test_check.py index 5d0b0226d8d..363ba6f1f06 100644 --- a/tests/console/commands/test_check.py +++ b/tests/console/commands/test_check.py @@ -43,6 +43,22 @@ def poetry_with_up_to_date_lockfile( yield Factory().create_poetry(cwd) +@pytest.fixture +def poetry_with_pypi_reference( + set_project_context: SetProjectContext, +) -> Iterator[Poetry]: + with set_project_context("pypi_reference", in_place=False) as cwd: + yield Factory().create_poetry(cwd) + + +@pytest.fixture +def poetry_with_invalid_pyproject( + set_project_context: SetProjectContext, +) -> Iterator[Poetry]: + with set_project_context("invalid_pyproject", in_place=False) as cwd: + yield Factory().create_poetry(cwd) + + @pytest.fixture() def tester( command_tester_factory: CommandTesterFactory, poetry_simple_project: Poetry @@ -111,19 +127,55 @@ def test_check_valid_legacy( assert tester.io.fetch_error() == expected -def test_check_invalid( +def test_check_invalid_dep_name_same_as_project_name( mocker: MockerFixture, tester: CommandTester, fixture_dir: FixtureDirGetter ) -> None: mocker.patch( "poetry.poetry.Poetry.file", - return_value=TOMLFile(fixture_dir("invalid_pyproject") / "pyproject.toml"), + return_value=TOMLFile( + fixture_dir("invalid_pyproject_dep_name") / "pyproject.toml" + ), new_callable=mocker.PropertyMock, ) + tester.execute("--lock") + fastjsonschema_error = "data must contain ['description'] properties" + custom_error = "The fields ['description'] are required in package mode." + expected_template = """\ +Error: Project name (invalid) is same as one of its dependencies +Error: Unrecognized classifiers: ['Intended Audience :: Clowns']. +Error: Declared README file does not exist: never/exists.md +Error: Invalid source "exists" referenced in dependencies. +Error: Invalid source "not-exists" referenced in dependencies. +Error: Invalid source "not-exists2" referenced in dependencies. +Error: poetry.lock was not found. +Warning: A wildcard Python dependency is ambiguous.\ + Consider specifying a more explicit one. +Warning: The "pendulum" dependency specifies the "allows-prereleases" property,\ + which is deprecated. Use "allow-prereleases" instead. +Warning: Deprecated classifier 'Natural Language :: Ukranian'.\ + Must be replaced by ['Natural Language :: Ukrainian']. +Warning: Deprecated classifier\ + 'Topic :: Communications :: Chat :: AOL Instant Messenger'.\ + Must be removed. +""" + expected = { + expected_template.format(schema_error=schema_error) + for schema_error in (fastjsonschema_error, custom_error) + } + + assert tester.io.fetch_error() in expected + +def test_check_invalid( + tester: CommandTester, + fixture_dir: FixtureDirGetter, + command_tester_factory: CommandTesterFactory, + poetry_with_invalid_pyproject: Poetry, +) -> None: + tester = command_tester_factory("check", poetry=poetry_with_invalid_pyproject) tester.execute("--lock") expected = """\ -Error: Project name (invalid) is same as one of its dependencies Error: Unrecognized classifiers: ['Intended Audience :: Clowns']. Error: Declared README file does not exist: never/exists.md Error: Invalid source "not-exists" referenced in dependencies. @@ -254,3 +306,14 @@ def test_check_lock_up_to_date( # exit with an error assert status_code == 0 + + +def test_check_does_not_error_on_pypi_reference( + command_tester_factory: CommandTesterFactory, + poetry_with_pypi_reference: Poetry, +) -> None: + tester = command_tester_factory("check", poetry=poetry_with_pypi_reference) + status_code = tester.execute("") + + assert tester.io.fetch_output() == "All set!\n" + assert status_code == 0 diff --git a/tests/fixtures/invalid_pyproject/pyproject.toml b/tests/fixtures/invalid_pyproject/pyproject.toml index b830211ac6e..036c5a4aba0 100644 --- a/tests/fixtures/invalid_pyproject/pyproject.toml +++ b/tests/fixtures/invalid_pyproject/pyproject.toml @@ -16,7 +16,7 @@ readme = "never/exists.md" [tool.poetry.dependencies] python = "*" pendulum = {"version" = "^2.0.5", allows-prereleases = true} -invalid = "1.0" +invalid_dep = "1.0" invalid_source = { "version" = "*", source = "not-exists" } invalid_source_multi = [ { "version" = "*", platform = "linux", source = "exists" }, @@ -26,3 +26,4 @@ invalid_source_multi = [ [[tool.poetry.source]] name = "exists" priority = "explicit" +url = "https://example.com" diff --git a/tests/fixtures/invalid_pyproject_dep_name/pyproject.toml b/tests/fixtures/invalid_pyproject_dep_name/pyproject.toml new file mode 100644 index 00000000000..b830211ac6e --- /dev/null +++ b/tests/fixtures/invalid_pyproject_dep_name/pyproject.toml @@ -0,0 +1,28 @@ +[project] +name = "invalid" +version = "1.0.0" +license = { text = "INVALID" } +classifiers = [ + "Environment :: Console", + "Intended Audience :: Clowns", + "Natural Language :: Ukranian", + "Topic :: Communications :: Chat :: AOL Instant Messenger", +] +dynamic = [ "readme", "dependencies", "requires-python" ] + +[tool.poetry] +readme = "never/exists.md" + +[tool.poetry.dependencies] +python = "*" +pendulum = {"version" = "^2.0.5", allows-prereleases = true} +invalid = "1.0" +invalid_source = { "version" = "*", source = "not-exists" } +invalid_source_multi = [ + { "version" = "*", platform = "linux", source = "exists" }, + { "version" = "*", platform = "win32", source = "not-exists2" }, +] + +[[tool.poetry.source]] +name = "exists" +priority = "explicit" diff --git a/tests/fixtures/pypi_reference/pyproject.toml b/tests/fixtures/pypi_reference/pyproject.toml new file mode 100644 index 00000000000..09e4872eb56 --- /dev/null +++ b/tests/fixtures/pypi_reference/pyproject.toml @@ -0,0 +1,16 @@ +[project] +name = "foobar" +version = "0.1.0" +description = "" +authors = [ + { name = "Poetry Developer", email = "" } +] +dynamic = ["dependencies", "requires-python"] + +[tool.poetry.dependencies] +python = "^3.8" +docker = { version = ">=4.3.1", source = "PyPI" } + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" diff --git a/tests/test_factory.py b/tests/test_factory.py index ddd4ae657a4..215f5171b20 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -405,7 +405,7 @@ def test_create_poetry_fails_on_invalid_configuration( fixture_dir: FixtureDirGetter, ) -> None: with pytest.raises(RuntimeError) as e: - Factory().create_poetry(fixture_dir("invalid_pyproject")) + Factory().create_poetry(fixture_dir("invalid_pyproject_dep_name")) expected = """\ The Poetry configuration is invalid: From 1bac1a451572aa89354495652fc3b25d0ae45b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 1 Dec 2024 11:00:25 +0100 Subject: [PATCH 2/2] remove redundant issues, which are already included in invalid_project, from invalid_project_dep_name --- tests/console/commands/test_check.py | 28 +++---------------- .../invalid_pyproject_dep_name/pyproject.toml | 23 +-------------- 2 files changed, 5 insertions(+), 46 deletions(-) diff --git a/tests/console/commands/test_check.py b/tests/console/commands/test_check.py index 363ba6f1f06..ed71d7f60e6 100644 --- a/tests/console/commands/test_check.py +++ b/tests/console/commands/test_check.py @@ -137,33 +137,13 @@ def test_check_invalid_dep_name_same_as_project_name( ), new_callable=mocker.PropertyMock, ) - tester.execute("--lock") - fastjsonschema_error = "data must contain ['description'] properties" - custom_error = "The fields ['description'] are required in package mode." - expected_template = """\ + tester.execute("") + + expected = """\ Error: Project name (invalid) is same as one of its dependencies -Error: Unrecognized classifiers: ['Intended Audience :: Clowns']. -Error: Declared README file does not exist: never/exists.md -Error: Invalid source "exists" referenced in dependencies. -Error: Invalid source "not-exists" referenced in dependencies. -Error: Invalid source "not-exists2" referenced in dependencies. -Error: poetry.lock was not found. -Warning: A wildcard Python dependency is ambiguous.\ - Consider specifying a more explicit one. -Warning: The "pendulum" dependency specifies the "allows-prereleases" property,\ - which is deprecated. Use "allow-prereleases" instead. -Warning: Deprecated classifier 'Natural Language :: Ukranian'.\ - Must be replaced by ['Natural Language :: Ukrainian']. -Warning: Deprecated classifier\ - 'Topic :: Communications :: Chat :: AOL Instant Messenger'.\ - Must be removed. """ - expected = { - expected_template.format(schema_error=schema_error) - for schema_error in (fastjsonschema_error, custom_error) - } - assert tester.io.fetch_error() in expected + assert tester.io.fetch_error() == expected def test_check_invalid( diff --git a/tests/fixtures/invalid_pyproject_dep_name/pyproject.toml b/tests/fixtures/invalid_pyproject_dep_name/pyproject.toml index b830211ac6e..2f75face1d9 100644 --- a/tests/fixtures/invalid_pyproject_dep_name/pyproject.toml +++ b/tests/fixtures/invalid_pyproject_dep_name/pyproject.toml @@ -1,28 +1,7 @@ [project] name = "invalid" version = "1.0.0" -license = { text = "INVALID" } -classifiers = [ - "Environment :: Console", - "Intended Audience :: Clowns", - "Natural Language :: Ukranian", - "Topic :: Communications :: Chat :: AOL Instant Messenger", -] -dynamic = [ "readme", "dependencies", "requires-python" ] - -[tool.poetry] -readme = "never/exists.md" +dynamic = ["dependencies"] [tool.poetry.dependencies] -python = "*" -pendulum = {"version" = "^2.0.5", allows-prereleases = true} invalid = "1.0" -invalid_source = { "version" = "*", source = "not-exists" } -invalid_source_multi = [ - { "version" = "*", platform = "linux", source = "exists" }, - { "version" = "*", platform = "win32", source = "not-exists2" }, -] - -[[tool.poetry.source]] -name = "exists" -priority = "explicit"