Debian Codegraph @

tests/test_util/test_util_i18n.py

raw
1 """Test i18n util."""
2
3 from __future__ import annotations
4
5 import datetime
6 import os
7 import time
8 from pathlib import Path
9 from typing import TYPE_CHECKING
10
11 import pytest
12 from babel.messages.mofile import read_mo
13
14 from sphinx.errors import SphinxError
15 from sphinx.util import i18n
16
17 if TYPE_CHECKING:
18 from sphinx.testing.util import SphinxTestApp
19
20
21 def test_catalog_info_for_file_and_path() -> None:
22 cat = i18n.CatalogInfo('path', 'domain', 'utf-8')
23 assert cat.po_file == 'domain.po'
24 assert cat.mo_file == 'domain.mo'
25 assert cat.po_path == Path('path', 'domain.po')
26 assert cat.mo_path == Path('path', 'domain.mo')
27
28
29 def test_catalog_info_for_sub_domain_file_and_path() -> None:
30 cat = i18n.CatalogInfo('path', 'sub/domain', 'utf-8')
31 assert cat.po_file == 'sub/domain.po'
32 assert cat.mo_file == 'sub/domain.mo'
33 assert cat.po_path == Path('path', 'sub', 'domain.po')
34 assert cat.mo_path == Path('path', 'sub', 'domain.mo')
35
36
37 def test_catalog_outdated(tmp_path: Path) -> None:
38 (tmp_path / 'test.po').write_text('#', encoding='utf8')
39 cat = i18n.CatalogInfo(tmp_path, 'test', 'utf-8')
40 assert cat.is_outdated() # if mo is not exist
41
42 mo_file = tmp_path / 'test.mo'
43 mo_file.write_text('#', encoding='utf8')
44 assert not cat.is_outdated() # if mo is exist and newer than po
45
46 new_mtime = mo_file.stat().st_mtime_ns - 10_000_000_000
47 os.utime(mo_file, ns=(new_mtime, new_mtime)) # to be outdated
48 assert cat.is_outdated() # if mo is exist and older than po
49
50
51 def test_catalog_write_mo(tmp_path: Path) -> None:
52 (tmp_path / 'test.po').write_text('#', encoding='utf8')
53 cat = i18n.CatalogInfo(tmp_path, 'test', 'utf-8')
54 cat.write_mo('en')
55 mo_path = Path(cat.mo_path)
56 assert mo_path.exists()
57 with open(mo_path, 'rb') as f:
58 assert read_mo(f) is not None
59
60
61 def test_format_date() -> None:
62 date = datetime.datetime(2016, 2, 7, 5, 11, 17, 0) # NoQA: DTZ001
63
64 # strftime format
65 format = '%B %d, %Y'
66 assert i18n.format_date(format, date=date, language='') == 'February 07, 2016'
67 formatted_unknown = i18n.format_date(format, date=date, language='unknown')
68 assert formatted_unknown == 'February 07, 2016'
69 assert i18n.format_date(format, date=date, language='en') == 'February 07, 2016'
70 assert i18n.format_date(format, date=date, language='ja') == '2月 07, 2016'
71 assert i18n.format_date(format, date=date, language='de') == 'Februar 07, 2016'
72
73 # raw string
74 format = 'Mon Mar 28 12:37:08 2016, commit 4367aef'
75 assert i18n.format_date(format, date=date, language='en') == format
76
77 format = '%B %d, %Y, %H:%M:%S %I %p'
78 formatted = i18n.format_date(format, date=date, language='en')
79 assert formatted == 'February 07, 2016, 05:11:17 05 AM'
80
81 format = '%B %-d, %Y, %-H:%-M:%-S %-I %p'
82 formatted = i18n.format_date(format, date=date, language='en')
83 assert formatted == 'February 7, 2016, 5:11:17 5 AM'
84 format = '%x'
85 assert i18n.format_date(format, date=date, language='en') == 'Feb 7, 2016'
86 format = '%X'
87 assert i18n.format_date(format, date=date, language='en') == '5:11:17\u202fAM'
88 format = '%c'
89 formatted = i18n.format_date(format, date=date, language='en')
90 assert formatted == 'Feb 7, 2016, 5:11:17\u202fAM'
91
92 # timezone
93 format = '%Z'
94 assert i18n.format_date(format, date=date, language='en') == 'UTC'
95 format = '%z'
96 assert i18n.format_date(format, date=date, language='en') == '+0000'
97
98
99 def test_format_date_timezone() -> None:
100 dt = datetime.datetime(2016, 8, 7, 5, 11, 17, 0, tzinfo=datetime.UTC)
101 if time.localtime(dt.timestamp()).tm_gmtoff == 0:
102 raise pytest.skip('Local time zone is GMT') # NoQA: EM101,TRY003
103
104 fmt = '%Y-%m-%d %H:%M:%S'
105
106 iso_gmt = dt.isoformat(' ').split('+')[0]
107 fd_gmt = i18n.format_date(fmt, date=dt, language='en', local_time=False)
108 assert fd_gmt == '2016-08-07 05:11:17'
109 assert fd_gmt == iso_gmt
110
111 iso_local = dt.astimezone().isoformat(' ')[:19] # strip the timezone
112 fd_local = i18n.format_date(fmt, date=dt, language='en', local_time=True)
113 assert fd_local == iso_local
114 assert fd_local != fd_gmt
115
116
117 @pytest.mark.sphinx('html', testroot='root')
118 def test_get_filename_for_language(app: SphinxTestApp) -> None:
119 get_filename = i18n.get_image_filename_for_language
120 app.env.current_document.docname = 'index'
121
122 # language is en
123 app.config.language = 'en'
124 assert get_filename('foo.png', app.env) == 'foo.en.png'
125 assert get_filename('foo.bar.png', app.env) == 'foo.bar.en.png'
126 assert get_filename('dir/foo.png', app.env) == 'dir/foo.en.png'
127 assert get_filename('../foo.png', app.env) == '../foo.en.png'
128 assert get_filename('foo', app.env) == 'foo.en'
129
130 # modify figure_language_filename and language is 'en'
131 app.config.language = 'en'
132 app.config.figure_language_filename = 'images/{language}/{root}{ext}'
133 assert get_filename('foo.png', app.env) == 'images/en/foo.png'
134 assert get_filename('foo.bar.png', app.env) == 'images/en/foo.bar.png'
135 assert get_filename('subdir/foo.png', app.env) == 'images/en/subdir/foo.png'
136 assert get_filename('../foo.png', app.env) == 'images/en/../foo.png'
137 assert get_filename('foo', app.env) == 'images/en/foo'
138
139 # new path and basename tokens
140 app.config.language = 'en'
141 app.config.figure_language_filename = '{path}{language}/{basename}{ext}'
142 assert get_filename('foo.png', app.env) == 'en/foo.png'
143 assert get_filename('foo.bar.png', app.env) == 'en/foo.bar.png'
144 assert get_filename('subdir/foo.png', app.env) == 'subdir/en/foo.png'
145 assert get_filename('../foo.png', app.env) == '../en/foo.png'
146 assert get_filename('foo', app.env) == 'en/foo'
147
148 # invalid figure_language_filename
149 app.config.figure_language_filename = '{root}.{invalid}{ext}'
150 with pytest.raises(SphinxError):
151 get_filename('foo.png', app.env)
152
153 # docpath (for a document in the top of source directory)
154 app.config.language = 'en'
155 app.config.figure_language_filename = '/{docpath}{language}/{basename}{ext}'
156 assert get_filename('foo.png', app.env) == '/en/foo.png'
157
158 # docpath (for a document in the sub directory)
159 app.env.current_document.docname = 'subdir/index'
160 assert get_filename('foo.png', app.env) == '/subdir/en/foo.png'
161
162
163 def test_CatalogRepository(tmp_path: Path) -> None:
164 for po_file in (
165 (tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test1.po'),
166 (tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test2.po'),
167 (tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test3.po'),
168 (tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test4.po'),
169 (tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / '.dotdir' / 'test5.po'),
170 (tmp_path / 'loc1' / 'yy' / 'LC_MESSAGES' / 'test6.po'),
171 (tmp_path / 'loc2' / 'xx' / 'LC_MESSAGES' / 'test1.po'),
172 (tmp_path / 'loc2' / 'xx' / 'LC_MESSAGES' / 'test7.po'),
173 (tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / '.dotdir2' / 'test8.po'),
174 ):
175 po_file.parent.mkdir(parents=True, exist_ok=True)
176 po_file.write_text('#', encoding='utf8')
177
178 # for language xx
179 repo = i18n.CatalogRepository(tmp_path, ['loc1', 'loc2'], 'xx', 'utf-8')
180 assert list(repo.locale_dirs) == [
181 tmp_path / 'loc1',
182 tmp_path / 'loc2',
183 ]
184 assert all(isinstance(c, i18n.CatalogInfo) for c in repo.catalogs)
185 assert sorted(c.domain for c in repo.catalogs) == [
186 'sub/test3',
187 'sub/test4',
188 'test1',
189 'test1',
190 'test2',
191 'test7',
192 ]
193
194 # for language yy
195 repo = i18n.CatalogRepository(tmp_path, ['loc1', 'loc2'], 'yy', 'utf-8')
196 assert sorted(c.domain for c in repo.catalogs) == ['test6']
197
198 # unknown languages
199 repo = i18n.CatalogRepository(tmp_path, ['loc1', 'loc2'], 'zz', 'utf-8')
200 assert sorted(c.domain for c in repo.catalogs) == []
201
202 # no languages
203 repo = i18n.CatalogRepository(tmp_path, ['loc1', 'loc2'], '', 'utf-8')
204 assert sorted(c.domain for c in repo.catalogs) == []
205
206 # unknown locale_dirs
207 repo = i18n.CatalogRepository(tmp_path, ['loc3'], '', 'utf-8')
208 assert sorted(c.domain for c in repo.catalogs) == []
209
210 # no locale_dirs
211 repo = i18n.CatalogRepository(tmp_path, [], '', 'utf-8')
212 assert sorted(c.domain for c in repo.catalogs) == []