Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions tests/test_io.py
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()
Comment on lines +1 to +79

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In non-TTY environments (such as CI/CD pipelines like GitHub Actions) or when NO_COLOR is set, the color constants in commando/utils/io.py are 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_color has a bug and returns RED instead of CYAN, the assertion self.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.

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 processing"), 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()

Loading