override options set earlier on the command line, but can be overridden
by later flags.
"""
- if name in self._options:
+ normalized = self._normalize_name(name)
+ if normalized in self._options:
raise Error("Option %r already defined in %s" %
- (name, self._options[name].file_name))
+ (normalized, self._options[normalized].file_name))
frame = sys._getframe(0)
options_file = frame.f_code.co_filename
group_name = group
else:
group_name = file_name
- normalized = self._normalize_name(name)
option = _Option(name, file_name=file_name,
default=default, type=type, help=help,
metavar=metavar, multiple=multiple,
from tornado.options import OptionParser, Error
from tornado.util import basestring_type, PY3
-from tornado.test.util import unittest
+from tornado.test.util import unittest, subTest
if PY3:
from io import StringIO
self.assertRegexpMatches(str(cm.exception),
'Option.*foo.*already defined')
+ def test_error_redefine_underscore(self):
+ # Ensure that the dash/underscore normalization doesn't
+ # interfere with the redefinition error.
+ tests = [
+ ('foo-bar', 'foo-bar'),
+ ('foo_bar', 'foo_bar'),
+ ('foo-bar', 'foo_bar'),
+ ('foo_bar', 'foo-bar'),
+ ]
+ for a, b in tests:
+ with subTest(self, a=a, b=b):
+ options = OptionParser()
+ options.define(a)
+ with self.assertRaises(Error) as cm:
+ options.define(b)
+ self.assertRegexpMatches(str(cm.exception),
+ 'Option.*foo.bar.*already defined')
+
def test_dash_underscore_cli(self):
# Dashes and underscores should be interchangeable.
for defined_name in ['foo-bar', 'foo_bar']:
from __future__ import absolute_import, division, print_function
+import contextlib
import os
import platform
import socket
except AttributeError:
return False
return mod.startswith('coverage')
+
+
+def subTest(test, *args, **kwargs):
+ """Compatibility shim for unittest.TestCase.subTest.
+
+ Usage: ``with tornado.test.util.subTest(self, x=x):``
+ """
+ try:
+ subTest = test.subTest # py34+
+ except AttributeError:
+ subTest = contextlib.contextmanager(lambda *a, **kw: (yield))
+ return subTest(*args, **kwargs)