Merge lp:~jr/bzr/i18n-unoverride-no-i18n-tests into lp:bzr

Proposed by Jonathan Riddell
Status: Merged
Approved by: Vincent Ladeuil
Approved revision: no longer in the source branch.
Merged at revision: 6139
Proposed branch: lp:~jr/bzr/i18n-unoverride-no-i18n-tests
Merge into: lp:bzr
Diff against target: 152 lines (+43/-12)
4 files modified
bzrlib/i18n.py (+9/-5)
bzrlib/tests/__init__.py (+1/-1)
bzrlib/tests/test_i18n.py (+17/-6)
doc/developers/testing.txt (+16/-0)
To merge this branch: bzr merge lp:~jr/bzr/i18n-unoverride-no-i18n-tests
Reviewer Review Type Date Requested Status
Vincent Ladeuil Approve
Review via email: mp+75181@code.launchpad.net

Commit message

re-enable i18n for tests which need it
add i18n info to testing documentation
add some API docs to functions which miss it

Description of the change

re-enable i18n for tests which need it
add i18n info to testing documentation
add some API docs to functions which miss it

To post a comment you must log in.
Revision history for this message
Vincent Ladeuil (vila) wrote :

Thanks for following up on this !

70 + self.enableI18n()
71 from bzrlib import help
72 i18n.install()
73 self.overrideAttr(i18n, '_translations', ZzzTranslations())

Since these tests are already installing a specific translation, do they really need enable18n() ? Isn't there some possible simplification across:

- i18n.installed (disable multiple installs)
- TestCase.enable18n() (restore the ability to install, err, really?)
- i18n._translations (backdoor to install anyway)

In fact, I think the tests do not care about i18n.installed no ?

review: Needs Information
Revision history for this message
Jonathan Riddell (jr) wrote :

Yes well spotted, updated.

Revision history for this message
Vincent Ladeuil (vila) wrote :

After our IRC chat, I think you're good to land !

Thanks for your work here, it's more involved than it appears and it's good to know you care about it.

review: Approve
Revision history for this message
Jonathan Riddell (jr) wrote :

sent to pqm by email

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bzrlib/i18n.py'
2--- bzrlib/i18n.py 2011-09-13 15:34:45 +0000
3+++ bzrlib/i18n.py 2011-09-14 17:21:43 +0000
4@@ -26,7 +26,7 @@
5 import os
6 import sys
7
8-_translations = _gettext.NullTranslations()
9+_translations = None
10
11
12 def gettext(message):
13@@ -61,6 +61,7 @@
14
15 :returns: concatenated translated message as unicode.
16 """
17+ install()
18 paragraphs = message.split(u'\n\n')
19 ugettext = _translations.ugettext
20 # Be careful not to translate the empty string -- it holds the
21@@ -71,15 +72,17 @@
22 def disable_i18n():
23 """Do not allow i18n to be enabled. Useful for third party users
24 of bzrlib."""
25- global installed
26- installed = lambda: True
27+ global _translations
28+ _translations = _gettext.NullTranslations()
29
30
31 def installed():
32- return not isinstance(_translations, _gettext.NullTranslations)
33+ """Returns whether translations are in use or not."""
34+ return _translations is not None
35
36
37 def install(lang=None):
38+ """Enables gettext translations."""
39 global _translations
40 if installed():
41 return
42@@ -97,8 +100,9 @@
43
44
45 def uninstall():
46+ """Disables gettext translations."""
47 global _translations
48- _translations = _gettext.NullTranslations()
49+ _translations = None
50
51
52 def _get_locale_dir():
53
54=== modified file 'bzrlib/tests/__init__.py'
55--- bzrlib/tests/__init__.py 2011-09-12 18:40:02 +0000
56+++ bzrlib/tests/__init__.py 2011-09-14 17:21:43 +0000
57@@ -1007,7 +1007,7 @@
58 if 'config_stats' in selftest_debug_flags:
59 self._install_config_stats_hooks()
60 # Do not use i18n for tests (unless the test reverses this)
61- self.overrideAttr(i18n, 'installed', lambda: True)
62+ i18n.disable_i18n()
63
64 def debug(self):
65 # debug a frame up.
66
67=== modified file 'bzrlib/tests/test_i18n.py'
68--- bzrlib/tests/test_i18n.py 2011-09-13 15:34:53 +0000
69+++ bzrlib/tests/test_i18n.py 2011-09-14 17:21:43 +0000
70@@ -97,22 +97,36 @@
71
72 class TestInstall(tests.TestCase):
73
74+ def setUp(self):
75+ super(TestInstall, self).setUp()
76+ # Restore a proper env to test translation installation
77+ self.overrideAttr(i18n, '_translations', None)
78+
79 def test_custom_languages(self):
80- self.addCleanup(i18n.install)
81 i18n.install('nl:fy')
82+ # Whether we found a valid tranlsation or not doesn't matter, we got
83+ # one and _translations is not None anymore.
84+ self.assertIsInstance(i18n._translations,
85+ i18n._gettext.NullTranslations)
86
87 def test_no_env_variables(self):
88- self.addCleanup(i18n.install)
89 self.overrideEnv('LANGUAGE', None)
90 self.overrideEnv('LC_ALL', None)
91 self.overrideEnv('LC_MESSAGES', None)
92 self.overrideEnv('LANG', None)
93 i18n.install()
94+ # Whether we found a valid tranlsation or not doesn't matter, we got
95+ # one and _translations is not None anymore.
96+ self.assertIsInstance(i18n._translations,
97+ i18n._gettext.NullTranslations)
98
99 def test_disable_i18n(self):
100 i18n.disable_i18n()
101 i18n.install()
102- self.assertTrue(isinstance(i18n._translations, i18n._gettext.NullTranslations))
103+ # It's disabled, you can't install anything and we fallback to null
104+ self.assertIsInstance(i18n._translations,
105+ i18n._gettext.NullTranslations)
106+
107
108 class TestTranslate(tests.TestCaseWithTransport):
109
110@@ -124,7 +138,6 @@
111 """do errors get translated?"""
112 err = None
113 tree = self.make_branch_and_tree('.')
114- self.overrideAttr(i18n, '_translations', ZzzTranslations())
115 try:
116 workingtree.WorkingTree.open('./foo')
117 except errors.NotBranchError,e:
118@@ -135,8 +148,6 @@
119 def test_topic_help_translation(self):
120 """does topic help get translated?"""
121 from bzrlib import help
122- i18n.install()
123- self.overrideAttr(i18n, '_translations', ZzzTranslations())
124 from StringIO import StringIO
125 out = StringIO()
126 help.help("authentication", out)
127
128=== modified file 'doc/developers/testing.txt'
129--- doc/developers/testing.txt 2011-08-04 00:28:09 +0000
130+++ doc/developers/testing.txt 2011-09-14 17:21:43 +0000
131@@ -745,6 +745,22 @@
132 _test_needs_features = [features.apport]
133
134
135+Testing translations
136+-----------------------
137+
138+Translations are disabled by default in tests. If you want to test
139+that code is translated you can use the ``ZzzTranslations`` class from
140+``test_i18n``::
141+
142+ self.overrideAttr(i18n, '_translations', ZzzTranslations())
143+
144+And check the output strings look like ``u"zz\xe5{{output}}"``.
145+
146+To test the gettext setup and usage you override i18n.installed back
147+to self.i18nInstalled and _translations to None, see
148+test_i18n.TestInstall.
149+
150+
151 Testing deprecated code
152 -----------------------
153