From 5672916cb24a44024ea4ceba7a3998c0a71f3c81 Mon Sep 17 00:00:00 2001 From: Jakub Warmuz Date: Sat, 2 May 2015 11:19:46 +0000 Subject: [PATCH] Tests and lint for plugins.common --- letsencrypt/client/plugins/common.py | 16 ++++-- letsencrypt/client/plugins/common_test.py | 61 +++++++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 letsencrypt/client/plugins/common_test.py diff --git a/letsencrypt/client/plugins/common.py b/letsencrypt/client/plugins/common.py index ba6efeccc..e9ee4d573 100644 --- a/letsencrypt/client/plugins/common.py +++ b/letsencrypt/client/plugins/common.py @@ -8,11 +8,11 @@ from letsencrypt.client import interfaces def option_namespace(name): """ArgumentParser options namespace (prefix of all options).""" - return name + '-' + return name + "-" def dest_namespace(name): """ArgumentParser dest namespace (prefix of all destinations).""" - return name + '_' + return name + "_" class Plugin(object): @@ -26,17 +26,19 @@ class Plugin(object): @property def option_namespace(self): + """ArgumentParser options namespace (prefix of all options).""" return option_namespace(self.name) @property def dest_namespace(self): + """ArgumentParser dest namespace (prefix of all destinations).""" return dest_namespace(self.name) def dest(self, var): """Find a destination for given variable ``var``.""" # this should do exactly the same what ArgumentParser(arg), # does to "arg" to compute "dest" - return self.dest_namespace + var.replace('-', '_') + return self.dest_namespace + var.replace("-", "_") def conf(self, var): """Find a configuration value for variable ``var``.""" @@ -44,12 +46,18 @@ class Plugin(object): @classmethod def inject_parser_options(cls, parser, name): + """Inject parser options. + + See `~.IPlugin.inject_parser_options` for docs. + + """ # dummy function, doesn't check if dest.startswith(self.dest_namespace) def add(arg_name_no_prefix, *args, **kwargs): + # pylint: disable=missing-docstring return parser.add_argument( "--{0}{1}".format(option_namespace(name), arg_name_no_prefix), *args, **kwargs) - cls.add_parser_arguments(add) + return cls.add_parser_arguments(add) @jose_util.abstractclassmethod def add_parser_arguments(cls, add): diff --git a/letsencrypt/client/plugins/common_test.py b/letsencrypt/client/plugins/common_test.py new file mode 100644 index 000000000..bdb5f7f3c --- /dev/null +++ b/letsencrypt/client/plugins/common_test.py @@ -0,0 +1,61 @@ +"""Tests for letsencrypt.client.plugins.common.""" +import unittest + +import mock + + +class NamespaceFunctionsTest(unittest.TestCase): + """Tests for letsencrypt.client.plugins.common.*_namespace functions.""" + + def test_option_namespace(self): + from letsencrypt.client.plugins.common import option_namespace + self.assertEqual("foo-", option_namespace("foo")) + + def test_dest_namespace(self): + from letsencrypt.client.plugins.common import dest_namespace + self.assertEqual("foo_", dest_namespace("foo")) + + +class PluginTest(unittest.TestCase): + """Test for letsencrypt.client.plugins.common.Plugin.""" + + def setUp(self): + from letsencrypt.client.plugins.common import Plugin + + class MockPlugin(Plugin): # pylint: disable=missing-docstring + @classmethod + def add_parser_arguments(cls, add): + add("foo-bar", dest="different_to_foo_bar", x=1, y=None) + + self.plugin_cls = MockPlugin + self.config = mock.MagicMock() + self.plugin = MockPlugin(config=self.config, name="mock") + + def test_init(self): + self.assertEqual("mock", self.plugin.name) + self.assertEqual(self.config, self.plugin.config) + + def test_option_namespace(self): + self.assertEqual("mock-", self.plugin.option_namespace) + + def test_dest_namespace(self): + self.assertEqual("mock_", self.plugin.dest_namespace) + + def test_dest(self): + self.assertEqual("mock_foo_bar", self.plugin.dest("foo-bar")) + self.assertEqual("mock_foo_bar", self.plugin.dest("foo_bar")) + + def test_conf(self): + self.assertEqual(self.config.mock_foo_bar, self.plugin.conf("foo-bar")) + + def test_inject_parser_options(self): + parser = mock.MagicMock() + self.plugin_cls.inject_parser_options(parser, "mock") + # note that inject_parser_options doesn't check if dest has + # correct prefix + parser.add_argument.assert_called_once_with( + "--mock-foo-bar", dest="different_to_foo_bar", x=1, y=None) + + +if __name__ == "__main__": + unittest.main()