| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
import ctypes |
| 29 |
from importlib import reload |
| 30 |
|
| 31 |
|
| 32 |
import logging |
| 33 |
import os |
| 34 |
import sys |
| 35 |
from unittest import mock |
| 36 |
|
| 37 |
from helpers import environ |
| 38 |
import pytest |
| 39 |
|
| 40 |
from apprise import locale |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
|
| 45 |
def test_apprise_trans(): |
| 46 |
""" |
| 47 |
API: Test apprise locale object |
| 48 |
""" |
| 49 |
lazytrans = locale.LazyTranslation("Token") |
| 50 |
assert str(lazytrans) == "Token" |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 54 |
def test_apprise_trans_gettext_init(): |
| 55 |
""" |
| 56 |
API: Handle gettext |
| 57 |
""" |
| 58 |
|
| 59 |
locale.GETTEXT_LOADED = False |
| 60 |
|
| 61 |
|
| 62 |
al = locale.AppriseLocale() |
| 63 |
|
| 64 |
with al.lang_at("en") as _: |
| 65 |
|
| 66 |
assert _ is None |
| 67 |
|
| 68 |
|
| 69 |
locale.GETTEXT_LOADED = True |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 73 |
@mock.patch("gettext.translation") |
| 74 |
@mock.patch("locale.getlocale") |
| 75 |
def test_apprise_trans_gettext_translations( |
| 76 |
mock_getlocale, mock_gettext_trans |
| 77 |
): |
| 78 |
""" |
| 79 |
API: Apprise() Gettext translations |
| 80 |
|
| 81 |
""" |
| 82 |
|
| 83 |
|
| 84 |
mock_getlocale.return_value = ("en_US", "UTF-8") |
| 85 |
|
| 86 |
mock_gettext_trans.side_effect = FileNotFoundError() |
| 87 |
|
| 88 |
|
| 89 |
al = locale.AppriseLocale() |
| 90 |
|
| 91 |
with al.lang_at("en"): |
| 92 |
|
| 93 |
pass |
| 94 |
|
| 95 |
|
| 96 |
locale.AppriseLocale(language="fr") |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.skipif(hasattr(ctypes, "windll"), reason="Unique Nux test cases") |
| 100 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 101 |
@mock.patch("locale.getlocale") |
| 102 |
def test_apprise_trans_gettext_lang_at(mock_getlocale): |
| 103 |
""" |
| 104 |
API: Apprise() Gettext lang_at |
| 105 |
|
| 106 |
""" |
| 107 |
|
| 108 |
|
| 109 |
mock_getlocale.return_value = ("en_CA", "UTF-8") |
| 110 |
|
| 111 |
|
| 112 |
al = locale.AppriseLocale() |
| 113 |
|
| 114 |
|
| 115 |
assert al.add("en", set_default=False) is True |
| 116 |
assert al.add("en", set_default=True) is True |
| 117 |
|
| 118 |
with al.lang_at("en"): |
| 119 |
|
| 120 |
pass |
| 121 |
|
| 122 |
|
| 123 |
locale.AppriseLocale(language="fr") |
| 124 |
|
| 125 |
with al.lang_at("en") as _: |
| 126 |
|
| 127 |
assert callable(_) |
| 128 |
|
| 129 |
with al.lang_at("es") as _: |
| 130 |
|
| 131 |
assert callable(_) |
| 132 |
|
| 133 |
with al.lang_at("fr") as _: |
| 134 |
|
| 135 |
assert callable(_) |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
fallback = locale.AppriseLocale._default_language |
| 142 |
mock_getlocale.return_value = None |
| 143 |
|
| 144 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 145 |
|
| 146 |
locale.AppriseLocale._default_language = "zz" |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
assert locale.AppriseLocale.detect_language() is None |
| 151 |
al = locale.AppriseLocale() |
| 152 |
|
| 153 |
|
| 154 |
assert al.lang is None |
| 155 |
|
| 156 |
with al.lang_at(None) as _: |
| 157 |
|
| 158 |
assert callable(_) |
| 159 |
|
| 160 |
with al.lang_at("en") as _: |
| 161 |
|
| 162 |
assert callable(_) |
| 163 |
|
| 164 |
with al.lang_at("es") as _: |
| 165 |
|
| 166 |
assert callable(_) |
| 167 |
|
| 168 |
with al.lang_at("fr") as _: |
| 169 |
|
| 170 |
assert callable(_) |
| 171 |
|
| 172 |
|
| 173 |
assert al.gettext("test") == "test" |
| 174 |
|
| 175 |
with environ("LANGUAGE", "LC_CTYPE", LC_ALL="C.UTF-8", LANG="en_CA"): |
| 176 |
|
| 177 |
locale.AppriseLocale._default_language = "fr" |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
assert locale.AppriseLocale.detect_language() == "en" |
| 182 |
al = locale.AppriseLocale() |
| 183 |
assert al.lang == "en" |
| 184 |
assert al.gettext("test") == "test" |
| 185 |
|
| 186 |
|
| 187 |
assert al.add("zy", set_default=False) is False |
| 188 |
assert al.gettext("test") == "test" |
| 189 |
|
| 190 |
al.add("ab", set_default=True) |
| 191 |
assert al.gettext("test") == "test" |
| 192 |
|
| 193 |
assert al.add("zy", set_default=False) is False |
| 194 |
locale.AppriseLocale._default_language = fallback |
| 195 |
|
| 196 |
|
| 197 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 198 |
def test_apprise_trans_add(): |
| 199 |
""" |
| 200 |
API: Apprise() Gettext add |
| 201 |
|
| 202 |
""" |
| 203 |
|
| 204 |
|
| 205 |
al = locale.AppriseLocale() |
| 206 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 207 |
|
| 208 |
assert al.add("en") is True |
| 209 |
|
| 210 |
al = locale.AppriseLocale() |
| 211 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", LANG="C.UTF-8"): |
| 212 |
|
| 213 |
assert al.add("en") is True |
| 214 |
|
| 215 |
al = locale.AppriseLocale() |
| 216 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", LANG="en_CA.UTF-8"): |
| 217 |
|
| 218 |
assert al.add("en") is True |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
assert al.add("en") is True |
| 223 |
|
| 224 |
|
| 225 |
assert al.add("bad") is False |
| 226 |
|
| 227 |
|
| 228 |
@pytest.mark.skipif( |
| 229 |
not hasattr(ctypes, "windll"), reason="Unique Windows test cases" |
| 230 |
) |
| 231 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 232 |
@mock.patch("locale.getlocale") |
| 233 |
def test_apprise_trans_windows_users_win(mock_getlocale): |
| 234 |
""" |
| 235 |
API: Apprise() Windows Locale Testing (Win version) |
| 236 |
|
| 237 |
""" |
| 238 |
|
| 239 |
|
| 240 |
mock_getlocale.return_value = ("fr_CA", "UTF-8") |
| 241 |
|
| 242 |
with mock.patch( |
| 243 |
"ctypes.windll.kernel32.GetUserDefaultUILanguage" |
| 244 |
) as ui_lang: |
| 245 |
|
| 246 |
ui_lang.return_value = 4105 |
| 247 |
|
| 248 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 249 |
|
| 250 |
locale.AppriseLocale._default_language = "zz" |
| 251 |
|
| 252 |
|
| 253 |
assert locale.AppriseLocale.detect_language() == "en" |
| 254 |
|
| 255 |
|
| 256 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", LANG="es_AR"): |
| 257 |
|
| 258 |
assert locale.AppriseLocale.detect_language() == "es" |
| 259 |
|
| 260 |
|
| 261 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 262 |
|
| 263 |
assert locale.AppriseLocale.detect_language() == "en" |
| 264 |
|
| 265 |
assert ( |
| 266 |
locale.AppriseLocale.detect_language(detect_fallback=False) is None |
| 267 |
) |
| 268 |
|
| 269 |
|
| 270 |
ui_lang.return_value = 0 |
| 271 |
with environ("LANGUAGE", "LANG", "LC_ALL", "LC_CTYPE"): |
| 272 |
|
| 273 |
assert locale.AppriseLocale.detect_language() == "fr" |
| 274 |
|
| 275 |
|
| 276 |
@pytest.mark.skipif(hasattr(ctypes, "windll"), reason="Unique Nux test cases") |
| 277 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 278 |
@mock.patch("locale.getlocale") |
| 279 |
def test_apprise_trans_windows_users_nux(mock_getlocale): |
| 280 |
""" |
| 281 |
API: Apprise() Windows Locale Testing (Nux version) |
| 282 |
|
| 283 |
""" |
| 284 |
|
| 285 |
|
| 286 |
mock_getlocale.return_value = ("fr_CA", "UTF-8") |
| 287 |
|
| 288 |
|
| 289 |
windll = mock.Mock() |
| 290 |
ctypes.windll = windll |
| 291 |
|
| 292 |
|
| 293 |
windll.kernel32.GetUserDefaultUILanguage.return_value = 4105 |
| 294 |
|
| 295 |
|
| 296 |
default_language = locale.AppriseLocale._default_language |
| 297 |
|
| 298 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 299 |
|
| 300 |
locale.AppriseLocale._default_language = "zz" |
| 301 |
|
| 302 |
|
| 303 |
assert locale.AppriseLocale.detect_language() == "en" |
| 304 |
|
| 305 |
|
| 306 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", LANG="es_AR"): |
| 307 |
|
| 308 |
assert locale.AppriseLocale.detect_language() == "es" |
| 309 |
|
| 310 |
|
| 311 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 312 |
|
| 313 |
assert locale.AppriseLocale.detect_language() == "en" |
| 314 |
|
| 315 |
assert locale.AppriseLocale.detect_language(detect_fallback=False) is None |
| 316 |
|
| 317 |
|
| 318 |
windll.kernel32.GetUserDefaultUILanguage.return_value = 0 |
| 319 |
with environ("LANGUAGE", "LANG", "LC_ALL", "LC_CTYPE"): |
| 320 |
|
| 321 |
assert locale.AppriseLocale.detect_language() == "fr" |
| 322 |
|
| 323 |
del ctypes.windll |
| 324 |
|
| 325 |
|
| 326 |
locale.AppriseLocale._default_language = default_language |
| 327 |
|
| 328 |
|
| 329 |
@pytest.mark.skipif(sys.platform == "win32", reason="Unique Nux test cases") |
| 330 |
@mock.patch("locale.getlocale") |
| 331 |
def test_detect_language_using_env(mock_getlocale): |
| 332 |
"""Test the reading of information from an environment variable.""" |
| 333 |
|
| 334 |
|
| 335 |
mock_getlocale.return_value = ("en_CA", "UTF-8") |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
with environ("LANG", "LANGUAGE", "LC_ALL", "LC_CTYPE"): |
| 340 |
|
| 341 |
assert isinstance(locale.AppriseLocale.detect_language(), str) |
| 342 |
|
| 343 |
|
| 344 |
with environ("LANGUAGE", "LC_ALL", LC_CTYPE="garbage", LANG="fr_CA"): |
| 345 |
assert locale.AppriseLocale.detect_language() == "fr" |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
with environ(*list(os.environ.keys()), LC_CTYPE="UTF-8"): |
| 353 |
assert isinstance(locale.AppriseLocale.detect_language(), str) |
| 354 |
|
| 355 |
|
| 356 |
with environ(*list(os.environ.keys())): |
| 357 |
assert isinstance(locale.AppriseLocale.detect_language(), str) |
| 358 |
|
| 359 |
|
| 360 |
mock_getlocale.return_value = None |
| 361 |
with environ("LC_ALL", "LC_CTYPE", "LANG", "LANGUAGE"): |
| 362 |
assert locale.AppriseLocale.detect_language() is None |
| 363 |
|
| 364 |
mock_getlocale.return_value = (None, None) |
| 365 |
with environ("LC_ALL", "LC_CTYPE", "LANG", "LANGUAGE"): |
| 366 |
assert locale.AppriseLocale.detect_language() is None |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
locale.AppriseLocale() |
| 371 |
|
| 372 |
|
| 373 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 374 |
def test_apprise_trans_gettext_missing(tmpdir): |
| 375 |
"""Verify we can still operate without the gettext library.""" |
| 376 |
|
| 377 |
|
| 378 |
del sys.modules["gettext"] |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
gettext_dir = tmpdir.mkdir("gettext") |
| 383 |
gettext_dir.join("__init__.py").write("") |
| 384 |
gettext_dir.join("gettext.py").write("""raise ImportError()""") |
| 385 |
|
| 386 |
|
| 387 |
sys.path.insert(0, str(gettext_dir)) |
| 388 |
|
| 389 |
|
| 390 |
reload(sys.modules["apprise.locale"]) |
| 391 |
from apprise import locale |
| 392 |
|
| 393 |
assert locale.GETTEXT_LOADED is False |
| 394 |
|
| 395 |
|
| 396 |
sys.path.pop(0) |
| 397 |
|
| 398 |
|
| 399 |
reload(sys.modules["apprise.locale"]) |
| 400 |
from apprise import locale |
| 401 |
|
| 402 |
assert locale.GETTEXT_LOADED is True |
| 403 |
|
| 404 |
|
| 405 |
@mock.patch("gettext.translation") |
| 406 |
def test_apprise_locale_add_existing_language(mock_translation): |
| 407 |
"""Re-add an existing language to hit fallback mapping path.""" |
| 408 |
|
| 409 |
dummy = mock.Mock() |
| 410 |
dummy.gettext = lambda x: f"tr:{x}" |
| 411 |
mock_translation.return_value = dummy |
| 412 |
|
| 413 |
al = locale.AppriseLocale() |
| 414 |
assert al.add("en", set_default=True) is True |
| 415 |
assert al.add("en", set_default=False) is True |
| 416 |
|
| 417 |
|
| 418 |
@mock.patch("gettext.translation") |
| 419 |
@mock.patch("locale.getlocale") |
| 420 |
def test_apprise_trans_successful_translation( |
| 421 |
mock_getlocale, mock_translation |
| 422 |
): |
| 423 |
"""Trigger logger.trace by mocking successful gettext.translation()""" |
| 424 |
mock_getlocale.return_value = ("en_CA", "UTF-8") |
| 425 |
|
| 426 |
dummy_translation = mock.Mock() |
| 427 |
dummy_translation.gettext = lambda x: f"tr:{x}" |
| 428 |
mock_translation.return_value = dummy_translation |
| 429 |
|
| 430 |
al = locale.AppriseLocale() |
| 431 |
assert al.add("en") is True |
| 432 |
assert al.gettext("test") == "tr:test" |