Skip to content

Commit 68519c0

Browse files
Ryan P Kilbycarltongibson
authored andcommitted
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
1 parent 3515039 commit 68519c0

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed

runtests.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#! /usr/bin/env python
22
from __future__ import print_function
33

4-
import os
54
import subprocess
65
import sys
76

@@ -82,20 +81,6 @@ def is_class(string):
8281
run_flake8 = False
8382
run_isort = False
8483

85-
try:
86-
# Remove the package root directory from `sys.path`, ensuring that rest_framework
87-
# is imported from the installed site packages. Used for testing the distribution
88-
sys.argv.remove('--no-pkgroot')
89-
except ValueError:
90-
pass
91-
else:
92-
sys.path.pop(0)
93-
94-
# import rest_framework before pytest re-adds the package root directory.
95-
import rest_framework
96-
package_dir = os.path.join(os.getcwd(), 'rest_framework')
97-
assert not rest_framework.__file__.startswith(package_dir)
98-
9984
if len(sys.argv) > 1:
10085
pytest_args = sys.argv[1:]
10186
first_arg = pytest_args[0]

tests/conftest.py

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
def pytest_configure():
2-
from django.conf import settings
1+
import os
2+
import sys
33

4-
MIDDLEWARE = (
5-
'django.middleware.common.CommonMiddleware',
6-
'django.contrib.sessions.middleware.SessionMiddleware',
7-
'django.contrib.auth.middleware.AuthenticationMiddleware',
8-
'django.contrib.messages.middleware.MessageMiddleware',
9-
)
4+
import django
5+
from django.core import management
6+
7+
8+
def pytest_addoption(parser):
9+
parser.addoption('--no-pkgroot', action='store_true', default=False,
10+
help='Remove package root directory from sys.path, ensuring that '
11+
'rest_framework is imported from the installed site-packages. '
12+
'Used for testing the distribution.')
13+
parser.addoption('--staticfiles', action='store_true', default=False,
14+
help='Run tests with static files collection, using manifest '
15+
'staticfiles storage. Used for testing the distribution.')
16+
17+
18+
def pytest_configure(config):
19+
from django.conf import settings
1020

1121
settings.configure(
1222
DEBUG_PROPAGATE_EXCEPTIONS=True,
@@ -31,8 +41,12 @@ def pytest_configure():
3141
}
3242
},
3343
],
34-
MIDDLEWARE=MIDDLEWARE,
35-
MIDDLEWARE_CLASSES=MIDDLEWARE,
44+
MIDDLEWARE=(
45+
'django.middleware.common.CommonMiddleware',
46+
'django.contrib.sessions.middleware.SessionMiddleware',
47+
'django.contrib.auth.middleware.AuthenticationMiddleware',
48+
'django.contrib.messages.middleware.MessageMiddleware',
49+
),
3650
INSTALLED_APPS=(
3751
'django.contrib.auth',
3852
'django.contrib.contenttypes',
@@ -64,8 +78,21 @@ def pytest_configure():
6478
'guardian',
6579
)
6680

67-
try:
68-
import django
69-
django.setup()
70-
except AttributeError:
71-
pass
81+
if config.getoption('--no-pkgroot'):
82+
sys.path.pop(0)
83+
84+
# import rest_framework before pytest re-adds the package root directory.
85+
import rest_framework
86+
package_dir = os.path.join(os.getcwd(), 'rest_framework')
87+
assert not rest_framework.__file__.startswith(package_dir)
88+
89+
# Manifest storage will raise an exception if static files are not present (ie, a packaging failure).
90+
if config.getoption('--staticfiles'):
91+
import rest_framework
92+
settings.STATIC_ROOT = os.path.join(os.path.dirname(rest_framework.__file__), 'static-root')
93+
settings.STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
94+
95+
django.setup()
96+
97+
if config.getoption('--staticfiles'):
98+
management.call_command('collectstatic', verbosity=0, interactive=False)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ deps =
3131
-rrequirements/requirements-optionals.txt
3232

3333
[testenv:dist]
34-
commands = ./runtests.py --fast {posargs} --no-pkgroot -rw
34+
commands = ./runtests.py --fast {posargs} --no-pkgroot --staticfiles -rw
3535
deps =
3636
django
3737
-rrequirements/requirements-testing.txt

0 commit comments

Comments
 (0)