2013-11-02 17:28:22 +08:00
|
|
|
import os
|
2015-08-05 17:06:26 +08:00
|
|
|
import shutil
|
2014-07-25 21:32:14 +08:00
|
|
|
import tempfile
|
2015-01-28 20:35:27 +08:00
|
|
|
from importlib import import_module
|
2013-11-02 17:28:22 +08:00
|
|
|
|
|
|
|
from django import conf
|
2013-11-05 21:30:31 +08:00
|
|
|
from django.contrib import admin
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-02-22 02:18:54 +08:00
|
|
|
from django.test.utils import extend_sys_path
|
2015-08-05 17:06:26 +08:00
|
|
|
from django.utils import autoreload
|
2015-02-15 08:47:07 +08:00
|
|
|
from django.utils._os import npath, upath
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.autoreload import gen_filenames
|
2013-11-02 17:28:22 +08:00
|
|
|
|
|
|
|
LOCALE_PATH = os.path.join(os.path.dirname(__file__), 'locale')
|
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class TestFilenameGenerator(SimpleTestCase):
|
2014-07-05 20:43:12 +08:00
|
|
|
def setUp(self):
|
|
|
|
# Empty cached variables
|
|
|
|
from django.utils import autoreload
|
|
|
|
autoreload._cached_modules = set()
|
|
|
|
autoreload._cached_filenames = []
|
|
|
|
|
2013-11-02 17:28:22 +08:00
|
|
|
def test_django_locales(self):
|
|
|
|
"""
|
|
|
|
Test that gen_filenames() also yields the built-in django locale files.
|
|
|
|
"""
|
|
|
|
filenames = list(gen_filenames())
|
2013-11-05 21:30:31 +08:00
|
|
|
self.assertIn(os.path.join(os.path.dirname(conf.__file__), 'locale',
|
|
|
|
'nl', 'LC_MESSAGES', 'django.mo'),
|
|
|
|
filenames)
|
2013-11-02 17:28:22 +08:00
|
|
|
|
2015-01-22 00:55:57 +08:00
|
|
|
@override_settings(LOCALE_PATHS=[LOCALE_PATH])
|
2013-11-05 21:30:31 +08:00
|
|
|
def test_locale_paths_setting(self):
|
|
|
|
"""
|
|
|
|
Test that gen_filenames also yields from LOCALE_PATHS locales.
|
|
|
|
"""
|
|
|
|
filenames = list(gen_filenames())
|
|
|
|
self.assertIn(os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo'),
|
|
|
|
filenames)
|
2013-11-02 17:28:22 +08:00
|
|
|
|
2013-12-23 17:37:34 +08:00
|
|
|
@override_settings(INSTALLED_APPS=[])
|
2013-11-05 21:30:31 +08:00
|
|
|
def test_project_root_locale(self):
|
|
|
|
"""
|
|
|
|
Test that gen_filenames also yields from the current directory (project
|
|
|
|
root).
|
|
|
|
"""
|
|
|
|
old_cwd = os.getcwd()
|
|
|
|
os.chdir(os.path.dirname(__file__))
|
|
|
|
try:
|
2013-12-23 17:37:34 +08:00
|
|
|
filenames = list(gen_filenames())
|
2013-11-05 21:30:31 +08:00
|
|
|
self.assertIn(
|
|
|
|
os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo'),
|
|
|
|
filenames)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_cwd)
|
2013-11-02 17:28:22 +08:00
|
|
|
|
2013-12-23 17:37:34 +08:00
|
|
|
@override_settings(INSTALLED_APPS=['django.contrib.admin'])
|
2013-11-02 17:28:22 +08:00
|
|
|
def test_app_locales(self):
|
|
|
|
"""
|
2013-12-20 05:33:46 +08:00
|
|
|
Test that gen_filenames also yields from locale dirs in installed apps.
|
2013-11-02 17:28:22 +08:00
|
|
|
"""
|
2013-12-23 17:37:34 +08:00
|
|
|
filenames = list(gen_filenames())
|
2015-02-15 08:47:07 +08:00
|
|
|
self.assertIn(
|
|
|
|
os.path.join(os.path.dirname(upath(admin.__file__)), 'locale', 'nl', 'LC_MESSAGES', 'django.mo'),
|
|
|
|
filenames
|
|
|
|
)
|
2013-11-05 21:30:31 +08:00
|
|
|
|
|
|
|
@override_settings(USE_I18N=False)
|
|
|
|
def test_no_i18n(self):
|
|
|
|
"""
|
|
|
|
If i18n machinery is disabled, there is no need for watching the
|
|
|
|
locale files.
|
|
|
|
"""
|
|
|
|
filenames = list(gen_filenames())
|
|
|
|
self.assertNotIn(
|
2015-02-15 08:47:07 +08:00
|
|
|
os.path.join(os.path.dirname(upath(conf.__file__)), 'locale', 'nl', 'LC_MESSAGES', 'django.mo'),
|
|
|
|
filenames
|
|
|
|
)
|
2014-07-05 20:43:12 +08:00
|
|
|
|
|
|
|
def test_only_new_files(self):
|
|
|
|
"""
|
|
|
|
When calling a second time gen_filenames with only_new = True, only
|
|
|
|
files from newly loaded modules should be given.
|
|
|
|
"""
|
2014-07-08 07:12:39 +08:00
|
|
|
list(gen_filenames())
|
|
|
|
from fractions import Fraction # NOQA
|
2014-07-05 20:43:12 +08:00
|
|
|
filenames2 = list(gen_filenames(only_new=True))
|
|
|
|
self.assertEqual(len(filenames2), 1)
|
|
|
|
self.assertTrue(filenames2[0].endswith('fractions.py'))
|
2014-07-15 03:40:55 +08:00
|
|
|
self.assertFalse(any(f.endswith('.pyc') for f in gen_filenames()))
|
2014-07-25 21:32:14 +08:00
|
|
|
|
|
|
|
def test_deleted_removed(self):
|
2015-02-22 02:18:54 +08:00
|
|
|
dirname = tempfile.mkdtemp()
|
|
|
|
filename = os.path.join(dirname, 'test_deleted_removed_module.py')
|
|
|
|
with open(filename, 'w'):
|
|
|
|
pass
|
|
|
|
with extend_sys_path(dirname):
|
|
|
|
import_module('test_deleted_removed_module')
|
|
|
|
self.assertIn(npath(filename), gen_filenames())
|
|
|
|
os.unlink(filename)
|
|
|
|
self.assertNotIn(filename, gen_filenames())
|
2015-08-05 17:06:26 +08:00
|
|
|
|
|
|
|
def test_check_errors(self):
|
|
|
|
"""
|
|
|
|
When a file containing an error is imported in a function wrapped by
|
|
|
|
check_errors(), gen_filenames() returns it.
|
|
|
|
|
|
|
|
Run assertions twice to test uncached and cached access.
|
|
|
|
"""
|
|
|
|
dirname = tempfile.mkdtemp()
|
|
|
|
filename = os.path.join(dirname, 'test_syntax_error.py')
|
|
|
|
self.addCleanup(shutil.rmtree, dirname)
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write("Ceci n'est pas du Python.")
|
|
|
|
|
|
|
|
with extend_sys_path(dirname):
|
|
|
|
with self.assertRaises(SyntaxError):
|
|
|
|
autoreload.check_errors(import_module)('test_syntax_error')
|
|
|
|
self.assertIn(npath(filename), gen_filenames())
|
|
|
|
self.assertIn(npath(filename), gen_filenames())
|
|
|
|
|
|
|
|
def test_check_errors_only_new(self):
|
|
|
|
"""
|
|
|
|
When a file containing an error is imported in a function wrapped by
|
|
|
|
check_errors(), gen_filenames(only_new=True) returns it.
|
|
|
|
|
|
|
|
Run assertions twice to test uncached and cached access.
|
|
|
|
"""
|
|
|
|
dirname = tempfile.mkdtemp()
|
|
|
|
filename = os.path.join(dirname, 'test_syntax_error.py')
|
|
|
|
self.addCleanup(shutil.rmtree, dirname)
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write("Ceci n'est pas du Python.")
|
|
|
|
|
|
|
|
with extend_sys_path(dirname):
|
|
|
|
with self.assertRaises(SyntaxError):
|
|
|
|
autoreload.check_errors(import_module)('test_syntax_error')
|
|
|
|
self.assertIn(npath(filename), gen_filenames(only_new=True))
|
|
|
|
self.assertNotIn(npath(filename), gen_filenames(only_new=True))
|