Skip to content

Commit

Permalink
Merge pull request #191 from blueyed/setup-once
Browse files Browse the repository at this point in the history
_setup: do not call django.setup() if settings are configured already
  • Loading branch information
blueyed committed Aug 16, 2018
2 parents a045609 + a0a4349 commit 5ea0db0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.coverage.*
.coverage
coverage.xml
sitecustomize.py
docs/_build
*.egg-info
*.egg
Expand Down
6 changes: 4 additions & 2 deletions configurations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ def _setup():

importer.install()

import django
django.setup()
from django.apps import apps
if not apps.ready:
import django
django.setup()


def load_ipython_extension(ipython):
Expand Down
9 changes: 9 additions & 0 deletions sitecustomize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Setup coverage tracking for subprocesses.
Any ImportError is silently ignored.
Requires COVERAGE_PROCESS_START in the environments, which gets set in
tox.ini.
"""
import coverage

coverage.process_startup()
17 changes: 17 additions & 0 deletions tests/setup_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Used by tests to ensure logging is kept when calling setup() twice."""

try:
from unittest import mock
except ImportError:
from mock import mock

import configurations

print('setup_1')
configurations.setup()

with mock.patch('django.setup', side_effect=Exception('setup called twice')):
print('setup_2')
configurations.setup()

print('setup_done')
13 changes: 13 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,16 @@ def test_configuration_argument_in_cli(self):
proc = subprocess.Popen(['django-cadmin', 'runserver', '--help'],
stdout=subprocess.PIPE)
self.assertIn('--configuration', proc.communicate()[0].decode('utf-8'))

def test_django_setup_only_called_once(self):
proc = subprocess.Popen(
[sys.executable, os.path.join(os.path.dirname(__file__),
'setup_test.py')],
stdout=subprocess.PIPE)
res = proc.communicate()
stdout = res[0].decode('utf-8')

self.assertIn('setup_1', stdout)
self.assertIn('setup_2', stdout)
self.assertIn('setup_done', stdout)
self.assertEqual(proc.returncode, 0)
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ deps =
dj20: django>=2.0a1,<2.1
dj21: django>=2.1a1,<2.2
djmaster: https://github.com/django/django/archive/master.tar.gz#egg=django
py27,pypy: mock

commands =
python --version
coverage run {envbindir}/django-cadmin test -v2 {posargs:tests}
coverage combine
coverage combine . tests/docs
coverage report -m --skip-covered

[testenv:readme-py27]
Expand Down

0 comments on commit 5ea0db0

Please sign in to comment.