Expurged INSTALLED_APPS from code and tests.

Except the app cache code and a few specific tests, of course.
This commit is contained in:
Aymeric Augustin 2013-12-19 22:33:46 +01:00
parent 9cdf1f6d54
commit 2239081ff1
5 changed files with 33 additions and 35 deletions

View File

@ -6,6 +6,7 @@ from django.contrib.auth import logout as auth_logout, REDIRECT_FIELD_NAME
from django.contrib.contenttypes import views as contenttype_views from django.contrib.contenttypes import views as contenttype_views
from django.views.decorators.csrf import csrf_protect from django.views.decorators.csrf import csrf_protect
from django.db.models.base import ModelBase from django.db.models.base import ModelBase
from django.core.apps import app_cache
from django.core.exceptions import ImproperlyConfigured, PermissionDenied from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.core.urlresolvers import reverse, NoReverseMatch from django.core.urlresolvers import reverse, NoReverseMatch
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
@ -156,20 +157,17 @@ class AdminSite(object):
""" """
Check that all things needed to run the admin have been correctly installed. Check that all things needed to run the admin have been correctly installed.
The default implementation checks that LogEntry, ContentType and the The default implementation checks that admin and contenttypes apps are
auth context processor are installed. installed, as well as the auth context processor.
""" """
from django.contrib.admin.models import LogEntry app_cache.populate_apps()
from django.contrib.contenttypes.models import ContentType if not app_cache.has_app('django.contrib.admin'):
if not LogEntry._meta.installed:
raise ImproperlyConfigured("Put 'django.contrib.admin' in your " raise ImproperlyConfigured("Put 'django.contrib.admin' in your "
"INSTALLED_APPS setting in order to use the admin application.") "INSTALLED_APPS setting in order to use the admin application.")
if not ContentType._meta.installed: if not app_cache.has_app('django.contrib.contenttypes'):
raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in " raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in "
"your INSTALLED_APPS setting in order to use the admin application.") "your INSTALLED_APPS setting in order to use the admin application.")
if not ('django.contrib.auth.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS or if 'django.contrib.auth.context_processors.auth' not in settings.TEMPLATE_CONTEXT_PROCESSORS:
'django.core.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS):
raise ImproperlyConfigured("Put 'django.contrib.auth.context_processors.auth' " raise ImproperlyConfigured("Put 'django.contrib.auth.context_processors.auth' "
"in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.") "in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.")

View File

@ -9,7 +9,7 @@ from django.core.apps import app_cache
from django.core.validators import RegexValidator, EmailValidator from django.core.validators import RegexValidator, EmailValidator
from django.db import models, migrations from django.db import models, migrations
from django.db.migrations.writer import MigrationWriter from django.db.migrations.writer import MigrationWriter
from django.test import TestCase, override_settings from django.test import TestCase
from django.utils import six from django.utils import six
from django.utils.deconstruct import deconstructible from django.utils.deconstruct import deconstructible
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -122,10 +122,9 @@ class WriterTests(TestCase):
base_dir = os.path.dirname(os.path.dirname(__file__)) base_dir = os.path.dirname(os.path.dirname(__file__))
with override_settings(INSTALLED_APPS=test_apps): for app in test_apps:
for app in test_apps: with app_cache._with_app(app):
with app_cache._with_app(app): migration = migrations.Migration('0001_initial', app.split('.')[-1])
migration = migrations.Migration('0001_initial', app.split('.')[-1]) expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py']))
expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py'])) writer = MigrationWriter(migration)
writer = MigrationWriter(migration) self.assertEqual(writer.path, expected_path)
self.assertEqual(writer.path, expected_path)

View File

@ -13,7 +13,8 @@ from .models import (ConcreteModel, ConcreteModelSubclass,
ConcreteModelSubclassProxy) ConcreteModelSubclassProxy)
@override_settings(INSTALLED_APPS=('app1', 'app2')) # Required for available_apps.
@override_settings(INSTALLED_APPS=['app1', 'app2'])
class ProxyModelInheritanceTests(TransactionTestCase): class ProxyModelInheritanceTests(TransactionTestCase):
""" """
Proxy model inheritance across apps can result in migrate not creating the table Proxy model inheritance across apps can result in migrate not creating the table
@ -25,20 +26,17 @@ class ProxyModelInheritanceTests(TransactionTestCase):
def setUp(self): def setUp(self):
self.old_sys_path = sys.path[:] self.old_sys_path = sys.path[:]
sys.path.append(os.path.dirname(os.path.abspath(upath(__file__)))) sys.path.append(os.path.dirname(os.path.abspath(upath(__file__))))
self._with_app1 = app_cache._begin_with_app('app1')
self._with_app2 = app_cache._begin_with_app('app2')
def tearDown(self): def tearDown(self):
app_cache._end_with_app(self._with_app1)
app_cache._end_with_app(self._with_app2)
sys.path = self.old_sys_path sys.path = self.old_sys_path
def test_table_exists(self): def test_table_exists(self):
call_command('migrate', verbosity=0) with app_cache._with_app('app1'), app_cache._with_app('app2'):
from .app1.models import ProxyModel call_command('migrate', verbosity=0)
from .app2.models import NiceModel from .app1.models import ProxyModel
self.assertEqual(NiceModel.objects.all().count(), 0) from .app2.models import NiceModel
self.assertEqual(ProxyModel.objects.all().count(), 0) self.assertEqual(NiceModel.objects.all().count(), 0)
self.assertEqual(ProxyModel.objects.all().count(), 0)
class MultiTableInheritanceProxyTest(TestCase): class MultiTableInheritanceProxyTest(TestCase):

