diff --git a/.pylintrc b/.pylintrc index 6b38b1d48..36d8c286f 100644 --- a/.pylintrc +++ b/.pylintrc @@ -225,7 +225,7 @@ single-line-if-stmt=no no-space-check=trailing-comma # Maximum number of lines in a module -max-module-lines=1300 +max-module-lines=1250 # String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 # tab). diff --git a/certbot/main.py b/certbot/main.py index fe9115f49..39f127b97 100644 --- a/certbot/main.py +++ b/certbot/main.py @@ -1,4 +1,5 @@ """Certbot main entry point.""" +# pylint: disable=too-many-lines from __future__ import print_function import functools import logging.handlers @@ -792,9 +793,7 @@ def install(config, plugins): else: raise errors.ConfigurationError("Path to certificate or key was not defined. " "If your certificate is managed by Certbot, please use --cert-name " - "to define which certificate you would like to install. " - "Alternatively you can use both --key-path and --cert-path to install a " - "custom certificate.") + "to define which certificate you would like to install.") def _populate_from_certname(config): """Helper function for install to populate missing config values from lineage diff --git a/certbot/tests/main_test.py b/certbot/tests/main_test.py index ba1fabae1..525327f95 100644 --- a/certbot/tests/main_test.py +++ b/certbot/tests/main_test.py @@ -592,11 +592,30 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met super(MainTest, self).tearDown() - def _call(self, args, stdout=None): - "Run the cli with output streams and actual client mocked out" - with mock.patch('certbot.main.client') as client: - ret, stdout, stderr = self._call_no_clientmock(args, stdout) - return ret, stdout, stderr, client + def _call(self, args, stdout=None, mockisfile=False): + """Run the cli with output streams, actual client and optionally + os.path.isfile() mocked out""" + + if mockisfile: + orig_open = os.path.isfile + def mock_isfile(fn, *args, **kwargs): + """Mock os.path.isfile()""" + if (fn.endswith("cert") or + fn.endswith("chain") or + fn.endswith("privkey")): + return True + else: + return orig_open(fn, *args, **kwargs) + + with mock.patch("os.path.isfile") as mock_if: + mock_if.side_effect = mock_isfile + with mock.patch('certbot.main.client') as client: + ret, stdout, stderr = self._call_no_clientmock(args, stdout) + return ret, stdout, stderr, client + else: + with mock.patch('certbot.main.client') as client: + ret, stdout, stderr = self._call_no_clientmock(args, stdout) + return ret, stdout, stderr, client def _call_no_clientmock(self, args, stdout=None): "Run the client with output streams mocked out" @@ -679,10 +698,9 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met @mock.patch('certbot.main.plug_sel.record_chosen_plugins') @mock.patch('certbot.main.plug_sel.pick_installer') def test_installer_selection(self, mock_pick_installer, _rec): - with mock.patch("os.path.isfile", return_value=True): - self._call(['install', '--domains', 'foo.bar', '--cert-path', 'cert', - '--key-path', 'key', '--chain-path', 'chain']) - self.assertEqual(mock_pick_installer.call_count, 1) + self._call(['install', '--domains', 'foo.bar', '--cert-path', 'cert', + '--key-path', 'privkey', '--chain-path', 'chain'], mockisfile=True) + self.assertEqual(mock_pick_installer.call_count, 1) @mock.patch('certbot.main._install_cert') @mock.patch('certbot.main.plug_sel.record_chosen_plugins') @@ -691,14 +709,14 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met mock_lineage = mock.MagicMock(cert_path="/tmp/cert", chain_path="/tmp/chain", fullchain_path="/tmp/chain", key_path="/tmp/privkey") + with mock.patch("certbot.cert_manager.lineage_for_certname") as mock_getlin: mock_getlin.return_value = mock_lineage - with mock.patch("os.path.isfile", return_value=True): - self._call(['install', '--cert-name', 'whatever']) - call_config = mock_install.call_args[0][0] - self.assertEqual(call_config.cert_path, "/tmp/cert") - self.assertEqual(call_config.fullchain_path, "/tmp/chain") - self.assertEqual(call_config.key_path, "/tmp/privkey") + self._call(['install', '--cert-name', 'whatever'], mockisfile=True) + call_config = mock_install.call_args[0][0] + self.assertEqual(call_config.cert_path, "/tmp/cert") + self.assertEqual(call_config.fullchain_path, "/tmp/chain") + self.assertEqual(call_config.key_path, "/tmp/privkey") @mock.patch('certbot.main._install_cert') @mock.patch('certbot.main.plug_sel.record_chosen_plugins') @@ -709,21 +727,21 @@ class MainTest(test_util.ConfigTestCase): # pylint: disable=too-many-public-met key_path="/tmp/privkey") with mock.patch("certbot.cert_manager.lineage_for_certname") as mock_getlin: mock_getlin.return_value = mock_lineage - with mock.patch("os.path.isfile", return_value=True): - self._call(['install', '--cert-name', 'whatever', - '--key-path', '/tmp/overriding_key_path']) - call_config = mock_install.call_args[0][0] - self.assertEqual(call_config.cert_path, "/tmp/cert") - self.assertEqual(call_config.fullchain_path, "/tmp/chain") - self.assertEqual(call_config.key_path, "/tmp/overriding_key_path") + self._call(['install', '--cert-name', 'whatever', + '--key-path', '/tmp/overriding_privkey'], mockisfile=True) + call_config = mock_install.call_args[0][0] + self.assertEqual(call_config.cert_path, "/tmp/cert") + self.assertEqual(call_config.fullchain_path, "/tmp/chain") + self.assertEqual(call_config.key_path, "/tmp/overriding_privkey") + mock_install.reset() - with mock.patch("os.path.isfile", return_value=True): - self._call(['install', '--cert-name', 'whatever', - '--cert-path', '/tmp/overriding_cert_path']) - call_config = mock_install.call_args[0][0] - self.assertEqual(call_config.cert_path, "/tmp/overriding_cert_path") - self.assertEqual(call_config.fullchain_path, "/tmp/chain") - self.assertEqual(call_config.key_path, "/tmp/privkey") + + self._call(['install', '--cert-name', 'whatever', + '--cert-path', '/tmp/overriding_cert'], mockisfile=True) + call_config = mock_install.call_args[0][0] + self.assertEqual(call_config.cert_path, "/tmp/overriding_cert") + self.assertEqual(call_config.fullchain_path, "/tmp/chain") + self.assertEqual(call_config.key_path, "/tmp/privkey") @mock.patch('certbot.main.plug_sel.record_chosen_plugins') @mock.patch('certbot.main.plug_sel.pick_installer')