mirror of
https://github.com/certbot/certbot.git
synced 2026-05-28 04:34:11 -04:00
Stop using print in certbot.cli module. (#5028)
* Update cerbot.tests.util.patch_get_utility (#3720) * Add new arg `stdout_notification` to `cerbot.tests.util.patch_get_utility` function. If `stdout_notification` is True, then the mock interfaces.IDisplay.notification function will print out to stdout. * Add new arg `stdout_notification` to _create_get_utility_mock function. * Add new function `_stdout_notification`. * Stop using print in certbot.cli (#3720) * certbot/cli.py (HelpfulArgumentParser._usage_string) (HelpfulArgumentParser.__init__): Update methods. * certbot/tests/cli_test.py (test_cli_ini_domains, test_no_args) (test_install_abspath, test_help, test_help_no_dashes) (test_parse_domains, test_preferred_challenges, test_server_flag) (test_must_staple_flag, test_no_gui, test_staging_flag) (test_dry_run_flag, test_option_was_set) (test_encode_revocation_reason, test_force_interactive) (test_deploy_hook_conflict, test_deploy_hook_matches_renew_hook) (test_deploy_hook_sets_renew_hook, test_renew_hook_conflict) (test_renew_hook_matches_deploy_hook) (test_renew_hook_does_not_set_renew_hook, test_max_log_backups_error) (test_max_log_backups_success, test_webroot_map) (test_report_config_interaction_str) (test_report_config_interaction_iterable): Update tests. * certbot/tests/main_test.py (test_certificates) (test_certonly_abspath, test_certonly_bad_args) (test_agree_dev_preview_config): Update tests. * certbot: Refactor cli_test.ParseTest. * certbot/tests/cli_test.py (ParseTest._unmocked_parse): Rename parse to _unmocked_parse. (parse): New method. (ParseTest._help_output, ParseTest.test_cli_ini_domains) (ParseTest.test_no_args, ParseTest.test_install_abspath) (ParseTest.test_help, ParseTest.test_help_no_dashes) (ParseTest.test_parse_domains, ParseTest.test_preferred_challenges) (ParseTest.test_server_flag, ParseTest.test_must_staple_flag) (ParseTest.test_no_gui, ParseTest.test_staging_flag) (ParseTest.test_dry_run_flag, ParseTest.test_option_was_set) (ParseTest.test_encode_revocation_reason) (ParseTest.test_force_interactive) (ParseTest.test_deploy_hook_conflict) (ParseTest.test_deploy_hook_matches_renew_hook) (ParseTest.test_deploy_hook_sets_renew_hook) (ParseTest.test_renew_hook_conflict) (ParseTest.test_renew_hook_matches_deploy_hook) (ParseTest.test_renew_hook_does_not_set_renew_hook) (ParseTest.test_max_log_backups_error) (ParseTest.test_max_log_backups_success): Update methods. * certbot: Refactor cli_test.SetByCliTest * certbot/tests/cli_test.py (SetByCliTest.test_webroot_map) (SetByCliTest.test_report_config_interaction_str) (SetByCliTest.test_report_config_interaction_iteratable) (_call_set_by_cli): Update methods. * certbot: cli: Fix style. * certbot/cli.py (HelpfulArgumentParser.__init__): Update method. * certbot: Revert changes to tests.util.patch_get_utility * certbot/tests/util.py (patch_get_utility): Remove `stdout_notification` arg. (_creat_get_utility_mock): Remove `stdout_notification` arg. (_stdout_notification): Remove function. * certbot: Revert changes to MainTest. * certbot/tests/main_test.py (MainTest.test_certificates, MainTest.test_certonly_abspath) (MainTest.test_certonly_bad_args): Update methods. * certbot: cli_test.py: Remove 'pylint: disable' lines. * certbot/tests/cli_test.py (ParseTest.parse): Update method. (_call_set_by_cli): Update function.
This commit is contained in:
parent
a5fae7eab5
commit
b43bf8f94a
2 changed files with 37 additions and 8 deletions
|
|
@ -11,6 +11,9 @@ import sys
|
|||
|
||||
import configargparse
|
||||
import six
|
||||
import zope.component
|
||||
|
||||
from zope.interface import interfaces as zope_interfaces
|
||||
|
||||
from acme import challenges
|
||||
|
||||
|
|
@ -23,6 +26,7 @@ from certbot import hooks
|
|||
from certbot import interfaces
|
||||
from certbot import util
|
||||
|
||||
from certbot.display import util as display_util
|
||||
from certbot.plugins import disco as plugins_disco
|
||||
import certbot.plugins.selection as plugin_selection
|
||||
|
||||
|
|
@ -439,6 +443,15 @@ class HelpfulArgumentParser(object):
|
|||
"delete": main.delete,
|
||||
}
|
||||
|
||||
# Get notification function for printing
|
||||
try:
|
||||
self.notify = zope.component.getUtility(
|
||||
interfaces.IDisplay).notification
|
||||
except zope_interfaces.ComponentLookupError:
|
||||
self.notify = display_util.NoninteractiveDisplay(
|
||||
sys.stdout).notification
|
||||
|
||||
|
||||
# List of topics for which additional help can be provided
|
||||
HELP_TOPICS = ["all", "security", "paths", "automation", "testing"]
|
||||
HELP_TOPICS += list(self.VERBS) + self.COMMANDS_TOPICS + ["manage"]
|
||||
|
|
@ -510,10 +523,10 @@ class HelpfulArgumentParser(object):
|
|||
|
||||
usage = SHORT_USAGE
|
||||
if help_arg == True:
|
||||
print(usage + COMMAND_OVERVIEW % (apache_doc, nginx_doc) + HELP_USAGE)
|
||||
self.notify(usage + COMMAND_OVERVIEW % (apache_doc, nginx_doc) + HELP_USAGE)
|
||||
sys.exit(0)
|
||||
elif help_arg in self.COMMANDS_TOPICS:
|
||||
print(usage + self._list_subcommands())
|
||||
self.notify(usage + self._list_subcommands())
|
||||
sys.exit(0)
|
||||
elif help_arg == "all":
|
||||
# if we're doing --help all, the OVERVIEW is part of the SHORT_USAGE at
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ from certbot import constants
|
|||
from certbot import errors
|
||||
from certbot.plugins import disco
|
||||
|
||||
import certbot.tests.util as test_util
|
||||
|
||||
from certbot.tests.util import TempDirTestCase
|
||||
|
||||
PLUGINS = disco.PluginsRegistry.find_all()
|
||||
|
|
@ -49,17 +51,30 @@ class ParseTest(unittest.TestCase): # pylint: disable=too-many-public-methods
|
|||
reload_module(cli)
|
||||
|
||||
@staticmethod
|
||||
def parse(*args, **kwargs):
|
||||
def _unmocked_parse(*args, **kwargs):
|
||||
"""Get result of cli.prepare_and_parse_args."""
|
||||
return cli.prepare_and_parse_args(PLUGINS, *args, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def parse(*args, **kwargs):
|
||||
"""Mocks zope.component.getUtility and calls _unmocked_parse."""
|
||||
with test_util.patch_get_utility():
|
||||
return ParseTest._unmocked_parse(*args, **kwargs)
|
||||
|
||||
def _help_output(self, args):
|
||||
"Run a command, and return the output string for scrutiny"
|
||||
|
||||
output = six.StringIO()
|
||||
|
||||
def write_msg(message, *args, **kwargs): # pylint: disable=missing-docstring,unused-argument
|
||||
output.write(message)
|
||||
|
||||
with mock.patch('certbot.main.sys.stdout', new=output):
|
||||
with mock.patch('certbot.main.sys.stderr'):
|
||||
self.assertRaises(SystemExit, self.parse, args, output)
|
||||
with test_util.patch_get_utility() as mock_get_utility:
|
||||
mock_get_utility().notification.side_effect = write_msg
|
||||
with mock.patch('certbot.main.sys.stderr'):
|
||||
self.assertRaises(SystemExit, self._unmocked_parse, args, output)
|
||||
|
||||
return output.getvalue()
|
||||
|
||||
@mock.patch("certbot.cli.flag_default")
|
||||
|
|
@ -450,9 +465,10 @@ class SetByCliTest(unittest.TestCase):
|
|||
|
||||
def _call_set_by_cli(var, args, verb):
|
||||
with mock.patch('certbot.cli.helpful_parser') as mock_parser:
|
||||
mock_parser.args = args
|
||||
mock_parser.verb = verb
|
||||
return cli.set_by_cli(var)
|
||||
with test_util.patch_get_utility():
|
||||
mock_parser.args = args
|
||||
mock_parser.verb = verb
|
||||
return cli.set_by_cli(var)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
Loading…
Reference in a new issue