Tests and lint for plugins.common

This commit is contained in:
Jakub Warmuz
2015-05-02 11:19:46 +00:00
parent 140ca2b4d0
commit 5672916cb2
2 changed files with 73 additions and 4 deletions
+12 -4
View File
@@ -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):
+61
View File
@@ -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()