From 62108cfbcd4f762062d8776ef2b1fd57a4039179 Mon Sep 17 00:00:00 2001 From: Mateeh Ullah Date: Sun, 1 Nov 2026 19:54:49 +0500 Subject: [PATCH 1/2] fix(windows): avoid Colorama initialization in Git Bash (#575) --- fire/formatting_windows.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fire/formatting_windows.py b/fire/formatting_windows.py index 749ab6d0..c2b00f0f 100644 --- a/fire/formatting_windows.py +++ b/fire/formatting_windows.py @@ -29,6 +29,10 @@ def initialize_or_disable(): """Enables ANSI processing on Windows or disables it as needed.""" + # Git Bash's MinGW terminals already handle ANSI sequences. Initializing + # Colorama would unnecessarily wrap stdout and can interfere with colors. + if os.environ.get('MSYSTEM', '').upper().startswith(('MINGW', 'MSYS')): + return if HAS_COLORAMA: wrap = True if (hasattr(sys.stdout, 'isatty') From c11b33608f66b77a1b843ff173a4524db3eb8f28 Mon Sep 17 00:00:00 2001 From: Mateeh Ullah Date: Sun, 1 Nov 2026 19:55:04 +0500 Subject: [PATCH 2/2] test(windows): cover Git Bash and native console initialization --- fire/formatting_windows_test.py | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 fire/formatting_windows_test.py diff --git a/fire/formatting_windows_test.py b/fire/formatting_windows_test.py new file mode 100644 index 00000000..fe9894f9 --- /dev/null +++ b/fire/formatting_windows_test.py @@ -0,0 +1,55 @@ +# Copyright (C) 2026 Google LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests for Windows ANSI initialization behavior.""" + +from unittest import mock + +from fire import formatting_windows +from fire import testutils + + +class FormattingWindowsTest(testutils.BaseTestCase): + + def test_git_bash_skips_colorama(self): + for msystem in ('MINGW64', 'MINGW32', 'MSYS', 'mingw64'): + with self.subTest(msystem=msystem): + with mock.patch.dict('os.environ', {'MSYSTEM': msystem}): + with mock.patch.object(formatting_windows, 'HAS_COLORAMA', True): + with mock.patch.object(formatting_windows, 'colorama', + create=True) as colorama: + with mock.patch.object(formatting_windows.platform, 'release', + side_effect=AssertionError('not needed')): + formatting_windows.initialize_or_disable() + colorama.init.assert_not_called() + + def test_non_git_bash_keeps_colorama_initialization(self): + with mock.patch.dict('os.environ', {}, clear=True): + with mock.patch.object(formatting_windows, 'HAS_COLORAMA', True): + with mock.patch.object(formatting_windows, 'colorama', + create=True) as colorama: + with mock.patch.object(formatting_windows.platform, 'release', + return_value='11'): + formatting_windows.initialize_or_disable() + colorama.init.assert_called_once_with(wrap=True) + + def test_missing_colorama_disables_colors_outside_git_bash(self): + with mock.patch.dict('os.environ', {}, clear=True): + with mock.patch.object(formatting_windows, 'HAS_COLORAMA', False): + formatting_windows.initialize_or_disable() + self.assertEqual('1', formatting_windows.os.environ['ANSI_COLORS_DISABLED']) + + +if __name__ == '__main__': + testutils.main()