Skip to content

Commit 899e3d8

Browse files
committed
fix dist test by moving --no-pkgroot to runtests.py
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".
1 parent d32346b commit 899e3d8

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

runtests.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#! /usr/bin/env python3
2+
import os
23
import sys
34

45
import pytest
@@ -34,6 +35,18 @@ def is_class(string):
3435
'--cov-report', 'xml',
3536
] + pytest_args
3637

38+
try:
39+
pytest_args.remove('--no-pkgroot')
40+
except ValueError:
41+
pass
42+
else:
43+
sys.path.pop(0)
44+
45+
# import rest_framework before pytest re-adds the package root directory.
46+
import rest_framework
47+
package_dir = os.path.join(os.getcwd(), 'rest_framework')
48+
assert not rest_framework.__file__.startswith(package_dir)
49+
3750
if first_arg.startswith('-'):
3851
# `runtests.py [flags]`
3952
pytest_args = ['tests'] + pytest_args

tests/conftest.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import os
2-
import sys
32

43
import django
54
from django.core import management
65

76

87
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.')
138
parser.addoption('--staticfiles', action='store_true', default=False,
149
help='Run tests with static files collection, using manifest '
1510
'staticfiles storage. Used for testing the distribution.')
@@ -87,19 +82,15 @@ def pytest_configure(config):
8782
'guardian',
8883
)
8984

90-
if config.getoption('--no-pkgroot'):
91-
sys.path.pop(0)
92-
93-
# import rest_framework before pytest re-adds the package root directory.
94-
import rest_framework
95-
package_dir = os.path.join(os.getcwd(), 'rest_framework')
96-
assert not rest_framework.__file__.startswith(package_dir)
97-
9885
# Manifest storage will raise an exception if static files are not present (ie, a packaging failure).
9986
if config.getoption('--staticfiles'):
10087
import rest_framework
10188
settings.STATIC_ROOT = os.path.join(os.path.dirname(rest_framework.__file__), 'static-root')
102-
settings.STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
89+
backend = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
90+
if django.VERSION < (4, 2):
91+
settings.STATICFILES_STORAGE = backend
92+
else:
93+
settings.STORAGES['staticfiles']['BACKEND'] = backend
10394

10495
django.setup()
10596

0 commit comments

Comments
 (0)