test_autoreload.py 3.48 KB
Newer Older
1
from importlib import import_module
2
import os
3
import tempfile
4 5

from django import conf
6
from django.contrib import admin
7 8
from django.test import TestCase, override_settings
from django.utils.autoreload import gen_filenames
9
from django.utils._os import upath
10 11 12 13 14

LOCALE_PATH = os.path.join(os.path.dirname(__file__), 'locale')


class TestFilenameGenerator(TestCase):
15 16 17 18 19 20
    def setUp(self):
        # Empty cached variables
        from django.utils import autoreload
        autoreload._cached_modules = set()
        autoreload._cached_filenames = []

21 22 23 24 25
    def test_django_locales(self):
        """
        Test that gen_filenames() also yields the built-in django locale files.
        """
        filenames = list(gen_filenames())
26 27 28
        self.assertIn(os.path.join(os.path.dirname(conf.__file__), 'locale',
                                   'nl', 'LC_MESSAGES', 'django.mo'),
                      filenames)
29

30 31 32 33 34 35 36 37
    @override_settings(LOCALE_PATHS=(LOCALE_PATH,))
    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)
38

39
    @override_settings(INSTALLED_APPS=[])
40 41 42 43 44 45 46 47
    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:
48
            filenames = list(gen_filenames())
49 50 51 52 53
            self.assertIn(
                os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo'),
                filenames)
        finally:
            os.chdir(old_cwd)
54

55
    @override_settings(INSTALLED_APPS=['django.contrib.admin'])
56 57
    def test_app_locales(self):
        """
58
        Test that gen_filenames also yields from locale dirs in installed apps.
59
        """
60
        filenames = list(gen_filenames())
61 62
        self.assertIn(os.path.join(os.path.dirname(admin.__file__), 'locale',
                                   'nl', 'LC_MESSAGES', 'django.mo'),
63
                      filenames)
64 65 66 67 68 69 70 71 72 73 74 75

    @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(
            os.path.join(os.path.dirname(conf.__file__), 'locale', 'nl',
                         'LC_MESSAGES', 'django.mo'),
            filenames)
76 77 78 79 80 81

    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.
        """
Tim Graham's avatar
Tim Graham committed
82 83
        list(gen_filenames())
        from fractions import Fraction  # NOQA
84 85 86
        filenames2 = list(gen_filenames(only_new=True))
        self.assertEqual(len(filenames2), 1)
        self.assertTrue(filenames2[0].endswith('fractions.py'))
87
        self.assertFalse(any(f.endswith('.pyc') for f in gen_filenames()))
88 89

    def test_deleted_removed(self):
90
        fd, filepath = tempfile.mkstemp(dir=os.path.dirname(upath(__file__)), suffix='.py')
91 92
        try:
            _, filename = os.path.split(filepath)
93
            import_module('.%s' % filename.replace('.py', ''), package='utils_tests')
94 95
            self.assertIn(filepath, gen_filenames())
        finally:
96
            os.close(fd)
97 98
            os.remove(filepath)
        self.assertNotIn(filepath, gen_filenames())