From d42e5a733eef0c87d939426410f3536ebebd4854 Mon Sep 17 00:00:00 2001 From: Anthony James Padavano Date: Tue, 17 Nov 2026 16:35:32 -0500 Subject: [PATCH] test(formatting): snapshot and restore full Colors state in disable tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Colors.disable() blanks every uppercase attribute, but test_colors_disable (test_formatting.py) and test_colors_disable_sets_empty_strings (test_formatting_coverage.py) saved and restored only RED/RESET. The remaining attributes (GREEN, YELLOW, BLUE, BOLD_*, DIM, ...) stayed blanked for the rest of the process, so any later test asserting on them failed depending on execution order — e.g. TestColors::test_colors_defined after test_colors_disable, and TestFormattingNoColor:: test_no_color_env_triggers_disable (precondition Colors.GREEN != '') after test_formatting.py. Both tests now snapshot the full Colors class dict and restore it in a finally block. Test-local isolation only; Colors.disable() semantics and all production code are untouched. Refs #34. --- rege/tests/test_formatting.py | 30 +++++++++++++++----------- rege/tests/test_formatting_coverage.py | 30 +++++++++++++++----------- 2 files changed, 34 insertions(+), 26 deletions(-) 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."""