mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-12-18 14:16:17 +03:00
4296189283
This change fixes the dist test by moving the --no-pkgroot option from pytest to the runtests script. The current "filterwarnings" setting for pytest includes rest_framework, which causes an early import of the module. As a result the current --no-pkgroot behavior fails with an assertion error. Trying to remove the module from sys.modules will cause the warning filter to not apply, so this change moves this code before pytest parses the config and loads the "filterwarnings".
99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
import os
|
|
|
|
import django
|
|
from django.core import management
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
parser.addoption('--staticfiles', action='store_true', default=False,
|
|
help='Run tests with static files collection, using manifest '
|
|
'staticfiles storage. Used for testing the distribution.')
|
|
|
|
|
|
def pytest_configure(config):
|
|
from django.conf import settings
|
|
|
|
# USE_L10N is deprecated, and will be removed in Django 5.0.
|
|
use_l10n = {"USE_L10N": True} if django.VERSION < (4, 0) else {}
|
|
settings.configure(
|
|
DEBUG_PROPAGATE_EXCEPTIONS=True,
|
|
DATABASES={
|
|
'default': {
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
'NAME': ':memory:'
|
|
},
|
|
'secondary': {
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
'NAME': ':memory:'
|
|
}
|
|
},
|
|
SITE_ID=1,
|
|
SECRET_KEY='not very secret in tests',
|
|
USE_I18N=True,
|
|
STATIC_URL='/static/',
|
|
ROOT_URLCONF='tests.urls',
|
|
TEMPLATES=[
|
|
{
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
'APP_DIRS': True,
|
|
'OPTIONS': {
|
|
"debug": True, # We want template errors to raise
|
|
}
|
|
},
|
|
],
|
|
MIDDLEWARE=(
|
|
'django.middleware.common.CommonMiddleware',
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
|
),
|
|
INSTALLED_APPS=(
|
|
'django.contrib.admin',
|
|
'django.contrib.auth',
|
|
'django.contrib.contenttypes',
|
|
'django.contrib.sessions',
|
|
'django.contrib.sites',
|
|
'django.contrib.staticfiles',
|
|
'rest_framework',
|
|
'rest_framework.authtoken',
|
|
'tests.authentication',
|
|
'tests.generic_relations',
|
|
'tests.importable',
|
|
'tests',
|
|
),
|
|
PASSWORD_HASHERS=(
|
|
'django.contrib.auth.hashers.MD5PasswordHasher',
|
|
),
|
|
**use_l10n,
|
|
)
|
|
|
|
# guardian is optional
|
|
try:
|
|
import guardian # NOQA
|
|
except ImportError:
|
|
pass
|
|
else:
|
|
settings.ANONYMOUS_USER_ID = -1
|
|
settings.AUTHENTICATION_BACKENDS = (
|
|
'django.contrib.auth.backends.ModelBackend',
|
|
'guardian.backends.ObjectPermissionBackend',
|
|
)
|
|
settings.INSTALLED_APPS += (
|
|
'guardian',
|
|
)
|
|
|
|
# Manifest storage will raise an exception if static files are not present (ie, a packaging failure).
|
|
if config.getoption('--staticfiles'):
|
|
import rest_framework
|
|
settings.STATIC_ROOT = os.path.join(os.path.dirname(rest_framework.__file__), 'static-root')
|
|
backend = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
|
|
if django.VERSION < (4, 2):
|
|
settings.STATICFILES_STORAGE = backend
|
|
else:
|
|
settings.STORAGES['staticfiles']['BACKEND'] = backend
|
|
|
|
django.setup()
|
|
|
|
if config.getoption('--staticfiles'):
|
|
management.call_command('collectstatic', verbosity=0, interactive=False)
|