mirror of
https://github.com/certbot/certbot.git
synced 2026-08-01 18:56:55 +02:00
Tests and lint for plugins.common
This commit is contained in:
@@ -8,11 +8,11 @@ from letsencrypt.client import interfaces
|
|||||||
|
|
||||||
def option_namespace(name):
|
def option_namespace(name):
|
||||||
"""ArgumentParser options namespace (prefix of all options)."""
|
"""ArgumentParser options namespace (prefix of all options)."""
|
||||||
return name + '-'
|
return name + "-"
|
||||||
|
|
||||||
def dest_namespace(name):
|
def dest_namespace(name):
|
||||||
"""ArgumentParser dest namespace (prefix of all destinations)."""
|
"""ArgumentParser dest namespace (prefix of all destinations)."""
|
||||||
return name + '_'
|
return name + "_"
|
||||||
|
|
||||||
|
|
||||||
class Plugin(object):
|
class Plugin(object):
|
||||||
@@ -26,17 +26,19 @@ class Plugin(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def option_namespace(self):
|
def option_namespace(self):
|
||||||
|
"""ArgumentParser options namespace (prefix of all options)."""
|
||||||
return option_namespace(self.name)
|
return option_namespace(self.name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def dest_namespace(self):
|
def dest_namespace(self):
|
||||||
|
"""ArgumentParser dest namespace (prefix of all destinations)."""
|
||||||
return dest_namespace(self.name)
|
return dest_namespace(self.name)
|
||||||
|
|
||||||
def dest(self, var):
|
def dest(self, var):
|
||||||
"""Find a destination for given variable ``var``."""
|
"""Find a destination for given variable ``var``."""
|
||||||
# this should do exactly the same what ArgumentParser(arg),
|
# this should do exactly the same what ArgumentParser(arg),
|
||||||
# does to "arg" to compute "dest"
|
# does to "arg" to compute "dest"
|
||||||
return self.dest_namespace + var.replace('-', '_')
|
return self.dest_namespace + var.replace("-", "_")
|
||||||
|
|
||||||
def conf(self, var):
|
def conf(self, var):
|
||||||
"""Find a configuration value for variable ``var``."""
|
"""Find a configuration value for variable ``var``."""
|
||||||
@@ -44,12 +46,18 @@ class Plugin(object):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def inject_parser_options(cls, parser, name):
|
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)
|
# dummy function, doesn't check if dest.startswith(self.dest_namespace)
|
||||||
def add(arg_name_no_prefix, *args, **kwargs):
|
def add(arg_name_no_prefix, *args, **kwargs):
|
||||||
|
# pylint: disable=missing-docstring
|
||||||
return parser.add_argument(
|
return parser.add_argument(
|
||||||
"--{0}{1}".format(option_namespace(name), arg_name_no_prefix),
|
"--{0}{1}".format(option_namespace(name), arg_name_no_prefix),
|
||||||
*args, **kwargs)
|
*args, **kwargs)
|
||||||
cls.add_parser_arguments(add)
|
return cls.add_parser_arguments(add)
|
||||||
|
|
||||||
@jose_util.abstractclassmethod
|
@jose_util.abstractclassmethod
|
||||||
def add_parser_arguments(cls, add):
|
def add_parser_arguments(cls, add):
|
||||||
|
|||||||
@@ -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()
|
||||||
Reference in New Issue
Block a user