Skip to content

Commit 8ed66a2

Browse files
committed
add tests for the change
Signed-off-by: Frost Ming <me@frostming.com>
1 parent 6c02905 commit 8ed66a2

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Lib/test/test_argparse.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7558,6 +7558,40 @@ def test_error_and_warning_not_colorized_when_disabled(self):
75587558
self.assertNotIn('\x1b[', warn)
75597559
self.assertIn('warning:', warn)
75607560

7561+
def test_print_help_uses_target_file_for_color_decision(self):
7562+
parser = argparse.ArgumentParser(prog='PROG', color=True)
7563+
parser.add_argument('--opt')
7564+
output = io.StringIO()
7565+
calls = []
7566+
7567+
def fake_can_colorize(*, file=None):
7568+
calls.append(file)
7569+
return file is None
7570+
7571+
with swap_attr(_colorize, 'can_colorize', fake_can_colorize):
7572+
parser.print_help(file=output)
7573+
7574+
self.assertIs(calls[-1], output)
7575+
self.assertIn(output, calls)
7576+
self.assertNotIn('\x1b[', output.getvalue())
7577+
7578+
def test_print_usage_uses_target_file_for_color_decision(self):
7579+
parser = argparse.ArgumentParser(prog='PROG', color=True)
7580+
parser.add_argument('--opt')
7581+
output = io.StringIO()
7582+
calls = []
7583+
7584+
def fake_can_colorize(*, file=None):
7585+
calls.append(file)
7586+
return file is None
7587+
7588+
with swap_attr(_colorize, 'can_colorize', fake_can_colorize):
7589+
parser.print_usage(file=output)
7590+
7591+
self.assertIs(calls[-1], output)
7592+
self.assertIn(output, calls)
7593+
self.assertNotIn('\x1b[', output.getvalue())
7594+
75617595

75627596
class TestModule(unittest.TestCase):
75637597
def test_deprecated__version__(self):

0 commit comments

Comments
 (0)