From 7721443a625b2efd0744ad347c62795e5ba6bb91 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 20:41:15 -0600 Subject: Moved ipalib/tests/ into tests/test_ipalib/ --- tests/test_ipalib/test_cli.py | 138 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 tests/test_ipalib/test_cli.py (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py new file mode 100644 index 00000000..90c66d41 --- /dev/null +++ b/tests/test_ipalib/test_cli.py @@ -0,0 +1,138 @@ +# Authors: +# Jason Gerard DeRose +# +# Copyright (C) 2008 Red Hat +# see file 'COPYING' for use and warranty information +# +# 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; version 2 only +# +# 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 +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +""" +Unit tests for `ipalib.cli` module. +""" + +from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker +from ipalib import cli, plugable + + +def test_to_cli(): + """ + Tests the `cli.to_cli` function. + """ + f = cli.to_cli + assert f('initialize') == 'initialize' + assert f('user_add') == 'user-add' + + +def test_from_cli(): + """ + Tests the `cli.from_cli` function. + """ + f = cli.from_cli + assert f('initialize') == 'initialize' + assert f('user-add') == 'user_add' + + +def get_cmd_name(i): + return 'cmd_%d' % i + +class DummyCommand(object): + def __init__(self, name): + self.__name = name + + def __get_name(self): + return self.__name + name = property(__get_name) + +class DummyAPI(object): + def __init__(self, cnt): + self.__cmd = plugable.NameSpace(self.__cmd_iter(cnt)) + + def __get_cmd(self): + return self.__cmd + Command = property(__get_cmd) + + def __cmd_iter(self, cnt): + for i in xrange(cnt): + yield DummyCommand(get_cmd_name(i)) + + def finalize(self): + pass + + def register(self, *args, **kw): + pass + + + + + +class test_CLI(ClassChecker): + """ + Tests the `cli.CLI` class. + """ + _cls = cli.CLI + + def test_class(self): + assert type(self.cls.api) is property + + def test_api(self): + """ + Tests the `cli.CLI.api` property. + """ + api = 'the plugable.API instance' + o = self.cls(api) + assert read_only(o, 'api') is api + + def dont_parse(self): + """ + Tests the `cli.CLI.parse` method. + """ + o = self.cls(None) + args = ['hello', 'naughty', 'nurse'] + kw = dict( + first_name='Naughty', + last_name='Nurse', + ) + opts = ['--%s=%s' % (k.replace('_', '-'), v) for (k, v) in kw.items()] + assert o.parse(args + []) == (args, {}) + assert o.parse(opts + []) == ([], kw) + assert o.parse(args + opts) == (args, kw) + assert o.parse(opts + args) == (args, kw) + + def test_mcl(self): + """ + Tests the `cli.CLI.mcl` (Max Command Length) property . + """ + cnt = 100 + api = DummyAPI(cnt) + len(api.Command) == cnt + o = self.cls(api) + assert o.mcl is None + o.build_map() + assert o.mcl == 6 # len('cmd_99') + + def test_dict(self): + """ + Tests the `cli.CLI.__contains__` and `cli.CLI.__getitem__` methods. + """ + cnt = 25 + api = DummyAPI(cnt) + assert len(api.Command) == cnt + o = self.cls(api) + o.build_map() + for cmd in api.Command(): + key = cli.to_cli(cmd.name) + assert key in o + assert o[key] is cmd + assert cmd.name not in o + raises(KeyError, getitem, o, cmd.name) -- cgit From af56c71d506c2573f99a1ca8334cfa90dc7f74d7 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 21:25:23 -0600 Subject: Cleaned up package and module level docstrings for everything in tests/ --- tests/test_ipalib/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 90c66d41..4095af6d 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -18,7 +18,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ -Unit tests for `ipalib.cli` module. +Test the `ipalib.cli` module. """ from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker -- cgit From f6ac2df6bd7ceddd0f3eb968198cc3ebd1388087 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 21:59:47 -0600 Subject: Moved tstutil.py into base of tests so it can be used by all test subpackages more easily --- tests/test_ipalib/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 4095af6d..c7b37126 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -21,7 +21,7 @@ Test the `ipalib.cli` module. """ -from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker +from tests.tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker from ipalib import cli, plugable -- cgit From deb8e3dfc899cfc7ee31f47c1ba7c4301c58fc51 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 22:30:53 -0600 Subject: Renamed tests/tstutil.py to tests/util.py --- tests/test_ipalib/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index c7b37126..33e4d220 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -21,7 +21,7 @@ Test the `ipalib.cli` module. """ -from tests.tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker +from tests.util import raises, getitem, no_set, no_del, read_only, ClassChecker from ipalib import cli, plugable -- cgit From 9bff91fc0871cc9aa02e1fd63776a303185f3b3c Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 23:29:42 -0600 Subject: PEP 257: cleaned up docstrings in test_cli.py --- tests/test_ipalib/test_cli.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 33e4d220..50bfb932 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -27,7 +27,7 @@ from ipalib import cli, plugable def test_to_cli(): """ - Tests the `cli.to_cli` function. + Test the `ipalib.cli.to_cli` function. """ f = cli.to_cli assert f('initialize') == 'initialize' @@ -36,7 +36,7 @@ def test_to_cli(): def test_from_cli(): """ - Tests the `cli.from_cli` function. + Test the `ipalib.cli.from_cli` function. """ f = cli.from_cli assert f('initialize') == 'initialize' @@ -46,6 +46,7 @@ def test_from_cli(): def get_cmd_name(i): return 'cmd_%d' % i + class DummyCommand(object): def __init__(self, name): self.__name = name @@ -54,6 +55,7 @@ class DummyCommand(object): return self.__name name = property(__get_name) + class DummyAPI(object): def __init__(self, cnt): self.__cmd = plugable.NameSpace(self.__cmd_iter(cnt)) @@ -73,21 +75,21 @@ class DummyAPI(object): pass - - - class test_CLI(ClassChecker): """ - Tests the `cli.CLI` class. + Test the `ipalib.cli.CLI` class. """ _cls = cli.CLI def test_class(self): + """ + Test the `ipalib.cli.CLI` class. + """ assert type(self.cls.api) is property def test_api(self): """ - Tests the `cli.CLI.api` property. + Test the `ipalib.cli.CLI.api` property. """ api = 'the plugable.API instance' o = self.cls(api) @@ -95,7 +97,7 @@ class test_CLI(ClassChecker): def dont_parse(self): """ - Tests the `cli.CLI.parse` method. + Test the `ipalib.cli.CLI.parse` method. """ o = self.cls(None) args = ['hello', 'naughty', 'nurse'] @@ -111,7 +113,7 @@ class test_CLI(ClassChecker): def test_mcl(self): """ - Tests the `cli.CLI.mcl` (Max Command Length) property . + Test the `ipalib.cli.CLI.mcl` property . """ cnt = 100 api = DummyAPI(cnt) @@ -123,7 +125,7 @@ class test_CLI(ClassChecker): def test_dict(self): """ - Tests the `cli.CLI.__contains__` and `cli.CLI.__getitem__` methods. + Test container emulation of `ipalib.cli.CLI` class. """ cnt = 25 api = DummyAPI(cnt) -- cgit From 10026284dbf8f1b8a6eedf3b1c6ce05da568b4fa Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 27 Oct 2008 14:48:02 -0600 Subject: Started cleanup work on CLI class, added unit tests for CLI.parse_globals() --- tests/test_ipalib/test_cli.py | 105 +++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 52 deletions(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 50bfb932..10f23989 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -22,7 +22,7 @@ Test the `ipalib.cli` module. """ from tests.util import raises, getitem, no_set, no_del, read_only, ClassChecker -from ipalib import cli, plugable +from ipalib import cli, plugable, frontend, backend def test_to_cli(): @@ -81,60 +81,61 @@ class test_CLI(ClassChecker): """ _cls = cli.CLI - def test_class(self): - """ - Test the `ipalib.cli.CLI` class. - """ - assert type(self.cls.api) is property - - def test_api(self): - """ - Test the `ipalib.cli.CLI.api` property. - """ - api = 'the plugable.API instance' - o = self.cls(api) - assert read_only(o, 'api') is api - - def dont_parse(self): - """ - Test the `ipalib.cli.CLI.parse` method. - """ - o = self.cls(None) - args = ['hello', 'naughty', 'nurse'] - kw = dict( - first_name='Naughty', - last_name='Nurse', + def new(self, argv): + api = plugable.API( + frontend.Command, + frontend.Object, + frontend.Method, + frontend.Property, + frontend.Application, + backend.Backend, ) - opts = ['--%s=%s' % (k.replace('_', '-'), v) for (k, v) in kw.items()] - assert o.parse(args + []) == (args, {}) - assert o.parse(opts + []) == ([], kw) - assert o.parse(args + opts) == (args, kw) - assert o.parse(opts + args) == (args, kw) + o = self.cls(api, argv) + assert o.api is api + return o - def test_mcl(self): + def test_init(self): """ - Test the `ipalib.cli.CLI.mcl` property . + Test the `ipalib.cli.CLI.__init__` method. """ - cnt = 100 - api = DummyAPI(cnt) - len(api.Command) == cnt - o = self.cls(api) - assert o.mcl is None - o.build_map() - assert o.mcl == 6 # len('cmd_99') - - def test_dict(self): + argv = ['-v', 'user-add', '--first=Jonh', '--last=Doe'] + o = self.new(argv) + assert type(o.api) is plugable.API + assert o.argv == tuple(argv) + + def test_parse_globals(self): """ - Test container emulation of `ipalib.cli.CLI` class. + Test the `ipalib.cli.CLI.parse_globals` method. """ - cnt = 25 - api = DummyAPI(cnt) - assert len(api.Command) == cnt - o = self.cls(api) - o.build_map() - for cmd in api.Command(): - key = cli.to_cli(cmd.name) - assert key in o - assert o[key] is cmd - assert cmd.name not in o - raises(KeyError, getitem, o, cmd.name) + # Test with empty argv + o = self.new([]) + assert not hasattr(o, 'options') + assert not hasattr(o, 'cmd_argv') + assert o.isdone('parse_globals') is False + o.parse_globals() + assert o.isdone('parse_globals') is True + assert o.options.interactive is True + assert o.options.verbose is False + assert o.options.config_file is None + assert o.options.environment is None + assert o.cmd_argv == tuple() + e = raises(StandardError, o.parse_globals) + assert str(e) == 'CLI.parse_globals() already called' + + # Test with a populated argv + argv = ('-a', '-n', '-v', '-c', '/my/config.conf', '-e', 'my_key=my_val') + cmd_argv = ('user-add', '--first', 'John', '--last', 'Doe') + o = self.new(argv + cmd_argv) + assert not hasattr(o, 'options') + assert not hasattr(o, 'cmd_argv') + assert o.isdone('parse_globals') is False + o.parse_globals() + assert o.isdone('parse_globals') is True + assert o.options.prompt_all is True + assert o.options.interactive is False + assert o.options.verbose is True + assert o.options.config_file == '/my/config.conf' + assert o.options.environment == 'my_key=my_val' + assert o.cmd_argv == cmd_argv + e = raises(StandardError, o.parse_globals) + assert str(e) == 'CLI.parse_globals() already called' -- cgit From 17fd9cc4315f171a8d9e9d189936eea8ba2af0c0 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 27 Oct 2008 14:49:34 -0600 Subject: Started cleanup work on CLI class, added unit tests for CLI.parse_globals() --- tests/test_ipalib/test_cli.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 10f23989..28e441e7 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -139,3 +139,8 @@ class test_CLI(ClassChecker): assert o.cmd_argv == cmd_argv e = raises(StandardError, o.parse_globals) assert str(e) == 'CLI.parse_globals() already called' + + def test_bootstrap(self): + """ + Test the `ipalib.cli.CLI.bootstrap` method. + """ -- cgit From e6254026fe73c423d357a2fa1489de35475da46c Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 27 Oct 2008 15:19:49 -0600 Subject: Implemented basic CLI.bootstrap(); added corresponding unit tests --- tests/test_ipalib/test_cli.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 28e441e7..293371b7 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -22,6 +22,7 @@ Test the `ipalib.cli` module. """ from tests.util import raises, getitem, no_set, no_del, read_only, ClassChecker +from tests.util import TempHome from ipalib import cli, plugable, frontend, backend @@ -81,7 +82,8 @@ class test_CLI(ClassChecker): """ _cls = cli.CLI - def new(self, argv): + def new(self, argv=tuple()): + home = TempHome() api = plugable.API( frontend.Command, frontend.Object, @@ -90,17 +92,18 @@ class test_CLI(ClassChecker): frontend.Application, backend.Backend, ) + api.env.in_tree = True o = self.cls(api, argv) assert o.api is api - return o + return (o, api, home) def test_init(self): """ Test the `ipalib.cli.CLI.__init__` method. """ argv = ['-v', 'user-add', '--first=Jonh', '--last=Doe'] - o = self.new(argv) - assert type(o.api) is plugable.API + (o, api, home) = self.new(argv) + assert o.api is api assert o.argv == tuple(argv) def test_parse_globals(self): @@ -108,7 +111,7 @@ class test_CLI(ClassChecker): Test the `ipalib.cli.CLI.parse_globals` method. """ # Test with empty argv - o = self.new([]) + (o, api, home) = self.new() assert not hasattr(o, 'options') assert not hasattr(o, 'cmd_argv') assert o.isdone('parse_globals') is False @@ -125,7 +128,7 @@ class test_CLI(ClassChecker): # Test with a populated argv argv = ('-a', '-n', '-v', '-c', '/my/config.conf', '-e', 'my_key=my_val') cmd_argv = ('user-add', '--first', 'John', '--last', 'Doe') - o = self.new(argv + cmd_argv) + (o, api, home) = self.new(argv + cmd_argv) assert not hasattr(o, 'options') assert not hasattr(o, 'cmd_argv') assert o.isdone('parse_globals') is False @@ -144,3 +147,14 @@ class test_CLI(ClassChecker): """ Test the `ipalib.cli.CLI.bootstrap` method. """ + (o, api, home) = self.new() + keys = tuple(api.env) + assert o.isdone('parse_globals') is False + assert o.isdone('bootstrap') is False + o.bootstrap() + assert o.isdone('parse_globals') is True + assert o.isdone('bootstrap') is True + e = raises(StandardError, o.bootstrap) + assert str(e) == 'CLI.bootstrap() already called' + assert api.env.verbose is False + assert api.env.context == 'cli' -- cgit From bb9691099b7b025fc491279314d8803f4fa3b571 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 27 Oct 2008 15:36:41 -0600 Subject: API.bootstrap() now calls Env._finalize_core(); updated unit tests --- tests/test_ipalib/test_cli.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 293371b7..39959270 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -149,9 +149,11 @@ class test_CLI(ClassChecker): """ (o, api, home) = self.new() keys = tuple(api.env) + assert api.isdone('bootstrap') is False assert o.isdone('parse_globals') is False assert o.isdone('bootstrap') is False o.bootstrap() + assert api.isdone('bootstrap') is True assert o.isdone('parse_globals') is True assert o.isdone('bootstrap') is True e = raises(StandardError, o.bootstrap) -- cgit From 491e295412fceead7cf0c6c9cdd44904541e4044 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 27 Oct 2008 16:08:17 -0600 Subject: Unit test for CLI.boostrap() now checks that -e overrides and values from config files are merged in correctly --- tests/test_ipalib/test_cli.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 39959270..389bb52c 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -76,6 +76,24 @@ class DummyAPI(object): pass +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 +""" + + + + class test_CLI(ClassChecker): """ Test the `ipalib.cli.CLI` class. @@ -147,6 +165,7 @@ class test_CLI(ClassChecker): """ Test the `ipalib.cli.CLI.bootstrap` method. """ + # Test with empty argv (o, api, home) = self.new() keys = tuple(api.env) assert api.isdone('bootstrap') is False @@ -160,3 +179,25 @@ class test_CLI(ClassChecker): assert str(e) == 'CLI.bootstrap() already called' assert api.env.verbose is False assert api.env.context == 'cli' + keys = tuple(api.env) + added = ( + 'my_key', + 'whatever', + 'from_default_conf', + 'from_cli_conf' + ) + for key in added: + assert key not in api.env + assert key not in keys + + # Test with a populated argv + argv = ['-e', 'my_key=my_val,whatever=Hello'] + (o, api, home) = self.new(argv) + home.write(config_default, '.ipa', 'default.conf') + home.write(config_cli, '.ipa', 'cli.conf') + o.bootstrap() + assert api.env.my_key == 'my_val' + assert api.env.whatever == 'Hello' + assert api.env.from_default_conf == 'set in default.conf' + assert api.env.from_cli_conf == 'set in cli.conf' + assert list(api.env) == sorted(keys + added) -- cgit From 9b1e3f59465c6ba33f4266bc3add469b5e1711eb Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 27 Oct 2008 19:21:49 -0600 Subject: More docstrings, functionality, and unit tests for improved CLI class --- tests/test_ipalib/test_cli.py | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 389bb52c..7bcbfb0c 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -115,6 +115,17 @@ class test_CLI(ClassChecker): assert o.api is api return (o, api, home) + def check_cascade(self, *names): + (o, api, home) = self.new() + method = getattr(o, names[0]) + for name in names: + assert o.isdone(name) is False + method() + for name in names: + assert o.isdone(name) is True + e = raises(StandardError, method) + assert str(e) == 'CLI.%s() already called' % names[0] + def test_init(self): """ Test the `ipalib.cli.CLI.__init__` method. @@ -201,3 +212,36 @@ class test_CLI(ClassChecker): assert api.env.from_default_conf == 'set in default.conf' assert api.env.from_cli_conf == 'set in cli.conf' assert list(api.env) == sorted(keys + added) + + def test_load_plugins(self): + """ + Test the `ipalib.cli.CLI.load_plugins` method. + """ + self.check_cascade( + 'load_plugins', + 'bootstrap', + 'parse_globals' + ) + (o, api, home) = self.new() + assert api.isdone('load_plugins') is False + o.load_plugins() + assert api.isdone('load_plugins') is True + + def test_finalize(self): + """ + Test the `ipalib.cli.CLI.finalize` method. + """ + self.check_cascade( + 'finalize', + 'load_plugins', + 'bootstrap', + 'parse_globals' + ) + + (o, api, home) = self.new() + assert api.isdone('finalize') is False + assert 'Command' not in api + o.finalize() + assert api.isdone('finalize') is True + assert list(api.Command) == \ + sorted(k.__name__ for k in cli.cli_application_commands) -- cgit From 6e456cc7494bc00e905361f3e6d42dff99089c6b Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 27 Oct 2008 23:30:55 -0600 Subject: More CLI cleanup, got all basics working again --- tests/test_ipalib/test_cli.py | 141 +++++++++++++++++++++++------------------- 1 file changed, 77 insertions(+), 64 deletions(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 7bcbfb0c..c4740875 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -110,6 +110,7 @@ class test_CLI(ClassChecker): frontend.Application, backend.Backend, ) + api.env.mode = 'unit-test' api.env.in_tree = True o = self.cls(api, argv) assert o.api is api @@ -135,57 +136,65 @@ class test_CLI(ClassChecker): assert o.api is api assert o.argv == tuple(argv) - def test_parse_globals(self): + def test_run(self): """ - Test the `ipalib.cli.CLI.parse_globals` method. + Test the `ipalib.cli.CLI.run` method. """ - # Test with empty argv + self.check_cascade( + 'run', + 'finalize', + 'load_plugins', + 'bootstrap', + 'parse_globals' + ) + + def test_finalize(self): + """ + Test the `ipalib.cli.CLI.finalize` method. + """ + self.check_cascade( + 'finalize', + 'load_plugins', + 'bootstrap', + 'parse_globals' + ) + (o, api, home) = self.new() - assert not hasattr(o, 'options') - assert not hasattr(o, 'cmd_argv') - assert o.isdone('parse_globals') is False - o.parse_globals() - assert o.isdone('parse_globals') is True - assert o.options.interactive is True - assert o.options.verbose is False - assert o.options.config_file is None - assert o.options.environment is None - assert o.cmd_argv == tuple() - e = raises(StandardError, o.parse_globals) - assert str(e) == 'CLI.parse_globals() already called' + assert api.isdone('finalize') is False + assert 'Command' not in api + o.finalize() + assert api.isdone('finalize') is True + assert list(api.Command) == \ + sorted(k.__name__ for k in cli.cli_application_commands) - # Test with a populated argv - argv = ('-a', '-n', '-v', '-c', '/my/config.conf', '-e', 'my_key=my_val') - cmd_argv = ('user-add', '--first', 'John', '--last', 'Doe') - (o, api, home) = self.new(argv + cmd_argv) - assert not hasattr(o, 'options') - assert not hasattr(o, 'cmd_argv') - assert o.isdone('parse_globals') is False - o.parse_globals() - assert o.isdone('parse_globals') is True - assert o.options.prompt_all is True - assert o.options.interactive is False - assert o.options.verbose is True - assert o.options.config_file == '/my/config.conf' - assert o.options.environment == 'my_key=my_val' - assert o.cmd_argv == cmd_argv - e = raises(StandardError, o.parse_globals) - assert str(e) == 'CLI.parse_globals() already called' + def test_load_plugins(self): + """ + Test the `ipalib.cli.CLI.load_plugins` method. + """ + self.check_cascade( + 'load_plugins', + 'bootstrap', + 'parse_globals' + ) + (o, api, home) = self.new() + assert api.isdone('load_plugins') is False + o.load_plugins() + assert api.isdone('load_plugins') is True def test_bootstrap(self): """ Test the `ipalib.cli.CLI.bootstrap` method. """ + self.check_cascade( + 'bootstrap', + 'parse_globals' + ) # Test with empty argv (o, api, home) = self.new() keys = tuple(api.env) assert api.isdone('bootstrap') is False - assert o.isdone('parse_globals') is False - assert o.isdone('bootstrap') is False o.bootstrap() assert api.isdone('bootstrap') is True - assert o.isdone('parse_globals') is True - assert o.isdone('bootstrap') is True e = raises(StandardError, o.bootstrap) assert str(e) == 'CLI.bootstrap() already called' assert api.env.verbose is False @@ -213,35 +222,39 @@ class test_CLI(ClassChecker): assert api.env.from_cli_conf == 'set in cli.conf' assert list(api.env) == sorted(keys + added) - def test_load_plugins(self): + def test_parse_globals(self): """ - Test the `ipalib.cli.CLI.load_plugins` method. + Test the `ipalib.cli.CLI.parse_globals` method. """ - self.check_cascade( - 'load_plugins', - 'bootstrap', - 'parse_globals' - ) + # Test with empty argv (o, api, home) = self.new() - assert api.isdone('load_plugins') is False - o.load_plugins() - assert api.isdone('load_plugins') is True - - def test_finalize(self): - """ - Test the `ipalib.cli.CLI.finalize` method. - """ - self.check_cascade( - 'finalize', - 'load_plugins', - 'bootstrap', - 'parse_globals' - ) + assert not hasattr(o, 'options') + assert not hasattr(o, 'cmd_argv') + assert o.isdone('parse_globals') is False + o.parse_globals() + assert o.isdone('parse_globals') is True + assert o.options.interactive is True + assert o.options.verbose is False + assert o.options.config_file is None + assert o.options.environment is None + assert o.cmd_argv == tuple() + e = raises(StandardError, o.parse_globals) + assert str(e) == 'CLI.parse_globals() already called' - (o, api, home) = self.new() - assert api.isdone('finalize') is False - assert 'Command' not in api - o.finalize() - assert api.isdone('finalize') is True - assert list(api.Command) == \ - sorted(k.__name__ for k in cli.cli_application_commands) + # Test with a populated argv + argv = ('-a', '-n', '-v', '-c', '/my/config.conf', '-e', 'my_key=my_val') + cmd_argv = ('user-add', '--first', 'John', '--last', 'Doe') + (o, api, home) = self.new(argv + cmd_argv) + assert not hasattr(o, 'options') + assert not hasattr(o, 'cmd_argv') + assert o.isdone('parse_globals') is False + o.parse_globals() + assert o.isdone('parse_globals') is True + assert o.options.prompt_all is True + assert o.options.interactive is False + assert o.options.verbose is True + assert o.options.config_file == '/my/config.conf' + assert o.options.environment == 'my_key=my_val' + assert o.cmd_argv == cmd_argv + e = raises(StandardError, o.parse_globals) + assert str(e) == 'CLI.parse_globals() already called' -- cgit From 83d6c95e4636049a5bcedb533ad49f6e2cf79dfe Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 27 Oct 2008 23:39:43 -0600 Subject: API.load_plugins() no longer takes dry_run=False kwarg and instead checks in env.mode == 'unit_test' to decide whether to load the plugins; it also only loads ipa_server.plugins in env.in_server is True --- tests/test_ipalib/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index c4740875..7b3239d7 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -110,7 +110,7 @@ class test_CLI(ClassChecker): frontend.Application, backend.Backend, ) - api.env.mode = 'unit-test' + api.env.mode = 'unit_test' api.env.in_tree = True o = self.cls(api, argv) assert o.api is api -- cgit From 2fee6a3e20169f12b837647f0f71d6f28937490f Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 30 Oct 2008 01:34:46 -0600 Subject: Added tests.util.get_api() function to create a standard (api, home) tuple for unit testing --- tests/test_ipalib/test_cli.py | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 7b3239d7..f13db51e 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -21,8 +21,7 @@ Test the `ipalib.cli` module. """ -from tests.util import raises, getitem, no_set, no_del, read_only, ClassChecker -from tests.util import TempHome +from tests.util import raises, get_api, ClassChecker from ipalib import cli, plugable, frontend, backend @@ -92,8 +91,6 @@ from_cli_conf = overridden in default.conf """ - - class test_CLI(ClassChecker): """ Test the `ipalib.cli.CLI` class. @@ -101,17 +98,7 @@ class test_CLI(ClassChecker): _cls = cli.CLI def new(self, argv=tuple()): - home = TempHome() - api = plugable.API( - frontend.Command, - frontend.Object, - frontend.Method, - frontend.Property, - frontend.Application, - backend.Backend, - ) - api.env.mode = 'unit_test' - api.env.in_tree = True + (api, home) = get_api() o = self.cls(api, argv) assert o.api is api return (o, api, home) -- cgit From a23d41a57f43c3a0f298d3918ae1712181fa544e Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 31 Oct 2008 18:17:08 -0600 Subject: Reoganized global option functionality to it is easy for any script to use the environment-related global options; lite-xmlrpc.py now uses same global options --- tests/test_ipalib/test_cli.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index f13db51e..56545942 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -189,7 +189,6 @@ class test_CLI(ClassChecker): keys = tuple(api.env) added = ( 'my_key', - 'whatever', 'from_default_conf', 'from_cli_conf' ) @@ -203,8 +202,7 @@ class test_CLI(ClassChecker): home.write(config_default, '.ipa', 'default.conf') home.write(config_cli, '.ipa', 'cli.conf') o.bootstrap() - assert api.env.my_key == 'my_val' - assert api.env.whatever == 'Hello' + assert api.env.my_key == 'my_val,whatever=Hello' assert api.env.from_default_conf == 'set in default.conf' assert api.env.from_cli_conf == 'set in cli.conf' assert list(api.env) == sorted(keys + added) @@ -213,22 +211,23 @@ class test_CLI(ClassChecker): """ Test the `ipalib.cli.CLI.parse_globals` method. """ - # Test with empty argv + # Test with empty argv: (o, api, home) = self.new() assert not hasattr(o, 'options') assert not hasattr(o, 'cmd_argv') assert o.isdone('parse_globals') is False o.parse_globals() assert o.isdone('parse_globals') is True + assert o.options.prompt_all is False assert o.options.interactive is True - assert o.options.verbose is False - assert o.options.config_file is None - assert o.options.environment is None + assert o.options.verbose is None + assert o.options.conf is None + assert o.options.env is None assert o.cmd_argv == tuple() e = raises(StandardError, o.parse_globals) assert str(e) == 'CLI.parse_globals() already called' - # Test with a populated argv + # Test with a populated argv: argv = ('-a', '-n', '-v', '-c', '/my/config.conf', '-e', 'my_key=my_val') cmd_argv = ('user-add', '--first', 'John', '--last', 'Doe') (o, api, home) = self.new(argv + cmd_argv) @@ -240,8 +239,14 @@ class test_CLI(ClassChecker): assert o.options.prompt_all is True assert o.options.interactive is False assert o.options.verbose is True - assert o.options.config_file == '/my/config.conf' - assert o.options.environment == 'my_key=my_val' + assert o.options.conf == '/my/config.conf' + assert o.options.env == ['my_key=my_val'] assert o.cmd_argv == cmd_argv e = raises(StandardError, o.parse_globals) assert str(e) == 'CLI.parse_globals() already called' + + # Test with multiple -e args: + argv = ('-e', 'key1=val1', '-e', 'key2=val2') + (o, api, home) = self.new(argv) + o.parse_globals() + assert o.options.env == ['key1=val1', 'key2=val2'] -- cgit From 014af24731ff39520a9635694ed99dc9d09669c9 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Wed, 12 Nov 2008 00:46:04 -0700 Subject: Changed calling signature of output_for_cli(); started work on 'textui' backend plugin --- tests/test_ipalib/test_cli.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 56545942..8cedd088 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -25,6 +25,31 @@ from tests.util import raises, get_api, ClassChecker from ipalib import cli, plugable, frontend, backend +class test_textui(ClassChecker): + _cls = cli.textui + + def test_max_col_width(self): + """ + Test the `ipalib.cli.textui.max_col_width` method. + """ + o = self.cls() + 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 + + def test_to_cli(): """ Test the `ipalib.cli.to_cli` function. -- cgit From f5594dd489317dc406d20f897fc720e0cf89c9d2 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 13 Nov 2008 23:29:35 -0700 Subject: Started work on cleaning up how exceptions are caught and sys.exit() is called in ipalib.cli.CLI --- tests/test_ipalib/test_cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/test_ipalib/test_cli.py') diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 8cedd088..56297fdf 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -148,12 +148,12 @@ class test_CLI(ClassChecker): assert o.api is api assert o.argv == tuple(argv) - def test_run(self): + def test_run_real(self): """ - Test the `ipalib.cli.CLI.run` method. + Test the `ipalib.cli.CLI.run_real` method. """ self.check_cascade( - 'run', + 'run_real', 'finalize', 'load_plugins', 'bootstrap', -- cgit