@@ -31,37 +31,35 @@ def with_config(config):
3131 return with_config
3232
3333
34- def check_all ( args , expected , ** kwargs ):
34+ def invoke ( cli , args = None , env = None ):
3535 runner = CliRunner (catch_exceptions = False )
36+ result = runner .invoke (cli , args , env = env , standalone_mode = False )
37+ return result .return_value
3638
37- result = runner .invoke (cli , args , standalone_mode = False , ** kwargs )
38- assert expected == result .return_value
39-
40- loaded = load_config (cli , args )
41- assert expected == loaded
42-
43-
44- def check_error (args , strings = (), exc_type = click .BadParameter , ** kwargs ):
45- runner = CliRunner (catch_exceptions = False )
4639
40+ def check_error (strings = (), exc_type = click .BadParameter ):
4741 with pytest .raises (exc_type ) as exc_info :
48- runner . invoke (cli , args , standalone_mode = False , ** kwargs )
42+ invoke (cli , [ 'sub' ] )
4943 for s in strings :
5044 assert s in str (exc_info .value ).lower ()
5145
5246 with pytest .raises (exc_type ) as exc_info :
53- loaded = load_config (cli , args )
47+ loaded = load_config (cli )
5448 for s in strings :
5549 assert s in str (exc_info .value ).lower ()
5650
5751
5852def test_no_config ():
59- check_all (['sub' ], {'' : {'plugin' : ()}, 'sub' : {'option' : 'DEFAULT' }})
53+ expected = {'' : {'plugin' : ()}, 'sub' : {'option' : 'DEFAULT' }}
54+ assert invoke (cli , ['sub' ]) == expected
55+ assert load_config (cli ) == expected
6056
6157
6258def test_empty_config (with_config ):
6359 with_config ("[cli]" )
64- check_all (['sub' ], {'' : {'plugin' : ()}, 'sub' : {'option' : 'DEFAULT' }})
60+ expected = {'' : {'plugin' : ()}, 'sub' : {'option' : 'DEFAULT' }}
61+ assert invoke (cli , ['sub' ]) == expected
62+ assert load_config (cli ) == expected
6563
6664
6765def test_config (with_config ):
@@ -73,43 +71,37 @@ def test_config(with_config):
7371 option='config'
7472 """
7573 )
76- load_config (cli , []) == {'' : {'plugin' : ('CONFIG' ,)}}
77- check_all (
78- ['sub' ],
79- {
80- '' : {'plugin' : ('CONFIG' ,)},
81- 'sub' : {'option' : 'CONFIG' },
82- },
83- )
84- check_all (
85- ['--plugin' , 'user' , 'sub' , '--option' , 'user' ],
86- {
87- '' : {'plugin' : ('CONFIG' , 'USER' )},
88- 'sub' : {'option' : 'USER' },
89- },
90- )
74+
75+ expected = {'' : {'plugin' : ('CONFIG' ,)}, 'sub' : {'option' : 'CONFIG' }}
76+ assert invoke (cli , ['sub' ]) == expected
77+ assert load_config (cli ) == expected
78+
79+ env = {'CLI_SUB_OPTION' : 'env' }
80+ expected = {'' : {'plugin' : ('CONFIG' , 'USER' )}, 'sub' : {'option' : 'ENV' }}
81+ assert invoke (cli , ['--plugin' , 'user' , 'sub' ], env = env )
82+ assert load_config (cli , ['--plugin' , 'user' ], env = env )
9183
9284
9385def test_toml_error (with_config ):
9486 with_config ("[cli" )
95- check_error (['sub' ], [ ' toml error' ])
87+ check_error (['toml error' ])
9688
9789
9890def test_no_section_error (with_config ):
9991 with_config ("" )
100- check_error (['sub' ], [ ' no [cli] section' ])
92+ check_error (['no [cli] section' ])
10193
10294
10395def test_bad_section_type_error (with_config ):
10496 with_config ("cli = 1" )
105- check_error (['sub' ], [ ' cli:' , 'expected mapping' ])
97+ check_error (['cli:' , 'expected mapping' ])
10698
10799
108100def test_unknown_option_error (with_config ):
109101 with_config ("[cli]\n unknown = 1" )
110- check_error (['sub' ], [ ' cli.unknown:' , 'no such option' ])
102+ check_error (['cli.unknown:' , 'no such option' ])
111103
112104
113105def test_unknown_command_error (with_config ):
114106 with_config ("[cli.unknown]" )
115- check_error (['sub' ], [ ' cli.unknown:' , 'no such option' ])
107+ check_error (['cli.unknown:' , 'no such option' ])
0 commit comments