summaryrefslogtreecommitdiff
path: root/tests/i18n
diff options
context:
space:
mode:
authorTom Forbes <tom@tomforb.es>2019-01-14 01:33:47 +0000
committerTim Graham <timograham@gmail.com>2019-01-13 20:33:47 -0500
commitc8720e7696ca41f3262d5369365cc1bd72a216ca (patch)
tree1fc858e37415196f06982d2af32f98e29044cde1 /tests/i18n
parenta02a6fd5805f9f0e613b9951249555876b8c4041 (diff)
downloaddjango-c8720e7696ca41f3262d5369365cc1bd72a216ca.tar.gz
Fixed #27685 -- Added watchman support to the autoreloader.
Removed support for pyinotify (refs #9722).
Diffstat (limited to 'tests/i18n')
-rw-r--r--tests/i18n/tests.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py
index 7b54089cf2..2377c8992e 100644
--- a/tests/i18n/tests.py
+++ b/tests/i18n/tests.py
@@ -7,9 +7,12 @@ import re
import tempfile
from contextlib import contextmanager
from importlib import import_module
+from pathlib import Path
from threading import local
from unittest import mock
+import _thread
+
from django import forms
from django.apps import AppConfig
from django.conf import settings
@@ -33,6 +36,9 @@ from django.utils.translation import (
npgettext, npgettext_lazy, pgettext, to_language, to_locale, trans_null,
trans_real, ugettext, ugettext_lazy, ungettext, ungettext_lazy,
)
+from django.utils.translation.reloader import (
+ translation_file_changed, watch_for_translation_changes,
+)
from .forms import CompanyForm, I18nForm, SelectDateForm
from .models import Company, TestModel
@@ -1790,3 +1796,65 @@ class NonDjangoLanguageTests(SimpleTestCase):
def test_plural_non_django_language(self):
self.assertEqual(get_language(), 'xyz')
self.assertEqual(ngettext('year', 'years', 2), 'years')
+
+
+@override_settings(USE_I18N=True)
+class WatchForTranslationChangesTests(SimpleTestCase):
+ @override_settings(USE_I18N=False)
+ def test_i18n_disabled(self):
+ mocked_sender = mock.MagicMock()
+ watch_for_translation_changes(mocked_sender)
+ mocked_sender.watch_dir.assert_not_called()
+
+ def test_i18n_enabled(self):
+ mocked_sender = mock.MagicMock()
+ watch_for_translation_changes(mocked_sender)
+ self.assertGreater(mocked_sender.watch_dir.call_count, 1)
+
+ def test_i18n_locale_paths(self):
+ mocked_sender = mock.MagicMock()
+ with tempfile.TemporaryDirectory() as app_dir:
+ with self.settings(LOCALE_PATHS=[app_dir]):
+ watch_for_translation_changes(mocked_sender)
+ mocked_sender.watch_dir.assert_any_call(Path(app_dir), '**/*.mo')
+
+ def test_i18n_app_dirs(self):
+ mocked_sender = mock.MagicMock()
+ with self.settings(INSTALLED_APPS=['tests.i18n.sampleproject']):
+ watch_for_translation_changes(mocked_sender)
+ project_dir = Path(__file__).parent / 'sampleproject' / 'locale'
+ mocked_sender.watch_dir.assert_any_call(project_dir, '**/*.mo')
+
+ def test_i18n_local_locale(self):
+ mocked_sender = mock.MagicMock()
+ watch_for_translation_changes(mocked_sender)
+ locale_dir = Path(__file__).parent / 'locale'
+ mocked_sender.watch_dir.assert_any_call(locale_dir, '**/*.mo')
+
+
+class TranslationFileChangedTests(SimpleTestCase):
+ def setUp(self):
+ self.gettext_translations = gettext_module._translations.copy()
+ self.trans_real_translations = trans_real._translations.copy()
+
+ def tearDown(self):
+ gettext._translations = self.gettext_translations
+ trans_real._translations = self.trans_real_translations
+
+ def test_ignores_non_mo_files(self):
+ gettext_module._translations = {'foo': 'bar'}
+ path = Path('test.py')
+ self.assertIsNone(translation_file_changed(None, path))
+ self.assertEqual(gettext_module._translations, {'foo': 'bar'})
+
+ def test_resets_cache_with_mo_files(self):
+ gettext_module._translations = {'foo': 'bar'}
+ trans_real._translations = {'foo': 'bar'}
+ trans_real._default = 1
+ trans_real._active = False
+ path = Path('test.mo')
+ self.assertIs(translation_file_changed(None, path), True)
+ self.assertEqual(gettext_module._translations, {})
+ self.assertEqual(trans_real._translations, {})
+ self.assertIsNone(trans_real._default)
+ self.assertIsInstance(trans_real._active, _thread._local)