Test staticfiles (#5701)

* Remove 'MIDDLEWARE_CLASSES' compat setting

* Remove 'django.setup()' compat import

* Move '--no-pkgroot' handling to conftest

* Add staticfiles handling to dist build
This commit is contained in:
Ryan P Kilby 2018-01-02 05:35:56 -05:00 committed by Carlton Gibson
parent 351503907c
commit 68519c092f
3 changed files with 43 additions and 31 deletions

View File

@ -1,7 +1,6 @@
#! /usr/bin/env python #! /usr/bin/env python
from __future__ import print_function from __future__ import print_function
import os
import subprocess import subprocess
import sys import sys
@ -82,20 +81,6 @@ if __name__ == "__main__":
run_flake8 = False run_flake8 = False
run_isort = False run_isort = False
try:
# Remove the package root directory from `sys.path`, ensuring that rest_framework
# is imported from the installed site packages. Used for testing the distribution
sys.argv.remove('--no-pkgroot')
except ValueError:
pass
else:
sys.path.pop(0)
# import rest_framework before pytest re-adds the package root directory.
import rest_framework
package_dir = os.path.join(os.getcwd(), 'rest_framework')
assert not rest_framework.__file__.startswith(package_dir)
if len(sys.argv) > 1: if len(sys.argv) > 1:
pytest_args = sys.argv[1:] pytest_args = sys.argv[1:]
first_arg = pytest_args[0] first_arg = pytest_args[0]

View File

@ -1,12 +1,22 @@
def pytest_configure(): import os
from django.conf import settings import sys
MIDDLEWARE = ( import django
'django.middleware.common.CommonMiddleware', from django.core import management
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', def pytest_addoption(parser):
) parser.addoption('--no-pkgroot', action='store_true', default=False,
help='Remove package root directory from sys.path, ensuring that '
'rest_framework is imported from the installed site-packages. '
'Used for testing the distribution.')
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
settings.configure( settings.configure(
DEBUG_PROPAGATE_EXCEPTIONS=True, DEBUG_PROPAGATE_EXCEPTIONS=True,
@ -31,8 +41,12 @@ def pytest_configure():
} }
}, },
], ],
MIDDLEWARE=MIDDLEWARE, MIDDLEWARE=(
MIDDLEWARE_CLASSES=MIDDLEWARE, 'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
),
INSTALLED_APPS=( INSTALLED_APPS=(
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
@ -64,8 +78,21 @@ def pytest_configure():
'guardian', 'guardian',
) )
try: if config.getoption('--no-pkgroot'):
import django sys.path.pop(0)
# import rest_framework before pytest re-adds the package root directory.
import rest_framework
package_dir = os.path.join(os.getcwd(), 'rest_framework')
assert not rest_framework.__file__.startswith(package_dir)
# 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')
settings.STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
django.setup() django.setup()
except AttributeError:
pass if config.getoption('--staticfiles'):
management.call_command('collectstatic', verbosity=0, interactive=False)

View File

@ -31,7 +31,7 @@ deps =
-rrequirements/requirements-optionals.txt -rrequirements/requirements-optionals.txt
[testenv:dist] [testenv:dist]
commands = ./runtests.py --fast {posargs} --no-pkgroot -rw commands = ./runtests.py --fast {posargs} --no-pkgroot --staticfiles -rw
deps = deps =
django django
-rrequirements/requirements-testing.txt -rrequirements/requirements-testing.txt