-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fix #8797: validate version constraint in interactive init flow #10751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1215,3 +1215,38 @@ def test_init_does_not_add_readme_key_when_readme_missing( | |
| # Assert | ||
| pyproject = (tmp_path / "pyproject.toml").read_text(encoding="utf-8") | ||
| assert "readme =" not in pyproject | ||
|
|
||
|
|
||
| def test_interactive_rejects_invalid_version_constraint( | ||
| tester: CommandTester, repo: TestRepository | ||
| ) -> None: | ||
| repo.add_package(get_package("pendulum", "2.0.0")) | ||
|
|
||
| inputs = [ | ||
| "my-package", # Package name | ||
| "1.0.0", # Version | ||
| "Desc", # Description | ||
| "n", # Author | ||
| "MIT", # License | ||
| ">=3.8", # Python | ||
| "", # Interactive packages | ||
| "pendulum", # Search package | ||
| "0", # Select first result | ||
| "isort", # INVALID version constraint | ||
| ">=2.0.0", # VALID version constraint | ||
| "", # Stop searching for packages | ||
| "", # Interactive dev packages | ||
| "", | ||
| "\n", # Generate | ||
| ] | ||
|
|
||
| tester.execute(inputs="\n".join(inputs)) | ||
|
|
||
| output = tester.io.fetch_output() | ||
| error = tester.io.fetch_error() | ||
|
|
||
| # Must show validation error | ||
| assert "Invalid version constraint" in error | ||
|
|
||
| # Must use valid constraint | ||
| assert "pendulum (>=2.0.0" in output | ||
|
Comment on lines
+1251
to
+1252
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Prefer asserting against the generated pyproject to prove the constraint is persisted correctly In addition to checking the CLI output, please also read the generated Suggested implementation: # Must show validation error
assert "Invalid version constraint" in error
# Must use valid constraint in CLI output
assert "pendulum (>=2.0.0" in output
# Must persist validated constraint to pyproject.toml
pyproject_content = (tester.path / "pyproject.toml").read_text(encoding="utf-8")
assert 'pendulum = ">=2.0.0"' in pyproject_content
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Also assert that the flow reprompts for the version constraint after an invalid entry
The test currently checks only that an error is shown and that a valid constraint is eventually accepted. To fully cover the regression, please also assert that the version-constraint prompt (or surrounding question text) is shown again after the invalid input—e.g., by checking that it appears twice—so we verify the loop behavior, not just the error output.
Suggested implementation:
To implement your review comment, you should extend the test so that it explicitly asserts that the version-constraint question is shown again after an invalid entry. The key is to assert that the prompt (or its surrounding question text) appears at least twice in the captured output.
Because I cannot see the rest of the test body (how the command is executed and how output is captured), here is the concrete code you should add after the command is run and after any existing assertions about the error message / successful acceptance of the valid constraint:
You will need to adapt
"Version constraint"to match the exact prompt text used by the interactiveinitcommand in this project (for example, it might be something like"Version constraint (leave blank to use latest)"or similar). The important bits are:tester.io.fetch_output()(or the equivalent in your test harness) after running the command..count(<prompt_substring>)to assert that the prompt appears twice:assert output.count(<prompt_substring>) >= 2.If the output API is different (for example,
tester.io.fetch_output()is not available), replace it with the appropriate mechanism already used elsewhere intests/console/commands/test_init.pyto inspect interactive output.