@@ -56,13 +56,14 @@ def foo():
56
56
assert capsys .readouterr ().out == "Foo!\n "
57
57
58
58
59
- def test_group_command_called_once (capsys ):
59
+ def test_group_command_called_on_each_subcommands (capsys ):
60
60
@click .group (invoke_without_command = True )
61
61
@click .pass_context
62
62
def cli (ctx ):
63
- print ("cli()" )
64
63
if ctx .invoked_subcommand is None :
65
64
click_repl .repl (ctx )
65
+ else :
66
+ print ("cli()" )
66
67
67
68
@cli .command ()
68
69
def foo ():
@@ -74,27 +75,52 @@ def bar():
74
75
75
76
with mock_stdin ("foo\n bar\n " ):
76
77
with pytest .raises (SystemExit ):
77
- cli (args = [], prog_name = "test_group_called_once " )
78
- assert capsys .readouterr ().out == "cli()\n Foo!\n Bar!\n "
78
+ cli (args = [], prog_name = "test_group_command_called_on_each_subcommands " )
79
+ assert capsys .readouterr ().out == "cli()\n Foo!\n cli() \ n Bar!\n "
79
80
80
81
81
- def test_independant_args (capsys ):
82
+ def test_group_argument_are_preserved (capsys ):
82
83
@click .group (invoke_without_command = True )
83
84
@click .argument ("argument" )
84
85
@click .pass_context
85
86
def cli (ctx , argument ):
86
- print ("cli(%s)" % argument )
87
87
if ctx .invoked_subcommand is None :
88
88
click_repl .repl (ctx )
89
+ else :
90
+ print ("cli(%s)" % argument )
91
+
92
+ @cli .command ()
93
+ @click .argument ("argument" )
94
+ def foo (argument ):
95
+ print ("Foo: %s!" % argument )
96
+
97
+ with mock_stdin ("foo bar\n " ):
98
+ with pytest .raises (SystemExit ):
99
+ cli (args = ["arg" ], prog_name = "test_group_argument_are_preserved" )
100
+ assert capsys .readouterr ().out == "cli(arg)\n Foo: bar!\n "
101
+
102
+
103
+ def test_chain_commands (capsys ):
104
+ @click .group (invoke_without_command = True , chain = True )
105
+ @click .pass_context
106
+ def cli (ctx ):
107
+ if ctx .invoked_subcommand is None :
108
+ click_repl .repl (ctx )
109
+ else :
110
+ print ("cli()" )
89
111
90
112
@cli .command ()
91
113
def foo ():
92
114
print ("Foo!" )
93
115
94
- with mock_stdin ("foo\n " ):
116
+ @cli .command ()
117
+ def bar ():
118
+ print ("Bar!" )
119
+
120
+ with mock_stdin ("foo bar\n " ):
95
121
with pytest .raises (SystemExit ):
96
- cli (args = ["command-line-argument" ], prog_name = "test_group_called_once " )
97
- assert capsys .readouterr ().out == "cli(command-line-argument )\n Foo!\n "
122
+ cli (args = [], prog_name = "test_chain_commands " )
123
+ assert capsys .readouterr ().out == "cli()\n Foo! \n Bar !\n "
98
124
99
125
100
126
def test_exit_repl_function ():
0 commit comments