-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add assertDoesNotAddMessages to CheckerTestCase
#10930
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
Open
ShehabSherif0
wants to merge
11
commits into
pylint-dev:main
Choose a base branch
from
ShehabSherif0:add-assert-does-not-add-messages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
03cd23c
Add assertDoesNotAddMessages to CheckerTestCase
ShehabSherif0 3299943
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8a426ba
Address Copilot review comments
ShehabSherif0 4c98679
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] faedc02
test: cover all _messages_match mismatch branches for 100% patch cove…
ShehabSherif0 0f15cc2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5bbba3f
Address maintainer feedback from Pierre-Sassoulas
ShehabSherif0 8a3aaec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9f088f4
Fix no-else-raise lint warning in assertDoesNotAddMessages
ShehabSherif0 12d10c4
Less LLM comments, skeleton of raise match assertion
Pierre-Sassoulas 7d08354
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Add ``assertDoesNotAddMessages`` to ``CheckerTestCase`` to assert that | ||
| specific messages are not emitted, while allowing other messages to be | ||
| present. This complements ``assertNoMessages`` which asserts that no | ||
| messages at all are emitted. | ||
|
|
||
| Refs #9598 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
| # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE | ||
| # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt | ||
|
|
||
| """Tests for CheckerTestCase assertion methods.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from pylint.checkers.base_checker import BaseChecker | ||
| from pylint.interfaces import UNDEFINED | ||
| from pylint.testutils import CheckerTestCase, MessageTest | ||
|
|
||
|
|
||
| class _DummyChecker(BaseChecker): | ||
| """A minimal checker used only for testing the test infrastructure.""" | ||
|
|
||
| name = "dummy-for-testcase" | ||
| msgs = { | ||
| "W9901": ("Dummy message A", "dummy-msg-a", "Dummy message A."), | ||
| "W9902": ("Dummy message B", "dummy-msg-b", "Dummy message B."), | ||
| } | ||
|
|
||
|
|
||
| _MSG_A = MessageTest("W9901", line=1, node=None, args=None, confidence=UNDEFINED) | ||
|
|
||
|
|
||
| class TestCheckerTestCase(CheckerTestCase): | ||
| CHECKER_CLASS = _DummyChecker | ||
|
|
||
| def test_assert_adds_messages_success(self) -> None: | ||
| """Scenario 1: expected raised / actual raised.""" | ||
| with self.assertAddsMessages(_MSG_A): | ||
| self.linter.add_message("W9901", line=1) | ||
|
|
||
| def test_assert_adds_messages_failure_not_raised(self) -> None: | ||
| """Scenario 2: expected raised / actual not raised.""" | ||
| with pytest.raises(AssertionError, match="..."): | ||
| with self.assertAddsMessages(_MSG_A): | ||
| pass # nothing emitted | ||
|
|
||
| def test_assert_adds_messages_failure_wrong_message(self) -> None: | ||
| """Scenario 3: expected raised / actual not raised but another one raised.""" | ||
| with pytest.raises(AssertionError): | ||
| with self.assertAddsMessages(_MSG_A): | ||
| self.linter.add_message("W9902", line=2) | ||
|
|
||
| def test_assert_does_not_add_messages_failure(self) -> None: | ||
| """Scenario 4: expected not raised / actual raised.""" | ||
| with pytest.raises(AssertionError, match="..."): | ||
| with self.assertDoesNotAddMessages("W9901"): | ||
| self.linter.add_message("W9901", line=1) | ||
|
|
||
| def test_assert_does_not_add_messages_success(self) -> None: | ||
| """Scenario 5: expected not raised / actual not raised.""" | ||
| with self.assertDoesNotAddMessages("W9901"): | ||
| pass # nothing emitted | ||
|
|
||
| def test_assert_does_not_add_messages_success_other_raised(self) -> None: | ||
| """Scenario 6: expected not raised / actual not raised but another one raised.""" | ||
| with self.assertDoesNotAddMessages("W9901"): | ||
| self.linter.add_message("W9902", line=2) | ||
|
|
||
| def test_assert_does_not_add_messages_no_args_raises(self) -> None: | ||
| """Calling with no arguments must raise TypeError.""" | ||
| with pytest.raises(TypeError, match="requires at least one"): | ||
| with self.assertDoesNotAddMessages(): | ||
| pass | ||
|
|
||
| def test_assert_does_not_add_messages_multiple_unwanted(self) -> None: | ||
| """Fails when any of the several unwanted message IDs is found.""" | ||
| with pytest.raises(AssertionError, match="..."): | ||
| with self.assertDoesNotAddMessages("W9901", "W9902"): | ||
| self.linter.add_message("W9902", line=2) | ||
|
|
||
| def test_assert_does_not_add_messages_exception_in_body_drains_messages( | ||
| self, | ||
| ) -> None: | ||
| """An exception in the with-block must not leak messages to later tests.""" | ||
| with pytest.raises(RuntimeError): | ||
| with self.assertDoesNotAddMessages("W9901"): | ||
| self.linter.add_message("W9901", line=1) | ||
| raise RuntimeError("something went wrong") | ||
| # Messages must have been drained; a subsequent assertNoMessages should pass. | ||
| with self.assertNoMessages(): | ||
| pass |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.