Skip to content

Commit

Permalink
Catch warnings in test_translate_existing.
Browse files Browse the repository at this point in the history
Otherwise a warning is printed each time you run the tests.
  • Loading branch information
mauritsvanrees committed Jan 13, 2014
1 parent 5efa988 commit 3c0c323
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
21 changes: 11 additions & 10 deletions src/zope/tal/talgettext.py
Expand Up @@ -34,6 +34,7 @@
import time
import getopt
import traceback
import warnings

from zope.interface import implementer
from zope.tal.htmltalparser import HTMLTALParser
Expand Down Expand Up @@ -137,16 +138,16 @@ def translate(self, msgid, domain=None, mapping=None, default=None,
for location in domain[msgid]])
# Note: a lot of encode calls here are needed so
# Python 3 does not break.
print(("Warning: msgid '%s' in %s already exists "
"with a different default (bad: %s, should be: %s)\n"
"The references for the existent value are:\n%s\n" %
(msgid.encode('utf-8'),
self.file.encode('utf-8') + ':'.encode('utf-8')
+ str(position).encode('utf-8'),
msgid.default.encode('utf-8'),
existing_msgid.default.encode('utf-8'),
references.encode('utf-8'))),
file=sys.stderr)
warnings.warn(
"Warning: msgid '%s' in %s already exists "
"with a different default (bad: %s, should be: %s)\n"
"The references for the existent value are:\n%s\n" %
(msgid.encode('utf-8'),
self.file.encode('utf-8') + ':'.encode('utf-8')
+ str(position).encode('utf-8'),
msgid.default.encode('utf-8'),
existing_msgid.default.encode('utf-8'),
references.encode('utf-8')))
domain[msgid].append((self.file, position))
return 'x'

Expand Down
10 changes: 8 additions & 2 deletions src/zope/tal/tests/test_talgettext.py
Expand Up @@ -15,6 +15,7 @@
"""
import sys
import unittest
import warnings

try:
# Python 2.x
Expand Down Expand Up @@ -76,8 +77,13 @@ def test_translate_existing(self):
default=u'Read more\u2026', position=13)
# Adding the same key with a different default is bad and
# triggers a warning.
engine.translate('foo', 'domain',
default='Read still more…', position=42)
with warnings.catch_warnings(record=True) as log:
warnings.simplefilter("always")
engine.translate('foo', 'domain',
default='Read still more…', position=42)
self.assertEqual(len(log), 1)
self.assertTrue("already exists with a different default"
in log[0].message.message)

def test_dynamic_msgids(self):
sample_source = """
Expand Down

0 comments on commit 3c0c323

Please sign in to comment.