1010# License for the specific language governing permissions and limitations
1111# under the License.
1212
13- import testscenarios
1413from unittest import mock
1514
15+ import testscenarios
16+
1617from cliff import command
1718from cliff import commandmanager
1819from cliff .tests import base
@@ -113,20 +114,23 @@ def test_intersected_commands(self):
113114
114115class TestLoad (base .TestBase ):
115116 def test_load_commands (self ):
116- testcmd = mock .Mock (name = 'testcmd' )
117- testcmd .name .replace .return_value = 'test'
117+ testcmd = mock .Mock ()
118+ testcmd .name = 'test_cmd'
119+ testcmd .module_name = 'module.a'
118120 mock_get_group_all = mock .Mock (return_value = [testcmd ])
119121 with mock .patch (
120122 'stevedore.ExtensionManager' , mock_get_group_all
121123 ) as mock_manager :
122124 mgr = commandmanager .CommandManager ('test' )
123- mock_manager .assert_called_once_with ('test' )
124- names = [n for n , v in mgr ]
125- self .assertEqual (['test' ], names )
125+
126+ mock_manager .assert_called_once_with ('test' )
127+ names = [n for n , v in mgr ]
128+ self .assertEqual (['test cmd' ], names )
126129
127130 def test_load_commands_keep_underscores (self ):
128131 testcmd = mock .Mock ()
129132 testcmd .name = 'test_cmd'
133+ testcmd .module_name = 'module.a'
130134 mock_get_group_all = mock .Mock (return_value = [testcmd ])
131135 with mock .patch (
132136 'stevedore.ExtensionManager' , mock_get_group_all
@@ -135,24 +139,58 @@ def test_load_commands_keep_underscores(self):
135139 'test' ,
136140 convert_underscores = False ,
137141 )
138- mock_manager .assert_called_once_with ('test' )
139- names = [n for n , v in mgr ]
140- self .assertEqual (['test_cmd' ], names )
141142
142- def test_load_commands_replace_underscores (self ):
143- testcmd = mock .Mock ()
144- testcmd .name = 'test_cmd'
145- mock_get_group_all = mock .Mock (return_value = [testcmd ])
143+ mock_manager .assert_called_once_with ('test' )
144+ names = [n for n , v in mgr ]
145+ self .assertEqual (['test_cmd' ], names )
146+
147+ def test_load_commands_ignore_modules (self ):
148+ testcmd_normal = mock .Mock ()
149+ testcmd_normal .name = 'normal_cmd'
150+ testcmd_normal .module_name = 'normal.module'
151+ testcmd_ignored = mock .Mock ()
152+ testcmd_ignored .name = 'ignored_cmd'
153+ testcmd_ignored .module_name = 'ignored.module'
154+ mock_get_group_all = mock .Mock (
155+ return_value = [testcmd_normal , testcmd_ignored ]
156+ )
146157 with mock .patch (
147158 'stevedore.ExtensionManager' , mock_get_group_all
148159 ) as mock_manager :
149160 mgr = commandmanager .CommandManager (
150161 'test' ,
151- convert_underscores = True ,
162+ ignored_modules = [ 'ignored.module' ] ,
152163 )
153- mock_manager .assert_called_once_with ('test' )
154- names = [n for n , v in mgr ]
155- self .assertEqual (['test cmd' ], names )
164+
165+ mock_manager .assert_called_once_with ('test' )
166+ names = [n for n , v in mgr ]
167+ self .assertEqual (['normal cmd' ], names )
168+
169+ def test_load_commands_duplicates (self ):
170+ testcmd_a = mock .Mock ()
171+ testcmd_a .name = 'test_cmd'
172+ testcmd_a .module_name = 'module.a'
173+ testcmd_a .entry_point .value = 'module.a:TestCmd'
174+ testcmd_b = mock .Mock ()
175+ testcmd_b .name = 'test_cmd'
176+ testcmd_b .module_name = 'module.b'
177+ testcmd_b .entry_point .value = 'module.b:TestCmd'
178+ mock_get_group_all = mock .Mock (return_value = [testcmd_a , testcmd_b ])
179+ with mock .patch (
180+ 'stevedore.ExtensionManager' , mock_get_group_all
181+ ) as mock_manager :
182+ mgr = commandmanager .CommandManager ('test' )
183+
184+ mock_manager .assert_called_once_with ('test' )
185+ names = [n for n , v in mgr ]
186+ commands = [v for _ , v in mgr ]
187+ self .assertEqual (['test cmd' ], names )
188+ # we should have ended up with the second command
189+ self .assertEqual ([testcmd_b .entry_point ], commands )
190+ self .assertIn (
191+ "found duplicate implementations of the 'test cmd' command" ,
192+ self .logger .output ,
193+ )
156194
157195
158196class FauxCommand (command .Command ):
@@ -216,6 +254,34 @@ def test(self):
216254 self .assertFalse (remaining )
217255
218256
257+ class TestIsModuleIgnored (base .TestBase ):
258+ def test_match (self ):
259+ result = commandmanager .CommandManager ._is_module_ignored (
260+ 'foo.bar.baz' , ['foo.bar.baz' , 'other.module' ]
261+ )
262+ self .assertTrue (result )
263+ result = commandmanager .CommandManager ._is_module_ignored (
264+ 'foo.bar.baz' , ['foo.bar' , 'other.module' ]
265+ )
266+ self .assertTrue (result )
267+ result = commandmanager .CommandManager ._is_module_ignored (
268+ 'foo.bar.baz' , ['foo' , 'other.module' ]
269+ )
270+ self .assertTrue (result )
271+
272+ def test_no_match (self ):
273+ result = commandmanager .CommandManager ._is_module_ignored (
274+ 'foo.bar.baz' , ['other.module' , 'another.package' ]
275+ )
276+ self .assertFalse (result )
277+
278+ def test_no_ignores (self ):
279+ result = commandmanager .CommandManager ._is_module_ignored (
280+ 'foo.bar.baz' , []
281+ )
282+ self .assertFalse (result )
283+
284+
219285class TestGetByPartialName (base .TestBase ):
220286 def setUp (self ):
221287 super ().setUp ()
@@ -334,8 +400,10 @@ def test_get_command_groups(self):
334400 def test_get_command_names (self ):
335401 mock_cmd_one = mock .Mock ()
336402 mock_cmd_one .name = 'one'
403+ mock_cmd_one .module_name = 'module.a'
337404 mock_cmd_two = mock .Mock ()
338405 mock_cmd_two .name = 'cmd two'
406+ mock_cmd_two .module_name = 'module.b'
339407 mock_get_group_all = mock .Mock (
340408 return_value = [mock_cmd_one , mock_cmd_two ],
341409 )
@@ -345,5 +413,9 @@ def test_get_command_names(self):
345413 ) as mock_manager :
346414 mgr = commandmanager .CommandManager ('test' )
347415 mock_manager .assert_called_once_with ('test' )
416+
348417 cmds = mgr .get_command_names ('test' )
418+ mock_manager .assert_has_calls (
419+ [mock .call ('test' ), mock .call ('test' )]
420+ )
349421 self .assertEqual (['one' , 'cmd two' ], cmds )
0 commit comments