Skip to content

Commit

Permalink
fix(check): don't raise error on pypi reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus committed Oct 27, 2024
1 parent 856e5f4 commit 25b430c
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/poetry/console/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]
Expand Down
80 changes: 77 additions & 3 deletions tests/console/commands/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -254,3 +306,25 @@ 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("")

# Warnings about deprecated syntax are expected
assert tester.io.fetch_error() == (
"Warning: [tool.poetry.name] is deprecated. Use [project.name] instead.\n"
"Warning: [tool.poetry.version] is set but 'version' is not in [project.dynamic]."
" If it is static use [project.version]. If it is dynamic, add 'version' to"
" [project.dynamic].\nIf you want to set the version dynamically via `poetry build"
" --local-version` or you are using a plugin, which sets the version dynamically,"
" you should define the version in [tool.poetry] and add 'version' to"
" [project.dynamic].\nWarning: [tool.poetry.description] is deprecated."
" Use [project.description] instead.\nWarning: [tool.poetry.authors] is"
" deprecated. Use [project.authors] instead.\n"
)
assert status_code == 1
3 changes: 2 additions & 1 deletion tests/fixtures/invalid_pyproject/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -26,3 +26,4 @@ invalid_source_multi = [
[[tool.poetry.source]]
name = "exists"
priority = "explicit"
url = "https://example.com"
28 changes: 28 additions & 0 deletions tests/fixtures/invalid_pyproject_dep_name/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Loading

0 comments on commit 25b430c

Please sign in to comment.