| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
import ctypes |
| 29 |
from importlib import reload |
| 30 |
|
| 31 |
# Disable logging for a cleaner testing output |
| 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 Apprise, AppriseAsset, locale |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
# True when the catalogs have been compiled (tox -e compile). They are not |
| 45 |
# kept in git and CI does not build them, so add() results depend on this. |
| 46 |
CATALOG_COMPILED = os.path.isfile( |
| 47 |
os.path.join( |
| 48 |
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
| 49 |
"apprise", |
| 50 |
"i18n", |
| 51 |
"en", |
| 52 |
"LC_MESSAGES", |
| 53 |
"apprise.mo", |
| 54 |
) |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
def test_apprise_trans(): |
| 59 |
""" |
| 60 |
API: Test apprise locale object |
| 61 |
""" |
| 62 |
# Text with no translation reads the same in every language |
| 63 |
text = "A string Apprise has no translation for" |
| 64 |
lazytrans = locale.LazyTranslation(text) |
| 65 |
assert str(lazytrans) == text |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 69 |
def test_apprise_trans_gettext_init(): |
| 70 |
""" |
| 71 |
API: Handle gettext |
| 72 |
""" |
| 73 |
# Toggle |
| 74 |
locale.GETTEXT_LOADED = False |
| 75 |
|
| 76 |
# Objects can still be created |
| 77 |
al = locale.AppriseLocale() |
| 78 |
|
| 79 |
with al.lang_at("en") as _: |
| 80 |
# functions still behave as normal |
| 81 |
assert _ is None |
| 82 |
|
| 83 |
# Lazy strings hand back the English source text they were given |
| 84 |
assert str(locale.gettext_lazy("Token")) == "Token" |
| 85 |
|
| 86 |
# Restore the object |
| 87 |
locale.GETTEXT_LOADED = True |
| 88 |
|
| 89 |
|
| 90 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 91 |
@mock.patch("gettext.translation") |
| 92 |
@mock.patch("locale.getlocale") |
| 93 |
def test_apprise_trans_gettext_translations( |
| 94 |
mock_getlocale, mock_gettext_trans |
| 95 |
): |
| 96 |
""" |
| 97 |
API: Apprise() Gettext translations |
| 98 |
|
| 99 |
""" |
| 100 |
|
| 101 |
# Set the gettext locale returned by the mock. |
| 102 |
mock_getlocale.return_value = ("en_US", "UTF-8") |
| 103 |
|
| 104 |
mock_gettext_trans.side_effect = FileNotFoundError() |
| 105 |
|
| 106 |
# This throws internally but we handle it gracefully |
| 107 |
al = locale.AppriseLocale() |
| 108 |
|
| 109 |
with al.lang_at("en"): |
| 110 |
# functions still behave as normal |
| 111 |
pass |
| 112 |
|
| 113 |
# This throws internally but we handle it gracefully |
| 114 |
locale.AppriseLocale(language="fr") |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.skipif(hasattr(ctypes, "windll"), reason="Unique Nux test cases") |
| 118 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 119 |
@mock.patch("locale.getlocale") |
| 120 |
def test_apprise_trans_gettext_lang_at(mock_getlocale): |
| 121 |
""" |
| 122 |
API: Apprise() Gettext lang_at |
| 123 |
|
| 124 |
""" |
| 125 |
|
| 126 |
# Set the gettext locale returned by the mock. |
| 127 |
mock_getlocale.return_value = ("en_CA", "UTF-8") |
| 128 |
|
| 129 |
# Clear the language variables so only the mock above is detected |
| 130 |
with environ("LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", "LANG"): |
| 131 |
# This throws internally but we handle it gracefully |
| 132 |
al = locale.AppriseLocale() |
| 133 |
|
| 134 |
# We detected en_CA, so plain en needs a compiled catalog |
| 135 |
assert al.add("en", set_default=False) is CATALOG_COMPILED |
| 136 |
assert al.add("en", set_default=True) is CATALOG_COMPILED |
| 137 |
|
| 138 |
with al.lang_at("en"): |
| 139 |
# functions still behave as normal |
| 140 |
pass |
| 141 |
|
| 142 |
# This throws internally but we handle it gracefully |
| 143 |
locale.AppriseLocale(language="fr") |
| 144 |
|
| 145 |
with al.lang_at("en") as _: |
| 146 |
# functions still behave as normal |
| 147 |
assert callable(_) |
| 148 |
|
| 149 |
with al.lang_at("es") as _: |
| 150 |
# functions still behave as normal |
| 151 |
assert callable(_) |
| 152 |
|
| 153 |
with al.lang_at("fr") as _: |
| 154 |
# functions still behave as normal |
| 155 |
assert callable(_) |
| 156 |
|
| 157 |
# Test our initialization when our fallback is a language we do |
| 158 |
# not have. This is only done to test edge cases when for whatever |
| 159 |
# reason the person who set up apprise does not have the languages |
| 160 |
# installed. |
| 161 |
fallback = locale.AppriseLocale._default_language |
| 162 |
mock_getlocale.return_value = None |
| 163 |
|
| 164 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 165 |
# Our default language |
| 166 |
locale.AppriseLocale._default_language = "zz" |
| 167 |
|
| 168 |
# We will detect the zz since there were no environment variables to |
| 169 |
# help us otherwise |
| 170 |
assert locale.AppriseLocale.detect_language() is None |
| 171 |
al = locale.AppriseLocale() |
| 172 |
|
| 173 |
# No language can be set because its locale directory does not exist. |
| 174 |
assert al.lang is None |
| 175 |
|
| 176 |
with al.lang_at(None) as _: |
| 177 |
# functions still behave as normal |
| 178 |
assert callable(_) |
| 179 |
|
| 180 |
with al.lang_at("en") as _: |
| 181 |
# functions still behave as normal |
| 182 |
assert callable(_) |
| 183 |
|
| 184 |
with al.lang_at("es") as _: |
| 185 |
# functions still behave as normal |
| 186 |
assert callable(_) |
| 187 |
|
| 188 |
with al.lang_at("fr") as _: |
| 189 |
# functions still behave as normal |
| 190 |
assert callable(_) |
| 191 |
|
| 192 |
# We can still perform simple lookups; they access a dummy wrapper: |
| 193 |
assert al.gettext("test") == "test" |
| 194 |
|
| 195 |
locale.AppriseLocale._default_language = "fr" |
| 196 |
|
| 197 |
with environ( |
| 198 |
"LANGUAGE", |
| 199 |
"LC_MESSAGES", |
| 200 |
"LC_CTYPE", |
| 201 |
LC_ALL="C.UTF-8", |
| 202 |
LANG="en_CA", |
| 203 |
): |
| 204 |
# LC_ALL is authoritative, and the C locale it names is a request for |
| 205 |
# no translation at all. LANG is not consulted, and we settle on the |
| 206 |
# language configured as our default. |
| 207 |
assert locale.AppriseLocale.detect_language() == "fr" |
| 208 |
|
| 209 |
with environ( |
| 210 |
"LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="en_CA" |
| 211 |
): |
| 212 |
# With no C locale in the way, LANG decides and overrides the |
| 213 |
# default. The region is kept so that an en_CA translation is |
| 214 |
# preferred over the en translation. |
| 215 |
assert locale.AppriseLocale.detect_language() == "en_CA" |
| 216 |
al = locale.AppriseLocale() |
| 217 |
assert al.lang == "en_CA" |
| 218 |
assert al.gettext("test") == "test" |
| 219 |
|
| 220 |
# Test case with set_default set to False (so we're still set to 'fr') |
| 221 |
assert al.add("zy", set_default=False) is False |
| 222 |
assert al.gettext("test") == "test" |
| 223 |
|
| 224 |
al.add("ab", set_default=True) |
| 225 |
assert al.gettext("test") == "test" |
| 226 |
|
| 227 |
assert al.add("zy", set_default=False) is False |
| 228 |
locale.AppriseLocale._default_language = fallback |
| 229 |
|
| 230 |
|
| 231 |
def test_normalize_language(): |
| 232 |
"""Both the 2 and 5 letter language forms are accepted.""" |
| 233 |
|
| 234 |
# A plain language stays as it is, and case never matters |
| 235 |
for entry in ("en", "EN", " en "): |
| 236 |
assert locale.AppriseLocale.normalize_language(entry) == "en" |
| 237 |
|
| 238 |
# A region may be separated by a hyphen or an underscore |
| 239 |
for entry in ("en-CA", "en_CA", "EN-ca", "en-ca"): |
| 240 |
assert locale.AppriseLocale.normalize_language(entry) == "en_CA" |
| 241 |
|
| 242 |
# A codeset (.UTF-8) and modifier (@euro) are both dropped |
| 243 |
for entry in ( |
| 244 |
"en_CA.UTF-8", |
| 245 |
"en_CA.utf8", |
| 246 |
"en_CA.ISO8859-1", |
| 247 |
"en_CA.ANSI_X3.4-1968", |
| 248 |
"en_CA@euro", |
| 249 |
"en_CA.UTF-8@euro", |
| 250 |
): |
| 251 |
assert locale.AppriseLocale.normalize_language(entry) == "en_CA" |
| 252 |
|
| 253 |
# A modifier is dropped even without a codeset |
| 254 |
assert locale.AppriseLocale.normalize_language("sr_RS@latin") == "sr_RS" |
| 255 |
assert locale.AppriseLocale.normalize_language("fr@euro") == "fr" |
| 256 |
|
| 257 |
# The form locale.normalize() returns for ca_ES@valencia |
| 258 |
assert ( |
| 259 |
locale.AppriseLocale.normalize_language("ca_ES.UTF-8@valencia") |
| 260 |
== "ca_ES" |
| 261 |
) |
| 262 |
|
| 263 |
# A codeset or modifier on its own is not a language |
| 264 |
for entry in (".UTF-8", "@euro", "garbage.stuff_more"): |
| 265 |
assert locale.AppriseLocale.normalize_language(entry) is None |
| 266 |
|
| 267 |
# Anything beyond a 2 letter region is dropped, leaving the language |
| 268 |
assert locale.AppriseLocale.normalize_language("zh-Hans") == "zh" |
| 269 |
assert locale.AppriseLocale.normalize_language("zh-Hans-CN") == "zh" |
| 270 |
|
| 271 |
# Only the first entry of a preference list is honored, whether it is |
| 272 |
# comma separated like an Accept-Language header or colon separated like |
| 273 |
# a LANGUAGE= environment variable |
| 274 |
assert ( |
| 275 |
locale.AppriseLocale.normalize_language("en-US,en;q=0.9,de;q=0.5") |
| 276 |
== "en_US" |
| 277 |
) |
| 278 |
assert locale.AppriseLocale.normalize_language("fr:de") == "fr" |
| 279 |
|
| 280 |
# A colon is a list separator, so it never introduces a region |
| 281 |
assert locale.AppriseLocale.normalize_language("en:CA") == "en" |
| 282 |
|
| 283 |
# Values we can not work with |
| 284 |
for entry in ("", "garbage", "!", "C", "C.UTF-8", None, 42): |
| 285 |
assert locale.AppriseLocale.normalize_language(entry) is None |
| 286 |
|
| 287 |
|
| 288 |
def test_is_ansii_locale(): |
| 289 |
"""The C locale is recognized under each of its spellings.""" |
| 290 |
|
| 291 |
# Every spelling, with or without a codeset or modifier |
| 292 |
for entry in ( |
| 293 |
"C", |
| 294 |
"c", |
| 295 |
"POSIX", |
| 296 |
"posix", |
| 297 |
"C.UTF-8", |
| 298 |
"C.utf8", |
| 299 |
"C.ANSI_X3.4-1968", |
| 300 |
"POSIX.UTF-8", |
| 301 |
"C@euro", |
| 302 |
): |
| 303 |
assert locale.AppriseLocale.is_ansii_locale(entry) is True |
| 304 |
|
| 305 |
# A language is not the C locale, including the ones that start with a c |
| 306 |
for entry in ("en", "ca", "cs", "ca_ES", "cat", "", None, 42): |
| 307 |
assert locale.AppriseLocale.is_ansii_locale(entry) is False |
| 308 |
|
| 309 |
|
| 310 |
def test_language_candidates(): |
| 311 |
"""A region is always retried without it before we give up.""" |
| 312 |
|
| 313 |
# A plain language has nowhere else to look |
| 314 |
assert locale.AppriseLocale.language_candidates("fr") == ["fr"] |
| 315 |
|
| 316 |
# A region is preferred, but the language itself is the backup |
| 317 |
assert locale.AppriseLocale.language_candidates("pt-BR") == [ |
| 318 |
"pt_BR", |
| 319 |
"pt", |
| 320 |
] |
| 321 |
|
| 322 |
# Nothing to search for |
| 323 |
assert locale.AppriseLocale.language_candidates("garbage") == [] |
| 324 |
|
| 325 |
|
| 326 |
@pytest.mark.skipif(sys.platform == "win32", reason="Unique Nux test cases") |
| 327 |
@mock.patch("locale.getlocale") |
| 328 |
def test_detect_language_precedence(mock_getlocale): |
| 329 |
"""The environment is read in the order gettext itself reads it.""" |
| 330 |
|
| 331 |
# Never reached while an environment variable answers for us |
| 332 |
mock_getlocale.return_value = (None, None) |
| 333 |
|
| 334 |
with environ( |
| 335 |
"LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="en_CA" |
| 336 |
): |
| 337 |
# LANG on its own |
| 338 |
assert locale.AppriseLocale.detect_language() == "en_CA" |
| 339 |
|
| 340 |
with environ( |
| 341 |
"LANGUAGE", "LC_ALL", "LC_CTYPE", LANG="en_CA", LC_MESSAGES="fr_CA" |
| 342 |
): |
| 343 |
# LC_MESSAGES is the category that governs translated text |
| 344 |
assert locale.AppriseLocale.detect_language() == "fr_CA" |
| 345 |
|
| 346 |
with environ( |
| 347 |
"LANGUAGE", |
| 348 |
"LC_CTYPE", |
| 349 |
LANG="en_CA", |
| 350 |
LC_MESSAGES="fr_CA", |
| 351 |
LC_ALL="de_DE", |
| 352 |
): |
| 353 |
# LC_ALL over-rides every category |
| 354 |
assert locale.AppriseLocale.detect_language() == "de_DE" |
| 355 |
|
| 356 |
with environ( |
| 357 |
"LC_CTYPE", |
| 358 |
LANG="en_CA", |
| 359 |
LC_MESSAGES="fr_CA", |
| 360 |
LC_ALL="de_DE", |
| 361 |
LANGUAGE="es", |
| 362 |
): |
| 363 |
# LANGUAGE wins over all of them |
| 364 |
assert locale.AppriseLocale.detect_language() == "es" |
| 365 |
|
| 366 |
with environ( |
| 367 |
"LANGUAGE", "LC_ALL", "LC_MESSAGES", LANG="en_CA", LC_CTYPE="de_DE" |
| 368 |
): |
| 369 |
# LC_CTYPE governs character handling, not the language of text |
| 370 |
assert locale.AppriseLocale.detect_language() == "en_CA" |
| 371 |
|
| 372 |
with environ( |
| 373 |
"LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="en_CA", LANGUAGE="fr:de" |
| 374 |
): |
| 375 |
# A LANGUAGE list is colon separated; its first entry is used |
| 376 |
assert locale.AppriseLocale.detect_language() == "fr" |
| 377 |
|
| 378 |
with environ( |
| 379 |
"LANGUAGE", |
| 380 |
"LC_ALL", |
| 381 |
"LC_MESSAGES", |
| 382 |
"LC_CTYPE", |
| 383 |
LANG="fr_FR.ISO8859-1@euro", |
| 384 |
): |
| 385 |
# The codeset and modifier are dropped |
| 386 |
assert locale.AppriseLocale.detect_language() == "fr_FR" |
| 387 |
|
| 388 |
default = locale.AppriseLocale._default_language |
| 389 |
for value in ( |
| 390 |
"C", |
| 391 |
"POSIX", |
| 392 |
"C.UTF-8", |
| 393 |
"posix", |
| 394 |
"C.ANSI_X3.4-1968", |
| 395 |
"C@euro", |
| 396 |
): |
| 397 |
with environ( |
| 398 |
"LC_MESSAGES", |
| 399 |
"LC_CTYPE", |
| 400 |
LC_ALL=value, |
| 401 |
LANG="fr_FR.UTF-8", |
| 402 |
LANGUAGE="de", |
| 403 |
): |
| 404 |
# Asking for the C locale is asking for untranslated text, so |
| 405 |
# nothing below LC_ALL gets a say, LANGUAGE included |
| 406 |
assert locale.AppriseLocale.detect_language() == default |
| 407 |
|
| 408 |
with environ("LC_ALL", "LC_CTYPE", LANG="C", LC_MESSAGES="fr_CA"): |
| 409 |
# A higher priority category still decides; only the locale actually |
| 410 |
# in charge of messages can silence translation |
| 411 |
assert locale.AppriseLocale.detect_language() == "fr_CA" |
| 412 |
|
| 413 |
with environ( |
| 414 |
"LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANGUAGE="garbage", LANG="fr_CA" |
| 415 |
): |
| 416 |
# A value we can make no sense of is stepped over rather than |
| 417 |
# stopping us; only the C locale does that |
| 418 |
assert locale.AppriseLocale.detect_language() == "fr_CA" |
| 419 |
|
| 420 |
|
| 421 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 422 |
def test_lang_at_invalid_entries(): |
| 423 |
"""An unusable language falls back instead of raising.""" |
| 424 |
|
| 425 |
al = locale.AppriseLocale() |
| 426 |
|
| 427 |
# None of these can be turned into a language, yet each still hands |
| 428 |
# back a usable translation function |
| 429 |
for entry in (None, "", " ", "garbage", "!", 42): |
| 430 |
with al.lang_at(entry) as fn: |
| 431 |
assert callable(fn) |
| 432 |
|
| 433 |
# The same holds true for the public details() call |
| 434 |
obj = Apprise() |
| 435 |
assert isinstance(obj.details(lang="invalid!"), dict) |
| 436 |
|
| 437 |
|
| 438 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 439 |
@mock.patch("gettext.translation") |
| 440 |
def test_lazy_translation_context(mock_translation): |
| 441 |
"""A lang_at() block decides how lazy strings are translated.""" |
| 442 |
|
| 443 |
# Our stand-in catalog marks anything it translates |
| 444 |
dummy = mock.Mock() |
| 445 |
dummy.gettext = lambda text: f"xx:{text}" |
| 446 |
mock_translation.return_value = dummy |
| 447 |
|
| 448 |
al = locale.AppriseLocale() |
| 449 |
entry = locale.gettext_lazy("Token") |
| 450 |
|
| 451 |
# Our own singleton is untouched outside of the block |
| 452 |
before = str(entry) |
| 453 |
|
| 454 |
with al.lang_at("fr"): |
| 455 |
assert str(entry) == "xx:Token" |
| 456 |
|
| 457 |
# The language in effect before us is restored |
| 458 |
assert str(entry) == before |
| 459 |
|
| 460 |
# Asking for a function other than gettext leaves lazy strings alone |
| 461 |
with al.lang_at("fr", mapto="ngettext"): |
| 462 |
assert str(entry) == before |
| 463 |
|
| 464 |
|
| 465 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 466 |
@mock.patch("gettext.translation") |
| 467 |
def test_asset_language_honored(mock_translation): |
| 468 |
"""An asset language is used by the details() it returns.""" |
| 469 |
|
| 470 |
# Our stand-in catalog marks anything it translates |
| 471 |
dummy = mock.Mock() |
| 472 |
dummy.gettext = lambda text: f"xx:{text}" |
| 473 |
mock_translation.return_value = dummy |
| 474 |
|
| 475 |
obj = Apprise(asset=AppriseAsset(language="fr-CA")) |
| 476 |
|
| 477 |
# Our asset decided the language of the object we built |
| 478 |
assert obj.locale.lang == "fr_CA" |
| 479 |
|
| 480 |
# Every human readable entry came back translated |
| 481 |
names = [ |
| 482 |
token["name"] |
| 483 |
for schema in obj.details()["schemas"] |
| 484 |
for token in schema["details"]["tokens"].values() |
| 485 |
] |
| 486 |
assert names |
| 487 |
assert all(str(name).startswith("xx:") for name in names) |
| 488 |
|
| 489 |
|
| 490 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 491 |
@mock.patch("locale.getlocale") |
| 492 |
def test_apprise_trans_add(mock_getlocale): |
| 493 |
""" |
| 494 |
API: Apprise() Gettext add |
| 495 |
|
| 496 |
""" |
| 497 |
# Used when no language variable is set. Windows asks the system |
| 498 |
# first, but always gets a regional answer (en_US), so the checks below |
| 499 |
# still hold. |
| 500 |
mock_getlocale.return_value = ("en_CA", "UTF-8") |
| 501 |
|
| 502 |
with environ("LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", "LANG"): |
| 503 |
# We start on en_CA, so plain en needs a compiled catalog |
| 504 |
al = locale.AppriseLocale() |
| 505 |
assert al.add("en") is CATALOG_COMPILED |
| 506 |
|
| 507 |
with environ( |
| 508 |
"LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="C.UTF-8" |
| 509 |
): |
| 510 |
# The C locale starts on our default language, so en is loaded |
| 511 |
al = locale.AppriseLocale() |
| 512 |
assert al.lang == locale.AppriseLocale._default_language |
| 513 |
assert al.add("en") is True |
| 514 |
|
| 515 |
with environ( |
| 516 |
"LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="en_CA.UTF-8" |
| 517 |
): |
| 518 |
# Test English Environment |
| 519 |
al = locale.AppriseLocale() |
| 520 |
assert al.add("en") is CATALOG_COMPILED |
| 521 |
|
| 522 |
# Double add (copy of above) to access logic that prevents adding it |
| 523 |
# again |
| 524 |
assert al.add("en") is CATALOG_COMPILED |
| 525 |
|
| 526 |
# Invalid Language |
| 527 |
assert al.add("bad") is False |
| 528 |
|
| 529 |
# A value that is not a language at all never reaches gettext; there is |
| 530 |
# no catalog name that could be built from it |
| 531 |
for entry in ("C", "!", "1"): |
| 532 |
assert al.add(entry) is False |
| 533 |
|
| 534 |
# An empty value is not a failure; it selects the default language |
| 535 |
assert al.add("") is CATALOG_COMPILED |
| 536 |
|
| 537 |
|
| 538 |
@pytest.mark.skipif( |
| 539 |
not hasattr(ctypes, "windll"), reason="Unique Windows test cases" |
| 540 |
) |
| 541 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 542 |
@mock.patch("locale.getlocale") |
| 543 |
def test_apprise_trans_windows_users_win(mock_getlocale): |
| 544 |
""" |
| 545 |
API: Apprise() Windows Locale Testing (Win version) |
| 546 |
|
| 547 |
""" |
| 548 |
|
| 549 |
# Set the gettext locale returned by the mock. |
| 550 |
mock_getlocale.return_value = ("fr_CA", "UTF-8") |
| 551 |
|
| 552 |
with mock.patch( |
| 553 |
"ctypes.windll.kernel32.GetUserDefaultUILanguage" |
| 554 |
) as ui_lang: |
| 555 |
# 4105 = en_CA |
| 556 |
ui_lang.return_value = 4105 |
| 557 |
|
| 558 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 559 |
# Our default language |
| 560 |
locale.AppriseLocale._default_language = "zz" |
| 561 |
|
| 562 |
# The Windows locale supplies English. |
| 563 |
assert locale.AppriseLocale.detect_language() == "en_CA" |
| 564 |
|
| 565 |
# Environment variables take precedence over the Windows locale. |
| 566 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", LANG="es_AR"): |
| 567 |
assert locale.AppriseLocale.detect_language() == "es_AR" |
| 568 |
|
| 569 |
# Without environment variables, use the Windows locale. |
| 570 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 571 |
assert locale.AppriseLocale.detect_language() == "en_CA" |
| 572 |
|
| 573 |
assert ( |
| 574 |
locale.AppriseLocale.detect_language(detect_fallback=False) is None |
| 575 |
) |
| 576 |
|
| 577 |
# 0 = IndexError |
| 578 |
ui_lang.return_value = 0 |
| 579 |
with environ("LANGUAGE", "LANG", "LC_ALL", "LC_CTYPE"): |
| 580 |
# Fall back to the POSIX locale. |
| 581 |
assert locale.AppriseLocale.detect_language() == "fr_CA" |
| 582 |
|
| 583 |
|
| 584 |
@pytest.mark.skipif(hasattr(ctypes, "windll"), reason="Unique Nux test cases") |
| 585 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 586 |
@mock.patch("locale.getlocale") |
| 587 |
def test_apprise_trans_windows_users_nux(mock_getlocale): |
| 588 |
""" |
| 589 |
API: Apprise() Windows Locale Testing (Nux version) |
| 590 |
|
| 591 |
""" |
| 592 |
|
| 593 |
# Set the gettext locale returned by the mock. |
| 594 |
mock_getlocale.return_value = ("fr_CA", "UTF-8") |
| 595 |
|
| 596 |
# Emulate a windows environment |
| 597 |
windll = mock.Mock() |
| 598 |
ctypes.windll = windll |
| 599 |
|
| 600 |
# 4105 = en_CA |
| 601 |
windll.kernel32.GetUserDefaultUILanguage.return_value = 4105 |
| 602 |
|
| 603 |
# Store default value to not break other tests |
| 604 |
default_language = locale.AppriseLocale._default_language |
| 605 |
|
| 606 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 607 |
# Our default language |
| 608 |
locale.AppriseLocale._default_language = "zz" |
| 609 |
|
| 610 |
# The Windows locale supplies English. |
| 611 |
assert locale.AppriseLocale.detect_language() == "en_CA" |
| 612 |
|
| 613 |
# Environment variables take precedence over the Windows locale. |
| 614 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", LANG="es_AR"): |
| 615 |
assert locale.AppriseLocale.detect_language() == "es_AR" |
| 616 |
|
| 617 |
# Without environment variables, use the Windows locale. |
| 618 |
with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): |
| 619 |
assert locale.AppriseLocale.detect_language() == "en_CA" |
| 620 |
|
| 621 |
assert locale.AppriseLocale.detect_language(detect_fallback=False) is None |
| 622 |
|
| 623 |
# 0 = IndexError |
| 624 |
windll.kernel32.GetUserDefaultUILanguage.return_value = 0 |
| 625 |
with environ("LANGUAGE", "LANG", "LC_ALL", "LC_CTYPE"): |
| 626 |
# Fall back to the POSIX locale. |
| 627 |
assert locale.AppriseLocale.detect_language() == "fr_CA" |
| 628 |
|
| 629 |
del ctypes.windll |
| 630 |
|
| 631 |
# Restore default value |
| 632 |
locale.AppriseLocale._default_language = default_language |
| 633 |
|
| 634 |
|
| 635 |
@pytest.mark.skipif(sys.platform == "win32", reason="Unique Nux test cases") |
| 636 |
@mock.patch("locale.getlocale") |
| 637 |
def test_detect_language_using_env(mock_getlocale): |
| 638 |
"""Test the reading of information from an environment variable.""" |
| 639 |
|
| 640 |
# Set the gettext locale returned by the mock. |
| 641 |
mock_getlocale.return_value = ("en_CA", "UTF-8") |
| 642 |
|
| 643 |
# The below accesses the windows fallback code and fail |
| 644 |
# then it will resort to the environment variables. |
| 645 |
with environ("LANG", "LANGUAGE", "LC_ALL", "LC_CTYPE"): |
| 646 |
# Language can now be detected in this case |
| 647 |
assert isinstance(locale.AppriseLocale.detect_language(), str) |
| 648 |
|
| 649 |
# Detect French language. |
| 650 |
with environ("LANGUAGE", "LC_ALL", LC_CTYPE="garbage", LANG="fr_CA"): |
| 651 |
assert locale.AppriseLocale.detect_language() == "fr_CA" |
| 652 |
|
| 653 |
# A system reporting the C locale falls back to our default language |
| 654 |
mock_getlocale.return_value = ("C", "UTF-8") |
| 655 |
with environ("LANG", "LANGUAGE", "LC_ALL", "LC_CTYPE"): |
| 656 |
assert ( |
| 657 |
locale.AppriseLocale.detect_language() |
| 658 |
== locale.AppriseLocale._default_language |
| 659 |
) |
| 660 |
|
| 661 |
# Restore what the rest of this test expects |
| 662 |
mock_getlocale.return_value = ("en_CA", "UTF-8") |
| 663 |
|
| 664 |
# The following unsets all environment variables and sets LC_CTYPE |
| 665 |
# This was causing Python 2.7 to internally parse UTF-8 as an invalid |
| 666 |
# locale and throw an uncaught ValueError; Python v2 support has been |
| 667 |
# dropped, but just to ensure this issue does not come back, we keep |
| 668 |
# this test: |
| 669 |
with environ(*list(os.environ.keys()), LC_CTYPE="UTF-8"): |
| 670 |
assert isinstance(locale.AppriseLocale.detect_language(), str) |
| 671 |
|
| 672 |
# Test with no environment variables. |
| 673 |
with environ(*list(os.environ.keys())): |
| 674 |
assert isinstance(locale.AppriseLocale.detect_language(), str) |
| 675 |
|
| 676 |
# Handle case where getlocale() can't be detected |
| 677 |
mock_getlocale.return_value = None |
| 678 |
with environ("LC_ALL", "LC_CTYPE", "LANG", "LANGUAGE"): |
| 679 |
assert locale.AppriseLocale.detect_language() is None |
| 680 |
|
| 681 |
mock_getlocale.return_value = (None, None) |
| 682 |
with environ("LC_ALL", "LC_CTYPE", "LANG", "LANGUAGE"): |
| 683 |
assert locale.AppriseLocale.detect_language() is None |
| 684 |
|
| 685 |
# if detect_language and windows env fail us, then we don't |
| 686 |
# set up a default language on first load |
| 687 |
locale.AppriseLocale() |
| 688 |
|
| 689 |
|
| 690 |
@pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") |
| 691 |
def test_apprise_trans_gettext_missing(tmpdir): |
| 692 |
"""Verify we can still operate without the gettext library.""" |
| 693 |
|
| 694 |
# Reloading a module rebinds every class it defines. Anything that |
| 695 |
# imported one of them earlier keeps the original, and the two no longer |
| 696 |
# compare as the same object, which breaks isinstance() checks and |
| 697 |
# pickling for the rest of the run. Only what Apprise hands out by |
| 698 |
# reference is restored afterwards; the modules this one imports are |
| 699 |
# left as the reload found them so they can still be mocked. |
| 700 |
restore = { |
| 701 |
name: getattr(sys.modules["apprise.locale"], name) |
| 702 |
for name in ( |
| 703 |
"ACTIVE_GETTEXT", |
| 704 |
"AppriseLocale", |
| 705 |
"LOCALE", |
| 706 |
"LazyTranslation", |
| 707 |
"Translatable", |
| 708 |
"gettext_lazy", |
| 709 |
) |
| 710 |
} |
| 711 |
|
| 712 |
# remove gettext from our system enviroment |
| 713 |
del sys.modules["gettext"] |
| 714 |
|
| 715 |
try: |
| 716 |
# Put a failing fake gettext module ahead of the real one. |
| 717 |
gettext_dir = tmpdir.mkdir("gettext") |
| 718 |
gettext_dir.join("__init__.py").write("") |
| 719 |
gettext_dir.join("gettext.py").write("""raise ImportError()""") |
| 720 |
|
| 721 |
# Update our path to point path to head |
| 722 |
sys.path.insert(0, str(gettext_dir)) |
| 723 |
|
| 724 |
# reload our module (forcing the import error when it tries to load |
| 725 |
# gettext |
| 726 |
reload(sys.modules["apprise.locale"]) |
| 727 |
from apprise import locale |
| 728 |
|
| 729 |
assert locale.GETTEXT_LOADED is False |
| 730 |
|
| 731 |
# Now roll our changes back |
| 732 |
sys.path.pop(0) |
| 733 |
|
| 734 |
# Reload again (reverting back) |
| 735 |
reload(sys.modules["apprise.locale"]) |
| 736 |
from apprise import locale |
| 737 |
|
| 738 |
assert locale.GETTEXT_LOADED is True |
| 739 |
|
| 740 |
finally: |
| 741 |
# Hand back the classes the rest of Apprise is already holding |
| 742 |
module = sys.modules["apprise.locale"] |
| 743 |
for name, value in restore.items(): |
| 744 |
setattr(module, name, value) |
| 745 |
|
| 746 |
|
| 747 |
@mock.patch("gettext.translation") |
| 748 |
def test_apprise_locale_add_existing_language(mock_translation): |
| 749 |
"""Re-add an existing language to hit fallback mapping path.""" |
| 750 |
# Return a fake gettext object with a valid gettext() method |
| 751 |
dummy = mock.Mock() |
| 752 |
dummy.gettext = lambda x: f"tr:{x}" |
| 753 |
mock_translation.return_value = dummy |
| 754 |
|
| 755 |
al = locale.AppriseLocale() |
| 756 |
assert al.add("en", set_default=True) is True |
| 757 |
assert al.add("en", set_default=False) is True |
| 758 |
|
| 759 |
|
| 760 |
@mock.patch("gettext.translation") |
| 761 |
@mock.patch("locale.getlocale") |
| 762 |
def test_apprise_trans_successful_translation( |
| 763 |
mock_getlocale, mock_translation |
| 764 |
): |
| 765 |
"""Trigger logger.trace by mocking successful gettext.translation()""" |
| 766 |
mock_getlocale.return_value = ("en_CA", "UTF-8") |
| 767 |
|
| 768 |
dummy_translation = mock.Mock() |
| 769 |
dummy_translation.gettext = lambda x: f"tr:{x}" |
| 770 |
mock_translation.return_value = dummy_translation |
| 771 |
|
| 772 |
al = locale.AppriseLocale() |
| 773 |
assert al.add("en") is True |
| 774 |
assert al.gettext("test") == "tr:test" |