16 # |
16 # |
17 # You should have received a copy of the GNU Lesser General Public License along |
17 # You should have received a copy of the GNU Lesser General Public License along |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
19 """unit tests for i18n messages generator""" |
19 """unit tests for i18n messages generator""" |
20 |
20 |
|
21 from contextlib import contextmanager |
|
22 from io import StringIO, BytesIO |
21 import os |
23 import os |
22 import os.path as osp |
24 import os.path as osp |
23 import sys |
25 import sys |
24 from subprocess import PIPE, Popen, STDOUT |
26 from subprocess import PIPE, Popen, STDOUT |
25 |
|
26 from unittest import TestCase, main |
27 from unittest import TestCase, main |
27 |
28 |
|
29 from six import PY2 |
|
30 from mock import patch |
|
31 |
|
32 from cubicweb.devtools import devctl |
|
33 from cubicweb.devtools.testlib import BaseTestCase |
28 |
34 |
29 DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data') |
35 DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data') |
30 |
36 |
31 |
37 |
32 def load_po(fname): |
38 def load_po(fname): |
85 msgs = load_po(osp.join(cubedir, 'i18n', 'en.po.ref')) |
94 msgs = load_po(osp.join(cubedir, 'i18n', 'en.po.ref')) |
86 newmsgs = load_po(osp.join(cubedir, 'i18n', 'en.po')) |
95 newmsgs = load_po(osp.join(cubedir, 'i18n', 'en.po')) |
87 self.assertEqual(msgs, newmsgs) |
96 self.assertEqual(msgs, newmsgs) |
88 |
97 |
89 |
98 |
|
99 class CustomMessageExtractor(devctl.I18nCubeMessageExtractor): |
|
100 blacklist = devctl.I18nCubeMessageExtractor.blacklist | set(['excludeme']) |
|
101 |
|
102 |
|
103 @contextmanager |
|
104 def capture_stdout(): |
|
105 stream = BytesIO() if PY2 else StringIO() |
|
106 sys.stdout = stream |
|
107 yield stream |
|
108 stream.seek(0) |
|
109 sys.stdout = sys.__stdout__ |
|
110 |
|
111 |
|
112 class I18nCollectorTest(BaseTestCase): |
|
113 |
|
114 def test_i18ncube_py_collection(self): |
|
115 extractor = CustomMessageExtractor(DATADIR, TESTCUBE_DIR) |
|
116 collected = extractor.collect_py() |
|
117 expected = [osp.join(TESTCUBE_DIR, path) |
|
118 for path in ('__init__.py', '__pkginfo__.py', |
|
119 'views.py', 'schema.py')] |
|
120 self.assertCountEqual(expected, collected) |
|
121 |
|
122 def test_i18ncube_js_collection(self): |
|
123 extractor = CustomMessageExtractor(DATADIR, TESTCUBE_DIR) |
|
124 collected = extractor.collect_js() |
|
125 self.assertCountEqual([], collected, []) |
|
126 extractor.blacklist = () # don't ignore anything |
|
127 collected = extractor.collect_js() |
|
128 expected = [osp.join(TESTCUBE_DIR, 'node_modules/cubes.somefile.js')] |
|
129 self.assertCountEqual(expected, collected) |
|
130 |
|
131 class FakeMessageExtractor(devctl.I18nCubeMessageExtractor): |
|
132 """Fake message extractor that generates no pot file.""" |
|
133 |
|
134 def generate_pot_file(self): |
|
135 return None |
|
136 |
|
137 @patch('pkg_resources.load_entry_point', return_value=FakeMessageExtractor) |
|
138 def test_cube_custom_extractor(self, mock_load_entry_point): |
|
139 for distname, cubedir in [ |
|
140 ('cubicweb_i18ntestcube', |
|
141 osp.join(DATADIR, 'libpython', 'cubicweb_i18ntestcube')), |
|
142 # Legacy cubes. |
|
143 ('i18ntestcube', osp.join(DATADIR, 'cubes', 'i18ntestcube')), |
|
144 ]: |
|
145 with self.subTest(cubedir=cubedir): |
|
146 with capture_stdout() as stream: |
|
147 devctl.update_cube_catalogs(cubedir) |
|
148 self.assertIn(u'no message catalog for cube i18ntestcube', |
|
149 stream.read()) |
|
150 mock_load_entry_point.assert_called_once_with( |
|
151 distname, 'cubicweb.i18ncube', 'i18ntestcube') |
|
152 mock_load_entry_point.reset_mock() |
|
153 |
|
154 |
90 if __name__ == '__main__': |
155 if __name__ == '__main__': |
91 main() |
156 main() |