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
|
|
|
"""
|
|
|
|
|
2015-12-16 09:06:03 -06:00
|
|
|
from ipatests.util import raises, ClassChecker
|
|
|
|
from ipalib import cli, plugable
|
2008-08-08 16:46:23 -05:00
|
|
|
|
2015-04-24 07:39:48 -05:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
pytestmark = pytest.mark.tier0
|
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',
|
2018-06-18 14:04:20 -05:00
|
|
|
'empathetic',
|
2008-11-12 01:46:04 -06:00
|
|
|
'nurse',
|
|
|
|
]
|
2018-06-18 14:04:20 -05:00
|
|
|
assert o.max_col_width(rows) == len('empathetic')
|
2008-11-12 01:46:04 -06:00
|
|
|
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
|
|
|
|
2018-09-26 04:59:50 -05:00
|
|
|
class DummyCommand:
|
2008-08-12 20:52:17 -05:00
|
|
|
def __init__(self, name):
|
|
|
|
self.__name = name
|
|
|
|
|
2022-11-18 09:37:20 -06:00
|
|
|
def __get_name(self):
|
2008-08-12 20:52:17 -05:00
|
|
|
return self.__name
|
|
|
|
name = property(__get_name)
|
|
|
|
|
2008-10-08 00:29:42 -05:00
|
|
|
|
2018-09-26 04:59:50 -05:00
|
|
|
class DummyAPI:
|
2008-08-12 20:52:17 -05:00
|
|
|
def __init__(self, cnt):
|
2016-06-08 07:38:23 -05:00
|
|
|
self.__cmd = plugable.APINameSpace(self.__cmd_iter(cnt), DummyCommand)
|
2008-08-12 20:52:17 -05:00
|
|
|
|
2022-11-18 09:37:20 -06:00
|
|
|
def __get_cmd(self):
|
2008-08-12 20:52:17 -05:00
|
|
|
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
|
|
|
|
"""
|