diff --git a/rege/tests/test_formatting.py b/rege/tests/test_formatting.py index 2446a94..7e051a3 100644 --- a/rege/tests/test_formatting.py +++ b/rege/tests/test_formatting.py @@ -31,19 +31,23 @@ def test_colors_defined(self): def test_colors_disable(self): """Test colors can be disabled.""" - # Save original values - original_red = Colors.RED - original_reset = Colors.RESET - - # Disable - Colors.disable() - - assert Colors.RED == "" - assert Colors.RESET == "" - - # Restore - Colors.RED = original_red - Colors.RESET = original_reset + # Save original values: disable() blanks every uppercase attribute, + # so snapshot the full class state — restoring only some attributes + # leaks blanked colors to later tests in the same process. + original = { + attr: getattr(Colors, attr) for attr in dir(Colors) if attr.isupper() + } + + try: + # Disable + Colors.disable() + + assert Colors.RED == "" + assert Colors.RESET == "" + finally: + # Restore + for attr, value in original.items(): + setattr(Colors, attr, value) class TestColorize: diff --git a/rege/tests/test_formatting_coverage.py b/rege/tests/test_formatting_coverage.py index de6f245..647559c 100644 --- a/rege/tests/test_formatting_coverage.py +++ b/rege/tests/test_formatting_coverage.py @@ -15,21 +15,25 @@ class TestColorsDisable: def test_colors_disable_sets_empty_strings(self): """Test Colors.disable sets all uppercase attrs to empty strings.""" from rege.formatting import Colors - # Save original values - original_reset = Colors.RESET - original_red = Colors.RED + # Save original values: disable() blanks every uppercase attribute, + # so snapshot the full class state — restoring only some attributes + # leaks blanked colors to later tests in the same process. + original = { + attr: getattr(Colors, attr) for attr in dir(Colors) if attr.isupper() + } - Colors.disable() - - assert Colors.RESET == "" - assert Colors.RED == "" - assert Colors.GREEN == "" - assert Colors.BOLD_RED == "" - assert Colors.DIM == "" + try: + Colors.disable() - # Restore for other tests - Colors.RESET = original_reset - Colors.RED = original_red + assert Colors.RESET == "" + assert Colors.RED == "" + assert Colors.GREEN == "" + assert Colors.BOLD_RED == "" + assert Colors.DIM == "" + finally: + # Restore for other tests + for attr, value in original.items(): + setattr(Colors, attr, value) def test_colors_disable_no_color_env(self): """Test that NO_COLOR env triggers disable at import time."""