View File

@ -298,7 +298,7 @@ class TestCollectionDryRun(CollectionTestCase, TestNoFilesCreated):
class TestCollectionFilesOverride(CollectionTestCase): class TestCollectionFilesOverride(CollectionTestCase):
""" """
Test overriding duplicated files by ``collectstatic`` management command. Test overriding duplicated files by ``collectstatic`` management command.
Check for proper handling of apps order in INSTALLED_APPS even if file modification Check for proper handling of apps order in installed apps even if file modification
dates are in different order: dates are in different order:
'staticfiles_tests.apps.test', 'staticfiles_tests.apps.test',
@ -314,7 +314,7 @@ class TestCollectionFilesOverride(CollectionTestCase):
# prepare duplicate of file2.txt from no_label app # prepare duplicate of file2.txt from no_label app
# this file will have modification time older than no_label/static/file2.txt # this file will have modification time older than no_label/static/file2.txt
# anyway it should be taken to STATIC_ROOT because 'test' app is before # anyway it should be taken to STATIC_ROOT because 'test' app is before
# 'no_label' app in INSTALLED_APPS # 'no_label' app in installed apps
self.testfile_path = os.path.join(TEST_ROOT, 'apps', 'test', 'static', 'file2.txt') self.testfile_path = os.path.join(TEST_ROOT, 'apps', 'test', 'static', 'file2.txt')
with open(self.testfile_path, 'w+') as f: with open(self.testfile_path, 'w+') as f:
f.write('duplicate of file2.txt') f.write('duplicate of file2.txt')
@ -340,7 +340,7 @@ class TestCollectionFilesOverride(CollectionTestCase):
self.assertFileContains('file2.txt', 'duplicate of file2.txt') self.assertFileContains('file2.txt', 'duplicate of file2.txt')
# and now change modification time of no_label/static/file2.txt # and now change modification time of no_label/static/file2.txt
# test app is first in INSTALLED_APPS so file2.txt should remain unmodified # test app is first in installed apps so file2.txt should remain unmodified
mtime = os.path.getmtime(self.testfile_path) mtime = os.path.getmtime(self.testfile_path)
atime = os.path.getatime(self.testfile_path) atime = os.path.getatime(self.testfile_path)
os.utime(self.orig_path, (mtime + 1, atime + 1)) os.utime(self.orig_path, (mtime + 1, atime + 1))

View File

@ -2,6 +2,7 @@ import os
from django import conf from django import conf
from django.contrib import admin from django.contrib import admin
from django.core.apps import app_cache
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.utils.autoreload import gen_filenames from django.utils.autoreload import gen_filenames
@ -27,7 +28,6 @@ class TestFilenameGenerator(TestCase):
self.assertIn(os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo'), self.assertIn(os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo'),
filenames) filenames)
@override_settings(INSTALLED_APPS=())
def test_project_root_locale(self): def test_project_root_locale(self):
""" """
Test that gen_filenames also yields from the current directory (project Test that gen_filenames also yields from the current directory (project
@ -36,19 +36,22 @@ class TestFilenameGenerator(TestCase):
old_cwd = os.getcwd() old_cwd = os.getcwd()
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
try: try:
filenames = list(gen_filenames()) # Remove the current app from the app cache to guarantee that the
# files will be found thanks to the current working directory.
with app_cache._empty():
filenames = list(gen_filenames())
self.assertIn( self.assertIn(
os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo'), os.path.join(LOCALE_PATH, 'nl', 'LC_MESSAGES', 'django.mo'),
filenames) filenames)
finally: finally:
os.chdir(old_cwd) os.chdir(old_cwd)
@override_settings(INSTALLED_APPS=('django.contrib.admin',))
def test_app_locales(self): def test_app_locales(self):
""" """
Test that gen_filenames also yields from INSTALLED_APPS locales. Test that gen_filenames also yields from locale dirs in installed apps.
""" """
filenames = list(gen_filenames()) with app_cache._empty(), app_cache._with_app('django.contrib.admin'):
filenames = list(gen_filenames())
self.assertIn(os.path.join(os.path.dirname(admin.__file__), 'locale', self.assertIn(os.path.join(os.path.dirname(admin.__file__), 'locale',
'nl', 'LC_MESSAGES', 'django.mo'), 'nl', 'LC_MESSAGES', 'django.mo'),
filenames) filenames)