-
Notifications
You must be signed in to change notification settings - Fork 128
Fix async_alert to respect allow_style setting for 3.x branch #1582
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
Merged
+60
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,50 @@ | ||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import cmd2 | ||
| import cmd2.cmd2 # to patch vt100_support | ||
| from cmd2 import rich_utils as ru | ||
|
|
||
|
|
||
| class TestAsyncAlert(unittest.TestCase): | ||
| def test_async_alert_strips_ansi_when_allow_style_is_never(self): | ||
| app = cmd2.Cmd() | ||
|
|
||
| # Patch vt100_support to True | ||
| with patch('cmd2.cmd2.vt100_support', True): | ||
| # Patch threading functions | ||
| mock_current_thread = MagicMock() | ||
| mock_current_thread.name = "NotMainThread" | ||
|
|
||
| with ( | ||
| patch('threading.current_thread', return_value=mock_current_thread), | ||
| patch('threading.main_thread', return_value=MagicMock()), | ||
| patch('cmd2.cmd2.rl_get_display_prompt', return_value='(Cmd) '), | ||
| patch('cmd2.cmd2.readline.get_line_buffer', return_value=''), | ||
| patch('cmd2.cmd2.rl_get_point', return_value=0), | ||
| patch('cmd2.cmd2.rl_force_redisplay'), | ||
| patch('sys.stdout', new_callable=MagicMock) as mock_stdout, | ||
| ): | ||
| # Set allow_style to NEVER | ||
| app.allow_style = ru.AllowStyle.NEVER | ||
|
|
||
| # Styled message | ||
| msg = "033円[31mError033円[0m" | ||
|
|
||
| # Call async_alert | ||
| app.async_alert(msg) | ||
|
|
||
| # Capture calls to write | ||
| # mock_stdout.write.call_args_list -> [call(str), call(str)...] | ||
| # We look at all written strings | ||
| written_content = "".join([call.args[0] for call in mock_stdout.write.call_args_list]) | ||
|
|
||
| # Check that ANSI codes for color are NOT present | ||
| if "033円[31m" in written_content: | ||
| raise AssertionError(f"Found ANSI color code in output: {written_content!r}") | ||
| if "Error" not in written_content: | ||
| raise AssertionError(f"Message 'Error' not found in output: {written_content!r}") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
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.