1
- def pytest_configure ():
2
- from django . conf import settings
1
+ import os
2
+ import sys
3
3
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
10
20
11
21
settings .configure (
12
22
DEBUG_PROPAGATE_EXCEPTIONS = True ,
@@ -31,8 +41,12 @@ def pytest_configure():
31
41
}
32
42
},
33
43
],
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
+ ),
36
50
INSTALLED_APPS = (
37
51
'django.contrib.auth' ,
38
52
'django.contrib.contenttypes' ,
@@ -64,8 +78,21 @@ def pytest_configure():
64
78
'guardian' ,
65
79
)
66
80
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 )
0 commit comments