Skip to content

Commit

Permalink
feat(console): Add --no-strict option to check command for warnings…
Browse files Browse the repository at this point in the history
… handling

The `--no-strict` option allows the command to return a successful exit code (0) even when warnings are present.
  • Loading branch information
finswimmer committed Jan 7, 2025
1 parent 229ae23 commit 82a7dd4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/poetry/console/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class CheckCommand(Command):
"Checks that <comment>poetry.lock</> exists for the current"
" version of <comment>pyproject.toml</>.",
),
option(
"no-strict",
None,
"Reports warnings without causing the command to fail,"
" changing the return code from 1 to 0 in case only warnings are present.",
),
]

def _validate_classifiers(
Expand Down Expand Up @@ -161,15 +167,20 @@ def handle(self) -> int:
"Run `poetry lock` to fix the lock file."
]

if not check_result["errors"] and not check_result["warnings"]:
self.info("All set!")
return_code = 0

if check_result["errors"] or (
check_result["warnings"] and not self.option("no-strict")
):
return_code = 1

return 0
if return_code == 0:
self.info("All set!")

for error in check_result["errors"]:
self.line_error(f"<error>Error: {error}</error>")

for error in check_result["warnings"]:
self.line_error(f"<warning>Warning: {error}</warning>")

return 1
return return_code
18 changes: 15 additions & 3 deletions tests/console/commands/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,26 @@ def test_check_valid(tester: CommandTester) -> None:
assert tester.io.fetch_output() == expected


@pytest.mark.parametrize(
["args", "expected_status"],
[
([], 1),
(["--no-strict"], 0),
],
)
def test_check_valid_legacy(
mocker: MockerFixture, tester: CommandTester, fixture_dir: FixtureDirGetter
args: list[str],
expected_status: int,
mocker: MockerFixture,
tester: CommandTester,
fixture_dir: FixtureDirGetter,
) -> None:
mocker.patch(
"poetry.poetry.Poetry.file",
return_value=TOMLFile(fixture_dir("simple_project_legacy") / "pyproject.toml"),
new_callable=mocker.PropertyMock,
)
tester.execute()
tester.execute(" ".join(args))

expected = (
"Warning: [tool.poetry.name] is deprecated. Use [project.name] instead.\n"
Expand Down Expand Up @@ -124,7 +135,8 @@ def test_check_valid_legacy(
"for scripts of type 'file').\n"
)

assert tester.io.fetch_error() == expected
assert expected in tester.io.fetch_error()
assert tester.status_code == expected_status


def test_check_invalid_dep_name_same_as_project_name(
Expand Down

0 comments on commit 82a7dd4

Please sign in to comment.