mirror of
https://github.com/certbot/certbot.git
synced 2026-07-26 07:39:52 +02:00
Added add_deprecated_argument tests
This commit is contained in:
@@ -6,8 +6,9 @@ import logging
|
||||
import os
|
||||
import platform
|
||||
import re
|
||||
import subprocess
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from letsencrypt import errors
|
||||
|
||||
@@ -258,7 +259,7 @@ def safe_email(email):
|
||||
return False
|
||||
|
||||
|
||||
def add_deprecated_argument(add_argument, argument_name):
|
||||
def add_deprecated_argument(add_argument, argument_name, nargs):
|
||||
"""Adds a deprecated argument with the name argument_name.
|
||||
|
||||
Deprecated arguments are not shown in the help. If they are used on
|
||||
@@ -268,11 +269,14 @@ def add_deprecated_argument(add_argument, argument_name):
|
||||
:param callable add_argument: Function that adds arguments to an
|
||||
argument parser/group.
|
||||
:param str argument_name: Name of deprecated argument.
|
||||
:param nargs: Value for nargs when adding the argument to argparse.
|
||||
|
||||
"""
|
||||
class ShowWarning(argparse.Action):
|
||||
"""Action to log a warning when an argument is used."""
|
||||
def __call__(self, unused1, unused2, unused3, option_string=None):
|
||||
print "Use of {0} is deprecated".format(option_string)
|
||||
sys.stderr.write(
|
||||
"Use of {0} is deprecated\n".format(option_string))
|
||||
|
||||
add_argument(argument_name, action=ShowWarning, help=argparse.SUPPRESS)
|
||||
add_argument(argument_name, action=ShowWarning,
|
||||
help=argparse.SUPPRESS, nargs=nargs)
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
"""Tests for letsencrypt.le_util."""
|
||||
import argparse
|
||||
import errno
|
||||
import os
|
||||
import shutil
|
||||
import stat
|
||||
import StringIO
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
@@ -284,5 +286,42 @@ class SafeEmailTest(unittest.TestCase):
|
||||
self.assertFalse(self._call(addr), "%s failed." % addr)
|
||||
|
||||
|
||||
class AddDeprecatedArgumentTest(unittest.TestCase):
|
||||
"""Test add_deprecated_argument."""
|
||||
def setUp(self):
|
||||
self.parser = argparse.ArgumentParser()
|
||||
|
||||
def _call(self, argument_name, nargs):
|
||||
from letsencrypt.le_util import add_deprecated_argument
|
||||
|
||||
add_deprecated_argument(self.parser.add_argument, argument_name, nargs)
|
||||
|
||||
def test_warning_no_arg(self):
|
||||
self._call("--old-option", 0)
|
||||
stderr = self._get_argparse_warnings(["--old-option"])
|
||||
self.assertTrue("--old-option is deprecated" in stderr)
|
||||
|
||||
def test_warning_with_arg(self):
|
||||
self._call("--old-option", 1)
|
||||
stderr = self._get_argparse_warnings(["--old-option", "42"])
|
||||
self.assertTrue("--old-option is deprecated" in stderr)
|
||||
|
||||
def _get_argparse_warnings(self, args):
|
||||
stderr = StringIO.StringIO()
|
||||
with mock.patch("letsencrypt.le_util.sys.stderr", new=stderr):
|
||||
self.parser.parse_args(args)
|
||||
return stderr.getvalue()
|
||||
|
||||
def test_help(self):
|
||||
self._call("--old-option", 2)
|
||||
stdout = StringIO.StringIO()
|
||||
with mock.patch("letsencrypt.le_util.sys.stdout", new=stdout):
|
||||
try:
|
||||
self.parser.parse_args(["-h"])
|
||||
except SystemExit:
|
||||
pass
|
||||
self.assertTrue("--old-option" not in stdout.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main() # pragma: no cover
|
||||
|
||||
Reference in New Issue
Block a user