Skip to content

Commit ffe1b6e

Browse files
committed
update test
1 parent 7265921 commit ffe1b6e

File tree

1 file changed

+70
-30
lines changed

1 file changed

+70
-30
lines changed

Lib/test/test_argparse.py

+70-30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import tempfile
1212
import unittest
1313
import argparse
14+
import warnings
1415

1516
from test.support import os_helper
1617
from unittest import mock
@@ -40,11 +41,11 @@ def setUp(self):
4041
# The tests assume that line wrapping occurs at 80 columns, but this
4142
# behaviour can be overridden by setting the COLUMNS environment
4243
# variable. To ensure that this width is used, set COLUMNS to 80.
43-
env = os_helper.EnvironmentVarGuard()
44+
env = self.enterContext(os_helper.EnvironmentVarGuard())
4445
env['COLUMNS'] = '80'
45-
self.addCleanup(env.__exit__)
4646

4747

48+
@os_helper.skip_unless_working_chmod
4849
class TempDirMixin(object):
4950

5051
def setUp(self):
@@ -295,7 +296,7 @@ class TestOptionalsSingleDashCombined(ParserTestCase):
295296
Sig('-z'),
296297
]
297298
failures = ['a', '--foo', '-xa', '-x --foo', '-x -z', '-z -x',
298-
'-yx', '-yz a', '-yyyx', '-yyyza', '-xyza']
299+
'-yx', '-yz a', '-yyyx', '-yyyza', '-xyza', '-x=']
299300
successes = [
300301
('', NS(x=False, yyy=None, z=None)),
301302
('-x', NS(x=True, yyy=None, z=None)),
@@ -769,6 +770,25 @@ class TestOptionalsActionAppendWithDefault(ParserTestCase):
769770
]
770771

771772

773+
class TestConstActionsMissingConstKwarg(ParserTestCase):
774+
"""Tests that const gets default value of None when not provided"""
775+
776+
argument_signatures = [
777+
Sig('-f', action='append_const'),
778+
Sig('--foo', action='append_const'),
779+
Sig('-b', action='store_const'),
780+
Sig('--bar', action='store_const')
781+
]
782+
failures = ['-f v', '--foo=bar', '--foo bar']
783+
successes = [
784+
('', NS(f=None, foo=None, b=None, bar=None)),
785+
('-f', NS(f=[None], foo=None, b=None, bar=None)),
786+
('--foo', NS(f=None, foo=[None], b=None, bar=None)),
787+
('-b', NS(f=None, foo=None, b=None, bar=None)),
788+
('--bar', NS(f=None, foo=None, b=None, bar=None)),
789+
]
790+
791+
772792
class TestOptionalsActionAppendConst(ParserTestCase):
773793
"""Tests the append_const action for an Optional"""
774794

@@ -1703,8 +1723,7 @@ def __eq__(self, other):
17031723
return self.name == other.name
17041724

17051725

1706-
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
1707-
"non-root user required")
1726+
@os_helper.skip_if_dac_override
17081727
class TestFileTypeW(TempDirMixin, ParserTestCase):
17091728
"""Test the FileType option/argument type for writing files"""
17101729

@@ -1726,8 +1745,8 @@ def setUp(self):
17261745
('-x - -', NS(x=eq_stdout, spam=eq_stdout)),
17271746
]
17281747

1729-
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
1730-
"non-root user required")
1748+
1749+
@os_helper.skip_if_dac_override
17311750
class TestFileTypeX(TempDirMixin, ParserTestCase):
17321751
"""Test the FileType option/argument type for writing new files only"""
17331752

@@ -1747,8 +1766,7 @@ def setUp(self):
17471766
]
17481767

17491768

1750-
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
1751-
"non-root user required")
1769+
@os_helper.skip_if_dac_override
17521770
class TestFileTypeWB(TempDirMixin, ParserTestCase):
17531771
"""Test the FileType option/argument type for writing binary files"""
17541772

@@ -1765,8 +1783,7 @@ class TestFileTypeWB(TempDirMixin, ParserTestCase):
17651783
]
17661784

17671785

1768-
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
1769-
"non-root user required")
1786+
@os_helper.skip_if_dac_override
17701787
class TestFileTypeXB(TestFileTypeX):
17711788
"Test the FileType option/argument type for writing new binary files only"
17721789

@@ -2245,8 +2262,7 @@ def test_help_blank(self):
22452262
main description
22462263
22472264
positional arguments:
2248-
foo
2249-
2265+
foo \n
22502266
options:
22512267
-h, --help show this help message and exit
22522268
'''))
@@ -2262,8 +2278,7 @@ def test_help_blank(self):
22622278
main description
22632279
22642280
positional arguments:
2265-
{}
2266-
2281+
{} \n
22672282
options:
22682283
-h, --help show this help message and exit
22692284
'''))
@@ -3041,15 +3056,24 @@ def get_parser(self, required):
30413056

30423057
class TestMutuallyExclusiveNested(MEMixin, TestCase):
30433058

