11
11
import tempfile
12
12
import unittest
13
13
import argparse
14
+ import warnings
14
15
15
16
from test .support import os_helper
16
17
from unittest import mock
@@ -40,11 +41,11 @@ def setUp(self):
40
41
# The tests assume that line wrapping occurs at 80 columns, but this
41
42
# behaviour can be overridden by setting the COLUMNS environment
42
43
# variable. To ensure that this width is used, set COLUMNS to 80.
43
- env = os_helper .EnvironmentVarGuard ()
44
+ env = self . enterContext ( os_helper .EnvironmentVarGuard () )
44
45
env ['COLUMNS' ] = '80'
45
- self .addCleanup (env .__exit__ )
46
46
47
47
48
+ @os_helper .skip_unless_working_chmod
48
49
class TempDirMixin (object ):
49
50
50
51
def setUp (self ):
@@ -295,7 +296,7 @@ class TestOptionalsSingleDashCombined(ParserTestCase):
295
296
Sig ('-z' ),
296
297
]
297
298
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=' ]
299
300
successes = [
300
301
('' , NS (x = False , yyy = None , z = None )),
301
302
('-x' , NS (x = True , yyy = None , z = None )),
@@ -769,6 +770,25 @@ class TestOptionalsActionAppendWithDefault(ParserTestCase):
769
770
]
770
771
771
772
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
+
772
792
class TestOptionalsActionAppendConst (ParserTestCase ):
773
793
"""Tests the append_const action for an Optional"""
774
794
@@ -1703,8 +1723,7 @@ def __eq__(self, other):
1703
1723
return self .name == other .name
1704
1724
1705
1725
1706
- @unittest .skipIf (hasattr (os , 'geteuid' ) and os .geteuid () == 0 ,
1707
- "non-root user required" )
1726
+ @os_helper .skip_if_dac_override
1708
1727
class TestFileTypeW (TempDirMixin , ParserTestCase ):
1709
1728
"""Test the FileType option/argument type for writing files"""
1710
1729
@@ -1726,8 +1745,8 @@ def setUp(self):
1726
1745
('-x - -' , NS (x = eq_stdout , spam = eq_stdout )),
1727
1746
]
1728
1747
1729
- @ unittest . skipIf ( hasattr ( os , 'geteuid' ) and os . geteuid () == 0 ,
1730
- "non-root user required" )
1748
+
1749
+ @ os_helper . skip_if_dac_override
1731
1750
class TestFileTypeX (TempDirMixin , ParserTestCase ):
1732
1751
"""Test the FileType option/argument type for writing new files only"""
1733
1752
@@ -1747,8 +1766,7 @@ def setUp(self):
1747
1766
]
1748
1767
1749
1768
1750
- @unittest .skipIf (hasattr (os , 'geteuid' ) and os .geteuid () == 0 ,
1751
- "non-root user required" )
1769
+ @os_helper .skip_if_dac_override
1752
1770
class TestFileTypeWB (TempDirMixin , ParserTestCase ):
1753
1771
"""Test the FileType option/argument type for writing binary files"""
1754
1772
@@ -1765,8 +1783,7 @@ class TestFileTypeWB(TempDirMixin, ParserTestCase):
1765
1783
]
1766
1784
1767
1785
1768
- @unittest .skipIf (hasattr (os , 'geteuid' ) and os .geteuid () == 0 ,
1769
- "non-root user required" )
1786
+ @os_helper .skip_if_dac_override
1770
1787
class TestFileTypeXB (TestFileTypeX ):
1771
1788
"Test the FileType option/argument type for writing new binary files only"
1772
1789
@@ -2245,8 +2262,7 @@ def test_help_blank(self):
2245
2262
main description
2246
2263
2247
2264
positional arguments:
2248
- foo
2249
-
2265
+ foo \n
2250
2266
options:
2251
2267
-h, --help show this help message and exit
2252
2268
''' ))
@@ -2262,8 +2278,7 @@ def test_help_blank(self):
2262
2278
main description
2263
2279
2264
2280
positional arguments:
2265
- {}
2266
-
2281
+ {} \n
2267
2282
options:
2268
2283
-h, --help show this help message and exit
2269
2284
''' ))
@@ -3041,15 +3056,24 @@ def get_parser(self, required):
3041
3056
3042
3057
class TestMutuallyExclusiveNested (MEMixin , TestCase ):
3043
3058
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
+
3044
3064
def get_parser (self , required ):
3045
3065
parser = ErrorRaisingArgumentParser (prog = 'PROG' )
3046
3066
group = parser .add_mutually_exclusive_group (required = required )
3047
3067
group .add_argument ('-a' )
3048
3068
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 )
3050
3072
group2 .add_argument ('-c' )
3051
3073
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 )
3053
3077
group3 .add_argument ('-e' )
3054
3078
group3 .add_argument ('-f' )
3055
3079
return parser
@@ -3321,6 +3345,7 @@ def _get_parser(self, tester):
3321
3345
def _test (self , tester , parser_text ):
3322
3346
expected_text = getattr (tester , self .func_suffix )
3323
3347
expected_text = textwrap .dedent (expected_text )
3348
+ tester .maxDiff = None
3324
3349
tester .assertEqual (expected_text , parser_text )
3325
3350
3326
3351
def test_format (self , tester ):
@@ -3400,9 +3425,8 @@ class TestShortColumns(HelpTestCase):
3400
3425
but we don't want any exceptions thrown in such cases. Only ugly representation.
3401
3426
'''
3402
3427
def setUp (self ):
3403
- env = os_helper .EnvironmentVarGuard ()
3428
+ env = self . enterContext ( os_helper .EnvironmentVarGuard () )
3404
3429
env .set ("COLUMNS" , '15' )
3405
- self .addCleanup (env .__exit__ )
3406
3430
3407
3431
parser_signature = TestHelpBiggerOptionals .parser_signature
3408
3432
argument_signatures = TestHelpBiggerOptionals .argument_signatures
@@ -3716,7 +3740,7 @@ class TestHelpUsage(HelpTestCase):
3716
3740
-w W [W ...] w
3717
3741
-x [X ...] x
3718
3742
--foo, --no-foo Whether to foo
3719
- --bar, --no-bar Whether to bar (default: True)
3743
+ --bar, --no-bar Whether to bar
3720
3744
-f, --foobar, --no-foobar, --barfoo, --no-barfoo
3721
3745
--bazz, --no-bazz Bazz!
3722
3746
@@ -4396,6 +4420,8 @@ class TestHelpArgumentDefaults(HelpTestCase):
4396
4420
Sig ('--bar' , action = 'store_true' , help = 'bar help' ),
4397
4421
Sig ('--taz' , action = argparse .BooleanOptionalAction ,
4398
4422
help = 'Whether to taz it' , default = True ),
4423
+ Sig ('--corge' , action = argparse .BooleanOptionalAction ,
4424
+ help = 'Whether to corge it' , default = argparse .SUPPRESS ),
4399
4425
Sig ('--quux' , help = "Set the quux" , default = 42 ),
4400
4426
Sig ('spam' , help = 'spam help' ),
4401
4427
Sig ('badger' , nargs = '?' , default = 'wooden' , help = 'badger help' ),
@@ -4405,29 +4431,30 @@ class TestHelpArgumentDefaults(HelpTestCase):
4405
4431
[Sig ('--baz' , type = int , default = 42 , help = 'baz help' )]),
4406
4432
]
4407
4433
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]
4410
4436
spam [badger]
4411
4437
'''
4412
4438
help = usage + '''\
4413
4439
4414
4440
description
4415
4441
4416
4442
positional arguments:
4417
- spam spam help
4418
- badger badger help (default: wooden)
4443
+ spam spam help
4444
+ badger badger help (default: wooden)
4419
4445
4420
4446
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)
4426
4453
4427
4454
title:
4428
4455
description
4429
4456
4430
- --baz BAZ baz help (default: 42)
4457
+ --baz BAZ baz help (default: 42)
4431
4458
'''
4432
4459
version = ''
4433
4460
@@ -4777,6 +4804,19 @@ def test_resolve_error(self):
4777
4804
--spam NEW_SPAM
4778
4805
''' ))
4779
4806
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
+
4780
4820
4781
4821
# =============================
4782
4822
# Help and Version option tests
0 commit comments