|
30 | 30 | from unittest import mock |
31 | 31 |
|
32 | 32 |
|
| 33 | + |
33 | 34 | py = os.path.basename(sys.executable) |
34 | 35 |
|
35 | 36 |
|
@@ -7356,6 +7357,28 @@ def __init__(self, prog): |
7356 | 7357 | ''')) |
7357 | 7358 |
|
7358 | 7359 |
|
| 7360 | +class TestColorEnvironment(TestCase): |
| 7361 | + """Tests for color behavior with environment variable changes.""" |
| 7362 | + |
| 7363 | + def test_subparser_respects_no_color_environment(self): |
| 7364 | + # Cleanup to ensure environment is properly reset |
| 7365 | + self.addCleanup(os.environ.pop, 'FORCE_COLOR', None) |
| 7366 | + self.addCleanup(os.environ.pop, 'NO_COLOR', None) |
| 7367 | + |
| 7368 | + # Create parser with FORCE_COLOR, capturing colored prog |
| 7369 | + os.environ['FORCE_COLOR'] = '1' |
| 7370 | + parser = argparse.ArgumentParser(prog='complex') |
| 7371 | + sub = parser.add_subparsers(dest='command') |
| 7372 | + demo_parser = sub.add_parser('demo') |
| 7373 | + |
| 7374 | + # Switch to NO_COLOR environment |
| 7375 | + os.environ.pop('FORCE_COLOR', None) |
| 7376 | + os.environ['NO_COLOR'] = '1' |
| 7377 | + |
| 7378 | + # Subparser help should have no color codes |
| 7379 | + help_text = demo_parser.format_help() |
| 7380 | + self.assertNotIn('\x1b[', help_text) |
| 7381 | + |
7359 | 7382 | class TestModule(unittest.TestCase): |
7360 | 7383 | def test_deprecated__version__(self): |
7361 | 7384 | with self.assertWarnsRegex( |
|
0 commit comments