3059+
# Nesting mutually exclusive groups is an undocumented feature
3060+
# that came about by accident through inheritance and has been
3061+
# the source of many bugs. It is deprecated and this test should
3062+
# eventually be removed along with it.
3063+
30443064
def get_parser(self, required):
30453065
parser = ErrorRaisingArgumentParser(prog='PROG')
30463066
group = parser.add_mutually_exclusive_group(required=required)
30473067
group.add_argument('-a')
30483068
group.add_argument('-b')
3049-
group2 = group.add_mutually_exclusive_group(required=required)
3069+
with warnings.catch_warnings():
3070+
warnings.simplefilter('ignore', DeprecationWarning)
3071+
group2 = group.add_mutually_exclusive_group(required=required)
30503072
group2.add_argument('-c')
30513073
group2.add_argument('-d')
3052-
group3 = group2.add_mutually_exclusive_group(required=required)
3074+
with warnings.catch_warnings():
3075+
warnings.simplefilter('ignore', DeprecationWarning)
3076+
group3 = group2.add_mutually_exclusive_group(required=required)
30533077
group3.add_argument('-e')
30543078
group3.add_argument('-f')
30553079
return parser
@@ -3321,6 +3345,7 @@ def _get_parser(self, tester):
33213345
def _test(self, tester, parser_text):
33223346
expected_text = getattr(tester, self.func_suffix)
33233347
expected_text = textwrap.dedent(expected_text)
3348+
tester.maxDiff = None
33243349
tester.assertEqual(expected_text, parser_text)
33253350

33263351
def test_format(self, tester):
@@ -3400,9 +3425,8 @@ class TestShortColumns(HelpTestCase):
34003425
but we don't want any exceptions thrown in such cases. Only ugly representation.
34013426
'''
34023427
def setUp(self):
3403-
env = os_helper.EnvironmentVarGuard()
3428+
env = self.enterContext(os_helper.EnvironmentVarGuard())
34043429
env.set("COLUMNS", '15')
3405-
self.addCleanup(env.__exit__)
34063430

34073431
parser_signature = TestHelpBiggerOptionals.parser_signature
34083432
argument_signatures = TestHelpBiggerOptionals.argument_signatures
@@ -3716,7 +3740,7 @@ class TestHelpUsage(HelpTestCase):
37163740
-w W [W ...] w
37173741
-x [X ...] x
37183742
--foo, --no-foo Whether to foo
3719-
--bar, --no-bar Whether to bar (default: True)
3743+
--bar, --no-bar Whether to bar
37203744
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
37213745
--bazz, --no-bazz Bazz!
37223746
@@ -4396,6 +4420,8 @@ class TestHelpArgumentDefaults(HelpTestCase):
43964420
Sig('--bar', action='store_true', help='bar help'),
43974421
Sig('--taz', action=argparse.BooleanOptionalAction,
43984422
help='Whether to taz it', default=True),
4423+
Sig('--corge', action=argparse.BooleanOptionalAction,
4424+
help='Whether to corge it', default=argparse.SUPPRESS),
43994425
Sig('--quux', help="Set the quux", default=42),
44004426
Sig('spam', help='spam help'),
44014427
Sig('badger', nargs='?', default='wooden', help='badger help'),
@@ -4405,29 +4431,30 @@ class TestHelpArgumentDefaults(HelpTestCase):
44054431
[Sig('--baz', type=int, default=42, help='baz help')]),
44064432
]
44074433
usage = '''\
4408-
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--quux QUUX]
4409-
[--baz BAZ]
4434+
usage: PROG [-h] [--foo FOO] [--bar] [--taz | --no-taz] [--corge | --no-corge]
4435+
[--quux QUUX] [--baz BAZ]
44104436
spam [badger]
44114437
'''
44124438
help = usage + '''\
44134439
44144440
description
44154441
44164442
positional arguments:
4417-
spam spam help
4418-
badger badger help (default: wooden)
4443+
spam spam help
4444+
badger badger help (default: wooden)
44194445
44204446
options:
4421-
-h, --help show this help message and exit
4422-
--foo FOO foo help - oh and by the way, None
4423-
--bar bar help (default: False)
4424-
--taz, --no-taz Whether to taz it (default: True)
4425-
--quux QUUX Set the quux (default: 42)
4447+
-h, --help show this help message and exit
4448+
--foo FOO foo help - oh and by the way, None
4449+
--bar bar help (default: False)
4450+
--taz, --no-taz Whether to taz it (default: True)
4451+
--corge, --no-corge Whether to corge it
4452+
--quux QUUX Set the quux (default: 42)
44264453
44274454
title:
44284455
description
44294456
4430-
--baz BAZ baz help (default: 42)
4457+
--baz BAZ baz help (default: 42)
44314458
'''
44324459
version = ''
44334460

@@ -4777,6 +4804,19 @@ def test_resolve_error(self):
47774804
--spam NEW_SPAM
47784805
'''))
47794806

4807+
def test_subparser_conflict(self):
4808+
parser = argparse.ArgumentParser()
4809+
sp = parser.add_subparsers()
4810+
sp.add_parser('fullname', aliases=['alias'])
4811+
self.assertRaises(argparse.ArgumentError,
4812+
sp.add_parser, 'fullname')
4813+
self.assertRaises(argparse.ArgumentError,
4814+
sp.add_parser, 'alias')
4815+
self.assertRaises(argparse.ArgumentError,
4816+
sp.add_parser, 'other', aliases=['fullname'])
4817+
self.assertRaises(argparse.ArgumentError,
4818+
sp.add_parser, 'other', aliases=['alias'])
4819+
47804820

47814821
# =============================
47824822
# Help and Version option tests

0 commit comments

Comments
 (0)