Repository navigation
π§ͺ [testing improvement] Add unit tests for get_category_color in utils/io.py #24
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import unittest | ||
| import commando.utils.io as io | ||
|
|
||
|
|
||
| class TestGetCategoryColor(unittest.TestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| # Save original colors to restore them later | ||
| cls.original_colors = { | ||
| "CYAN": io.CYAN, | ||
| "MAGENTA": io.MAGENTA, | ||
| "RED": io.RED, | ||
| "GREEN": io.GREEN, | ||
| "YELLOW": io.YELLOW, | ||
| "BOLD": io.BOLD, | ||
| } | ||
| # Set distinct mock values to prevent false positives in non-TTY/CI environments | ||
| io.CYAN = "mock_cyan" | ||
| io.MAGENTA = "mock_magenta" | ||
| io.RED = "mock_red" | ||
| io.GREEN = "mock_green" | ||
| io.YELLOW = "mock_yellow" | ||
| io.BOLD = "mock_bold" | ||
|
|
||
| @classmethod | ||
| def tearDownClass(cls): | ||
| # Restore original colors | ||
| io.CYAN = cls.original_colors["CYAN"] | ||
| io.MAGENTA = cls.original_colors["MAGENTA"] | ||
| io.RED = cls.original_colors["RED"] | ||
| io.GREEN = cls.original_colors["GREEN"] | ||
| io.YELLOW = cls.original_colors["YELLOW"] | ||
| io.BOLD = cls.original_colors["BOLD"] | ||
|
|
||
| def test_cyan_categories(self): | ||
| """Test categories that should return CYAN color""" | ||
| self.assertEqual(io.get_category_color("File Management"), io.CYAN) | ||
| self.assertEqual(io.get_category_color("Disk Utilities"), io.CYAN) | ||
| self.assertEqual(io.get_category_color("file"), io.CYAN) | ||
| self.assertEqual(io.get_category_color("DISK"), io.CYAN) | ||
|
|
||
| def test_magenta_categories(self): | ||
| """Test categories that should return MAGENTA color""" | ||
| self.assertEqual(io.get_category_color("Network"), io.MAGENTA) | ||
| self.assertEqual(io.get_category_color("Web Tools"), io.MAGENTA) | ||
| self.assertEqual(io.get_category_color("network admin"), io.MAGENTA) | ||
| self.assertEqual(io.get_category_color("WEB"), io.MAGENTA) | ||
|
|
||
| def test_red_categories(self): | ||
| """Test categories that should return RED color""" | ||
| self.assertEqual(io.get_category_color("Process Management"), io.RED) | ||
| self.assertEqual(io.get_category_color("System Utils"), io.RED) | ||
| self.assertEqual(io.get_category_color("process"), io.RED) | ||
| self.assertEqual(io.get_category_color("SYSTEM"), io.RED) | ||
|
|
||
| def test_green_categories(self): | ||
| """Test categories that should return GREEN color""" | ||
| self.assertEqual(io.get_category_color("Navigation"), io.GREEN) | ||
| self.assertEqual(io.get_category_color("Search Tools"), io.GREEN) | ||
| self.assertEqual(io.get_category_color("navig"), io.GREEN) | ||
| self.assertEqual(io.get_category_color("SEARCH"), io.GREEN) | ||
|
|
||
| def test_yellow_categories(self): | ||
| """Test categories that should return YELLOW color""" | ||
| self.assertEqual(io.get_category_color("Text Editors"), io.YELLOW) | ||
| self.assertEqual(io.get_category_color("Edit"), io.YELLOW) | ||
| self.assertEqual(io.get_category_color("Text Management"), io.YELLOW) | ||
| self.assertEqual(io.get_category_color("EDIT"), io.YELLOW) | ||
|
|
||
| def test_fallback_categories(self): | ||
| """Test categories that don't match any specific keyword should return BOLD""" | ||
| self.assertEqual(io.get_category_color("Unknown"), io.BOLD) | ||
| self.assertEqual(io.get_category_color("Misc"), io.BOLD) | ||
| self.assertEqual(io.get_category_color("Other Utilities"), io.BOLD) | ||
| self.assertEqual(io.get_category_color(""), io.BOLD) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
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.
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.
In non-TTY environments (such as CI/CD pipelines like GitHub Actions) or when
NO_COLORis set, the color constants incommando/utils/io.pyare initialized to empty strings ("").Because of this, importing and asserting against these constants directly in tests can lead to false positives (false greens). For example, if
get_category_colorhas a bug and returnsREDinstead ofCYAN, the assertionself.assertEqual(get_category_color("File Management"), CYAN)will compare""with""and pass successfully, masking the bug.To make the tests robust and reliable in all environments, we should override these color constants with distinct mock values during the test execution.