From 6fe90a170d2aa2ad5dab94a52631db2a852261cf Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Jun 22 2017 15:59:28 +0000 Subject: unit tests for cli.load_plugins --- diff --git a/cli/koji b/cli/koji index 0f75927..0a79ca3 100755 --- a/cli/koji +++ b/cli/koji @@ -65,16 +65,14 @@ def register_plugin(plugin): globals()[name] = v -def load_plugins(options): +def load_plugins(options, path): """Load plugins specified by our configuration plus system plugins. Order is that system plugins are first, so they can be overriden by user-specified ones with same name.""" logger = logging.getLogger('koji.plugins') - syspath = '%s/lib/python%s.%s/site-packages/koji_cli_plugins' % \ - (sys.prefix, sys.version_info.major, sys.version_info.minor) - if os.path.exists(syspath): - tracker = koji.plugin.PluginTracker(path=syspath) - for name in sorted(os.listdir(syspath)): + if os.path.exists(path): + tracker = koji.plugin.PluginTracker(path=path) + for name in sorted(os.listdir(path)): if not name.endswith('.py'): continue name = name[:-3] @@ -165,7 +163,9 @@ def get_options(): else: warn("Warning: The pkgurl option is obsolete, please use topurl instead") - load_plugins(options) + plugins_path = '%s/lib/python%s.%s/site-packages/koji_cli_plugins' % \ + (sys.prefix, sys.version_info.major, sys.version_info.minor) + load_plugins(options, plugins_path) if options.help_commands: list_commands() diff --git a/tests/test_cli/data/plugins/not_plugin.omg b/tests/test_cli/data/plugins/not_plugin.omg new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/test_cli/data/plugins/not_plugin.omg diff --git a/tests/test_cli/data/plugins/plugin1.py b/tests/test_cli/data/plugins/plugin1.py new file mode 100644 index 0000000..3e405f5 --- /dev/null +++ b/tests/test_cli/data/plugins/plugin1.py @@ -0,0 +1,19 @@ +from koji.plugin import export_cli, export_as + +@export_as('foobar') +@export_cli +def foo(): + pass + +@export_cli +def foo2(): + pass + +def foo3(): + pass + +foo4 = 'foo4' + +class bar(): + pass + diff --git a/tests/test_cli/data/plugins/plugin2.py b/tests/test_cli/data/plugins/plugin2.py new file mode 100644 index 0000000..58648d6 --- /dev/null +++ b/tests/test_cli/data/plugins/plugin2.py @@ -0,0 +1 @@ +sth = 123 \ No newline at end of file diff --git a/tests/test_cli/test_load_plugins.py b/tests/test_cli/test_load_plugins.py new file mode 100644 index 0000000..411ee96 --- /dev/null +++ b/tests/test_cli/test_load_plugins.py @@ -0,0 +1,20 @@ +from __future__ import absolute_import +import mock +import os +import unittest + +from . import loadcli +cli = loadcli.cli + + +class TestLoadPlugins(unittest.TestCase): + + @mock.patch('logging.getLogger') + def test_load_plugins(self, getLogger): + options = mock.MagicMock() + cli.load_plugins(options, os.path.dirname(__file__) + '/data/plugins') + self.assertTrue(callable(cli.foobar)) + self.assertTrue(callable(cli.foo2)) + self.assertFalse(hasattr(cli, 'foo3')) + self.assertFalse(hasattr(cli, 'foo4')) + self.assertFalse(hasattr(cli, 'sth'))