2016-11-07 23:51:23 +00:00
|
|
|
import gettext
|
2013-11-02 09:28:22 +00:00
|
|
|
import os
|
2015-08-05 09:06:26 +00:00
|
|
|
import shutil
|
2014-07-25 13:32:14 +00:00
|
|
|
import tempfile
|
2015-01-28 12:35:27 +00:00
|
|
|
from importlib import import_module
|
2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
2013-11-02 09:28:22 +00:00
|
|
|
|
2017-01-07 11:11:46 +00:00
|
|
|
import _thread
|
|
|
|
|
2013-11-02 09:28:22 +00:00
|
|
|
from django import conf
|
2013-11-05 13:30:31 +00:00
|
|
|
from django.contrib import admin
|
2017-01-19 17:16:04 +00:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-02-21 18:18:54 +00:00
|
|
|
from django.test.utils import extend_sys_path
|
2015-08-05 09:06:26 +00:00
|
|
|
from django.utils import autoreload
|
2016-11-07 23:51:23 +00:00
|
|
|
from django.utils.translation import trans_real
|
2013-11-02 09:28:22 +00:00
|
|
|
|
|
|
|
LOCALE_PATH = os.path.join(os.path.dirname(__file__), 'locale')
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class TestFilenameGenerator(SimpleTestCase):
|
2015-08-08 13:12:49 +00:00
|
|
|
|
|
|
|
def clear_autoreload_caches(self):
|
2014-07-05 12:43:12 +00:00
|
|
|
autoreload._cached_modules = set()
|
|
|
|
autoreload._cached_filenames = []
|
|
|
|
|
2015-08-08 13:12:49 +00:00
|
|
|
def assertFileFound(self, filename):
|
|
|
|
self.clear_autoreload_caches()
|
|
|
|
# Test uncached access
|
2017-01-20 13:01:02 +00:00
|
|
|
self.assertIn(filename, autoreload.gen_filenames())
|
2015-08-08 13:12:49 +00:00
|
|
|
# Test cached access
|
2017-01-20 13:01:02 +00:00
|
|
|
self.assertIn(filename, autoreload.gen_filenames())
|
2015-08-08 13:12:49 +00:00
|
|
|
|
|
|
|
def assertFileNotFound(self, filename):
|
|
|
|
self.clear_autoreload_caches()
|
|
|
|
# Test uncached access
|
2017-01-20 13:01:02 +00:00
|
|
|
self.assertNotIn(filename, autoreload.gen_filenames())
|
2015-08-08 13:12:49 +00:00
|
|
|
# Test cached access
|
2017-01-20 13:01:02 +00:00
|
|
|
self.assertNotIn(filename, autoreload.gen_filenames())
|
2015-08-08 13:12:49 +00:00
|
|
|
|
|
|
|
def assertFileFoundOnlyNew(self, filename):
|
|
|
|
self.clear_autoreload_caches()
|
|
|
|
# Test uncached access
|
2017-01-20 13:01:02 +00:00
|
|
|
self.assertIn(filename, autoreload.gen_filenames(only_new=True))
|
2015-08-08 13:12:49 +00:00
|
|
|
# Test cached access
|
2017-01-20 13:01:02 +00:00
|
|
|
self.assertNotIn(filename, autoreload.gen_filenames(only_new=True))
|
2015-08-08 13:12:49 +00:00
|
|
|
|
2013-11-02 09:28:22 +00:00
|
|
|
def test_django_locales(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
gen_filenames() yields the built-in Django locale files.
|
2013-11-02 09:28:22 +00:00
|
|
|
"""
|
2015-08-08 13:12:49 +00:00
|
|
|
django_dir = os.path.join(os.path.dirname(conf.__file__), 'locale')
|
|
|
|
django_mo = os.path.join(django_dir, 'nl', 'LC_MESSAGES', 'django.mo')
|
|
|
|
self.assertFileFound(django_mo)
|
2013-11-02 09:28:22 +00:00
|
|
|
|
2015-01-21 16:55:57 +00:00
|
|
|
@override_settings(LOCALE_PATHS=[LOCALE_PATH])
|
2013-11-05 13:30:31 +00:00
|
|
|
def test_locale_paths_setting(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
gen_filenames also yields from LOCALE_PATHS locales.
|
2013-11-05 13:30:31 +00:00
|
|
|
"""
|
2015-08-08 13:12:49 +00:00
|
|
|
locale_paths_mo = os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo')
|
|
|
|
self.assertFileFound(locale_paths_mo)
|
2013-11-02 09:28:22 +00:00
|
|
|
|
2013-12-23 09:37:34 +00:00
|
|
|
@override_settings(INSTALLED_APPS=[])
|
2013-11-05 13:30:31 +00:00
|
|
|
def test_project_root_locale(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
gen_filenames() also yields from the current directory (project root).
|
2013-11-05 13:30:31 +00:00
|
|
|
"""
|
|
|
|
old_cwd = os.getcwd()
|
|
|
|
os.chdir(os.path.dirname(__file__))
|
2015-08-08 13:12:49 +00:00
|
|
|
current_dir = os.path.join(os.path.dirname(__file__), 'locale')
|
|
|
|
current_dir_mo = os.path.join(current_dir, 'nl', 'LC_MESSAGES', 'django.mo')
|
2013-11-05 13:30:31 +00:00
|
|
|
try:
|
2015-08-08 13:12:49 +00:00
|
|
|
self.assertFileFound(current_dir_mo)
|
2013-11-05 13:30:31 +00:00
|
|
|
finally:
|
|
|
|
os.chdir(old_cwd)
|
2013-11-02 09:28:22 +00:00
|
|
|
|
2013-12-23 09:37:34 +00:00
|
|
|
@override_settings(INSTALLED_APPS=['django.contrib.admin'])
|
2013-11-02 09:28:22 +00:00
|
|
|
def test_app_locales(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
gen_filenames() also yields from locale dirs in installed apps.
|
2013-11-02 09:28:22 +00:00
|
|
|
"""
|
2015-08-08 13:12:49 +00:00
|
|
|
admin_dir = os.path.join(os.path.dirname(admin.__file__), 'locale')
|
|
|
|
admin_mo = os.path.join(admin_dir, 'nl', 'LC_MESSAGES', 'django.mo')
|
|
|
|
self.assertFileFound(admin_mo)
|
2013-11-05 13:30:31 +00: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.
|
|
|
|
"""
|
2015-08-08 13:12:49 +00:00
|
|
|
django_dir = os.path.join(os.path.dirname(conf.__file__), 'locale')
|
|
|
|
django_mo = os.path.join(django_dir, 'nl', 'LC_MESSAGES', 'django.mo')
|
|
|
|
self.assertFileNotFound(django_mo)
|
2014-07-05 12:43:12 +00:00
|
|
|
|
2015-08-29 11:05:22 +00:00
|
|
|
def test_paths_are_native_strings(self):
|
|
|
|
for filename in autoreload.gen_filenames():
|
|
|
|
self.assertIsInstance(filename, str)
|
|
|
|
|
2014-07-05 12:43:12 +00: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.
|
|
|
|
"""
|
2015-08-08 13:12:49 +00:00
|
|
|
dirname = tempfile.mkdtemp()
|
|
|
|
filename = os.path.join(dirname, 'test_only_new_module.py')
|
|
|
|
self.addCleanup(shutil.rmtree, dirname)
|
|
|
|
with open(filename, 'w'):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Test uncached access
|
|
|
|
self.clear_autoreload_caches()
|
|
|
|
filenames = set(autoreload.gen_filenames(only_new=True))
|
|
|
|
filenames_reference = set(autoreload.gen_filenames())
|
|
|
|
self.assertEqual(filenames, filenames_reference)
|
|
|
|
|
|
|
|
# Test cached access: no changes
|
|
|
|
filenames = set(autoreload.gen_filenames(only_new=True))
|
|
|
|
self.assertEqual(filenames, set())
|
|
|
|
|
|
|
|
# Test cached access: add a module
|
|
|
|
with extend_sys_path(dirname):
|
|
|
|
import_module('test_only_new_module')
|
|
|
|
filenames = set(autoreload.gen_filenames(only_new=True))
|
2017-01-20 13:01:02 +00:00
|
|
|
self.assertEqual(filenames, {filename})
|
2014-07-25 13:32:14 +00:00
|
|
|
|
|
|
|
def test_deleted_removed(self):
|
2015-08-08 13:12:49 +00:00
|
|
|
"""
|
|
|
|
When a file is deleted, gen_filenames() no longer returns it.
|
|
|
|
"""
|
2015-02-21 18:18:54 +00:00
|
|
|
dirname = tempfile.mkdtemp()
|
|
|
|
filename = os.path.join(dirname, 'test_deleted_removed_module.py')
|
2015-08-08 13:12:49 +00:00
|
|
|
self.addCleanup(shutil.rmtree, dirname)
|
2015-02-21 18:18:54 +00:00
|
|
|
with open(filename, 'w'):
|
|
|
|
pass
|
2015-08-08 13:12:49 +00:00
|
|
|
|
2015-02-21 18:18:54 +00:00
|
|
|
with extend_sys_path(dirname):
|
|
|
|
import_module('test_deleted_removed_module')
|
2015-08-08 13:12:49 +00:00
|
|
|
self.assertFileFound(filename)
|
|
|
|
|
2015-02-21 18:18:54 +00:00
|
|
|
os.unlink(filename)
|
2015-08-08 13:12:49 +00:00
|
|
|
self.assertFileNotFound(filename)
|
2015-08-05 09:06:26 +00: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.
|
|
|
|
"""
|
|
|
|
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')
|
2015-08-08 13:12:49 +00:00
|
|
|
self.assertFileFound(filename)
|
2015-08-05 09:06:26 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
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')
|
2015-08-08 13:12:49 +00:00
|
|
|
self.assertFileFoundOnlyNew(filename)
|
2015-08-29 11:26:00 +00:00
|
|
|
|
|
|
|
def test_check_errors_catches_all_exceptions(self):
|
|
|
|
"""
|
|
|
|
Since Python may raise arbitrary exceptions when importing code,
|
|
|
|
check_errors() must catch Exception, not just some subclasses.
|
|
|
|
"""
|
|
|
|
dirname = tempfile.mkdtemp()
|
|
|
|
filename = os.path.join(dirname, 'test_exception.py')
|
|
|
|
self.addCleanup(shutil.rmtree, dirname)
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write("raise Exception")
|
|
|
|
|
|
|
|
with extend_sys_path(dirname):
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
autoreload.check_errors(import_module)('test_exception')
|
|
|
|
self.assertFileFound(filename)
|
2016-11-07 23:51:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CleanFilesTests(SimpleTestCase):
|
|
|
|
TEST_MAP = {
|
|
|
|
# description: (input_file_list, expected_returned_file_list)
|
|
|
|
'falsies': ([None, False], []),
|
|
|
|
'pycs': (['myfile.pyc'], ['myfile.py']),
|
|
|
|
'pyos': (['myfile.pyo'], ['myfile.py']),
|
|
|
|
'$py.class': (['myclass$py.class'], ['myclass.py']),
|
|
|
|
'combined': (
|
|
|
|
[None, 'file1.pyo', 'file2.pyc', 'myclass$py.class'],
|
|
|
|
['file1.py', 'file2.py', 'myclass.py'],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
def _run_tests(self, mock_files_exist=True):
|
|
|
|
with mock.patch('django.utils.autoreload.os.path.exists', return_value=mock_files_exist):
|
|
|
|
for description, values in self.TEST_MAP.items():
|
|
|
|
filenames, expected_returned_filenames = values
|
|
|
|
self.assertEqual(
|
|
|
|
autoreload.clean_files(filenames),
|
|
|
|
expected_returned_filenames if mock_files_exist else [],
|
|
|
|
msg='{} failed for input file list: {}; returned file list: {}'.format(
|
|
|
|
description, filenames, expected_returned_filenames
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_files_exist(self):
|
|
|
|
"""
|
|
|
|
If the file exists, any compiled files (pyc, pyo, $py.class) are
|
|
|
|
transformed as their source files.
|
|
|
|
"""
|
|
|
|
self._run_tests()
|
|
|
|
|
|
|
|
def test_files_do_not_exist(self):
|
|
|
|
"""
|
|
|
|
If the files don't exist, they aren't in the returned file list.
|
|
|
|
"""
|
|
|
|
self._run_tests(mock_files_exist=False)
|
|
|
|
|
|
|
|
|
|
|
|
class ResetTranslationsTests(SimpleTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.gettext_translations = gettext._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_resets_gettext(self):
|
|
|
|
gettext._translations = {'foo': 'bar'}
|
|
|
|
autoreload.reset_translations()
|
|
|
|
self.assertEqual(gettext._translations, {})
|
|
|
|
|
|
|
|
def test_resets_trans_real(self):
|
|
|
|
trans_real._translations = {'foo': 'bar'}
|
|
|
|
trans_real._default = 1
|
|
|
|
trans_real._active = False
|
|
|
|
autoreload.reset_translations()
|
|
|
|
self.assertEqual(trans_real._translations, {})
|
|
|
|
self.assertIsNone(trans_real._default)
|
|
|
|
self.assertIsInstance(trans_real._active, _thread._local)
|
2017-10-25 07:28:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RestartWithReloaderTests(SimpleTestCase):
|
|
|
|
executable = '/usr/bin/python'
|
|
|
|
|
|
|
|
def patch_autoreload(self, argv):
|
|
|
|
patch_call = mock.patch('django.utils.autoreload.subprocess.call', return_value=0)
|
|
|
|
patches = [
|
|
|
|
mock.patch('django.utils.autoreload.sys.argv', argv),
|
|
|
|
mock.patch('django.utils.autoreload.sys.executable', self.executable),
|
|
|
|
mock.patch('django.utils.autoreload.sys.warnoptions', ['all']),
|
|
|
|
]
|
|
|
|
for p in patches:
|
|
|
|
p.start()
|
|
|
|
self.addCleanup(p.stop)
|
|
|
|
mock_call = patch_call.start()
|
|
|
|
self.addCleanup(patch_call.stop)
|
|
|
|
return mock_call
|
|
|
|
|
|
|
|
def test_manage_py(self):
|
|
|
|
argv = ['./manage.py', 'runserver']
|
|
|
|
mock_call = self.patch_autoreload(argv)
|
|
|
|
autoreload.restart_with_reloader()
|
|
|
|
self.assertEqual(mock_call.call_count, 1)
|
|
|
|
self.assertEqual(mock_call.call_args[0][0], [self.executable, '-Wall'] + argv)
|
2017-10-25 07:29:05 +00:00
|
|
|
|
|
|
|
def test_python_m_django(self):
|
|
|
|
main = '/usr/lib/pythonX.Y/site-packages/django/__main__.py'
|
|
|
|
argv = [main, 'runserver']
|
|
|
|
mock_call = self.patch_autoreload(argv)
|
|
|
|
with mock.patch('django.__main__.__file__', main):
|
|
|
|
autoreload.restart_with_reloader()
|
|
|
|
self.assertEqual(mock_call.call_count, 1)
|
|
|
|
self.assertEqual(mock_call.call_args[0][0], [self.executable, '-Wall', '-m', 'django'] + argv[1:])
|