1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
13 """Bash completion tests
18 from cliff import app as application
19 from cliff import commandmanager
20 from cliff import complete
23 def test_complete_dictionary():
24 sot = complete.CompleteDictionary()
25 sot.add_command("image delete".split(),
26 [mock.Mock(option_strings=["1"])])
27 sot.add_command("image list".split(),
28 [mock.Mock(option_strings=["2"])])
29 sot.add_command("image create".split(),
30 [mock.Mock(option_strings=["3"])])
31 sot.add_command("volume type create".split(),
32 [mock.Mock(option_strings=["4"])])
33 sot.add_command("volume type delete".split(),
34 [mock.Mock(option_strings=["5"])])
35 assert "image volume" == sot.get_commands()
36 result = sot.get_data()
37 assert "image" == result[0][0]
38 assert "create delete list" == result[0][1]
39 assert "image_create" == result[1][0]
40 assert "3" == result[1][1]
41 assert "image_delete" == result[2][0]
42 assert "1" == result[2][1]
43 assert "image_list" == result[3][0]
44 assert "2" == result[3][1]
47 def test_complete_dictionary_subcmd():
48 sot = complete.CompleteDictionary()
49 sot.add_command("image delete".split(),
50 [mock.Mock(option_strings=["1"])])
51 sot.add_command("image list".split(),
52 [mock.Mock(option_strings=["2"])])
53 sot.add_command("image list better".split(),
54 [mock.Mock(option_strings=["3"])])
55 assert "image" == sot.get_commands()
56 result = sot.get_data()
57 assert "image" == result[0][0]
58 assert "delete list list_better" == result[0][1]
59 assert "image_delete" == result[1][0]
60 assert "1" == result[1][1]
61 assert "image_list" == result[2][0]
62 assert "2 better" == result[2][1]
63 assert "image_list_better" == result[3][0]
64 assert "3" == result[3][1]
71 def write(self, text):
72 self.content.append(text)
74 def make_string(self):
76 for line in self.content:
77 result = result + line
81 def given_cmdo_data():
83 data = [("image", "create"),
84 ("image_create", "--eolus"),
85 ("server", "meta ssh"),
86 ("server_meta_delete", "--wilson"),
87 ("server_ssh", "--sunlight")]
91 def then_data(content):
92 assert " cmds='image server'\n" in content
93 assert " cmds_image='create'\n" in content
94 assert " cmds_image_create='--eolus'\n" in content
95 assert " cmds_server='meta ssh'\n" in content
96 assert " cmds_server_meta_delete='--wilson'\n" in content
97 assert " cmds_server_ssh='--sunlight'\n" in content
100 def test_complete_no_code():
101 output = FakeStdout()
102 sot = complete.CompleteNoCode("doesNotMatter", output)
103 sot.write(*given_cmdo_data())
104 then_data(output.content)
107 def test_complete_bash():
108 output = FakeStdout()
109 sot = complete.CompleteBash("openstack", output)
110 sot.write(*given_cmdo_data())
111 then_data(output.content)
112 assert "_openstack()\n" in output.content[0]
113 assert "complete -F _openstack openstack\n" in output.content[-1]
116 def test_complete_command_parser():
117 sot = complete.CompleteCommand(mock.Mock(), mock.Mock())
118 parser = sot.get_parser('nothing')
119 assert "nothing" == parser.prog
120 assert "print bash completion command\n " == parser.description
123 def given_complete_command():
124 cmd_mgr = commandmanager.CommandManager('cliff.tests')
125 app = application.App('testing', '1', cmd_mgr, stdout=FakeStdout())
126 sot = complete.CompleteCommand(app, mock.Mock())
127 cmd_mgr.add_command('complete', complete.CompleteCommand)
128 return sot, app, cmd_mgr
131 def then_actions_equal(actions):
132 optstr = ' '.join(opt for action in actions
133 for opt in action.option_strings)
134 assert '-h --help --name --shell' == optstr
137 def test_complete_command_get_actions():
138 sot, app, cmd_mgr = given_complete_command()
139 app.interactive_mode = False
140 actions = sot.get_actions(["complete"])
141 then_actions_equal(actions)
144 def test_complete_command_get_actions_interactive():
145 sot, app, cmd_mgr = given_complete_command()
146 app.interactive_mode = True
147 actions = sot.get_actions(["complete"])
148 then_actions_equal(actions)
151 def test_complete_command_take_action():
152 sot, app, cmd_mgr = given_complete_command()
153 parsed_args = mock.Mock()
154 parsed_args.name = "test_take"
155 parsed_args.shell = "bash"
156 content = app.stdout.content
157 assert 0 == sot.take_action(parsed_args)
158 assert "_test_take()\n" in content[0]
159 assert "complete -F _test_take test_take\n" in content[-1]
160 assert " cmds='complete help'\n" in content
161 assert " cmds_complete='-h --help --name --shell'\n" in content
162 assert " cmds_help='-h --help'\n" in content
165 def test_complete_command_remove_dashes():
166 sot, app, cmd_mgr = given_complete_command()
167 parsed_args = mock.Mock()
168 parsed_args.name = "test-take"
169 parsed_args.shell = "bash"
170 content = app.stdout.content
171 assert 0 == sot.take_action(parsed_args)
172 assert "_test_take()\n" in content[0]
173 assert "complete -F _test_take test-take\n" in content[-1]