2008-08-08 16:46:23 -05:00
|
|
|
# Authors:
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2008-08-08 16:46:23 -05:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-08-08 16:46:23 -05:00
|
|
|
|
|
|
|
"""
|
2008-10-07 22:25:23 -05:00
|
|
|
Test the `ipalib.cli` module.
|
2008-08-08 16:46:23 -05:00
|
|
|
"""
|
|
|
|
|
2013-05-21 06:40:27 -05:00
|
|
|
from ipatests.util import raises, get_api, ClassChecker
|
2008-10-27 15:48:02 -05:00
|
|
|
from ipalib import cli, plugable, frontend, backend
|
2008-08-08 16:46:23 -05:00
|
|
|
|
|
|
|
|
2008-11-12 01:46:04 -06:00
|
|
|
class test_textui(ClassChecker):
|
|
|
|
_cls = cli.textui
|
|
|
|
|
|
|
|
def test_max_col_width(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.cli.textui.max_col_width` method.
|
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
|
|
|
o = self.cls(api)
|
2008-11-12 01:46:04 -06:00
|
|
|
e = raises(TypeError, o.max_col_width, 'hello')
|
|
|
|
assert str(e) == 'rows: need %r or %r; got %r' % (list, tuple, 'hello')
|
|
|
|
rows = [
|
|
|
|
'hello',
|
|
|
|
'naughty',
|
|
|
|
'nurse',
|
|
|
|
]
|
|
|
|
assert o.max_col_width(rows) == len('naughty')
|
|
|
|
rows = (
|
|
|
|
( 'a', 'bbb', 'ccccc'),
|
|
|
|
('aa', 'bbbb', 'cccccc'),
|
|
|
|
)
|
|
|
|
assert o.max_col_width(rows, col=0) == 2
|
|
|
|
assert o.max_col_width(rows, col=1) == 4
|
|
|
|
assert o.max_col_width(rows, col=2) == 6
|
|
|
|
|
|
|
|
|
2008-08-08 16:46:23 -05:00
|
|
|
def test_to_cli():
|
2008-08-14 03:28:48 -05:00
|
|
|
"""
|
2008-10-08 00:29:42 -05:00
|
|
|
Test the `ipalib.cli.to_cli` function.
|
2008-08-14 03:28:48 -05:00
|
|
|
"""
|
2008-08-08 16:46:23 -05:00
|
|
|
f = cli.to_cli
|
|
|
|
assert f('initialize') == 'initialize'
|
|
|
|
assert f('user_add') == 'user-add'
|
|
|
|
|
|
|
|
|
|
|
|
def test_from_cli():
|
2008-08-14 03:28:48 -05:00
|
|
|
"""
|
2008-10-08 00:29:42 -05:00
|
|
|
Test the `ipalib.cli.from_cli` function.
|
2008-08-14 03:28:48 -05:00
|
|
|
"""
|
2008-08-08 16:46:23 -05:00
|
|
|
f = cli.from_cli
|
|
|
|
assert f('initialize') == 'initialize'
|
|
|
|
assert f('user-add') == 'user_add'
|
2008-08-11 14:35:57 -05:00
|
|
|
|
|
|
|
|
2008-08-12 20:52:17 -05:00
|
|
|
def get_cmd_name(i):
|
|
|
|
return 'cmd_%d' % i
|
|
|
|
|
2008-10-08 00:29:42 -05:00
|
|
|
|
2008-08-15 14:49:04 -05:00
|
|
|
class DummyCommand(object):
|
2008-08-12 20:52:17 -05:00
|
|
|
def __init__(self, name):
|
|
|
|
self.__name = name
|
|
|
|
|
|
|
|
def __get_name(self):
|
|
|
|
return self.__name
|
|
|
|
name = property(__get_name)
|
|
|
|
|
2008-10-08 00:29:42 -05:00
|
|
|
|
2008-08-12 20:52:17 -05:00
|
|
|
class DummyAPI(object):
|
|
|
|
def __init__(self, cnt):
|
2008-08-14 00:46:20 -05:00
|
|
|
self.__cmd = plugable.NameSpace(self.__cmd_iter(cnt))
|
2008-08-12 20:52:17 -05:00
|
|
|
|
|
|
|
def __get_cmd(self):
|
|
|
|
return self.__cmd
|
2008-08-15 14:49:04 -05:00
|
|
|
Command = property(__get_cmd)
|
2008-08-12 20:52:17 -05:00
|
|
|
|
|
|
|
def __cmd_iter(self, cnt):
|
2015-08-12 08:23:56 -05:00
|
|
|
for i in range(cnt):
|
2008-08-15 14:49:04 -05:00
|
|
|
yield DummyCommand(get_cmd_name(i))
|
2008-08-12 20:52:17 -05:00
|
|
|
|
|
|
|
def finalize(self):
|
|
|
|
pass
|
|
|
|
|
2008-08-12 23:10:23 -05:00
|
|
|
def register(self, *args, **kw):
|
|
|
|
pass
|
|
|
|
|
2008-08-12 20:52:17 -05:00
|
|
|
|
2008-10-27 17:08:17 -05:00
|
|
|
config_cli = """
|
|
|
|
[global]
|
|
|
|
|
|
|
|
from_cli_conf = set in cli.conf
|
|
|
|
"""
|
|
|
|
|
|
|
|
config_default = """
|
|
|
|
[global]
|
|
|
|
|
|
|
|
from_default_conf = set in default.conf
|
|
|
|
|
|
|
|
# Make sure cli.conf is loaded first:
|
|
|
|
from_cli_conf = overridden in default.conf
|
|
|
|
"""
|