Fixed #15064 -- Made manage.py honor the existence and value of DJANGO_SETTINGS_MODULE env var. Thanks olau for the report and Shawn Milochik for a patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16222 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
5ecb88c146
commit
1d7fa75c97
|
@ -411,7 +411,11 @@ def setup_environ(settings_mod, original_settings_path=None):
|
||||||
if original_settings_path:
|
if original_settings_path:
|
||||||
os.environ['DJANGO_SETTINGS_MODULE'] = original_settings_path
|
os.environ['DJANGO_SETTINGS_MODULE'] = original_settings_path
|
||||||
else:
|
else:
|
||||||
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
|
# If DJANGO_SETTINGS_MODULE is already set, use it.
|
||||||
|
os.environ['DJANGO_SETTINGS_MODULE'] = os.environ.get(
|
||||||
|
'DJANGO_SETTINGS_MODULE',
|
||||||
|
'%s.%s' % (project_name, settings_name)
|
||||||
|
)
|
||||||
|
|
||||||
# Import the project module. We add the parent directory to PYTHONPATH to
|
# Import the project module. We add the parent directory to PYTHONPATH to
|
||||||
# avoid some of the path errors new users can have.
|
# avoid some of the path errors new users can have.
|
||||||
|
|
|
@ -37,8 +37,7 @@ class AdminScriptTestCase(unittest.TestCase):
|
||||||
if apps is None:
|
if apps is None:
|
||||||
apps = ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts']
|
apps = ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts']
|
||||||
|
|
||||||
if apps:
|
settings_file.write("INSTALLED_APPS = %s\n" % apps)
|
||||||
settings_file.write("INSTALLED_APPS = %s\n" % apps)
|
|
||||||
|
|
||||||
if sdict:
|
if sdict:
|
||||||
for k, v in sdict.items():
|
for k, v in sdict.items():
|
||||||
|
@ -119,11 +118,12 @@ class AdminScriptTestCase(unittest.TestCase):
|
||||||
os.chdir(test_dir)
|
os.chdir(test_dir)
|
||||||
try:
|
try:
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
|
except ImportError:
|
||||||
|
stdin, stdout, stderr = os.popen3(cmd)
|
||||||
|
else:
|
||||||
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
||||||
stdin, stdout, stderr = (p.stdin, p.stdout, p.stderr)
|
stdin, stdout, stderr = (p.stdin, p.stdout, p.stderr)
|
||||||
p.wait()
|
p.wait()
|
||||||
except ImportError:
|
|
||||||
stdin, stdout, stderr = os.popen3(cmd)
|
|
||||||
out, err = stdout.read(), stderr.read()
|
out, err = stdout.read(), stderr.read()
|
||||||
|
|
||||||
# Restore the old environment
|
# Restore the old environment
|
||||||
|
@ -665,8 +665,8 @@ class ManageDefaultSettings(AdminScriptTestCase):
|
||||||
"default: manage.py builtin commands fail if settings file (from environment) doesn't exist"
|
"default: manage.py builtin commands fail if settings file (from environment) doesn't exist"
|
||||||
args = ['sqlall','admin_scripts']
|
args = ['sqlall','admin_scripts']
|
||||||
out, err = self.run_manage(args,'bad_settings')
|
out, err = self.run_manage(args,'bad_settings')
|
||||||
self.assertNoOutput(err)
|
self.assertNoOutput(out)
|
||||||
self.assertOutput(out, 'CREATE TABLE')
|
self.assertOutput(err, "Could not import settings 'bad_settings'")
|
||||||
|
|
||||||
def test_custom_command(self):
|
def test_custom_command(self):
|
||||||
"default: manage.py can execute user commands when default settings are appropriate"
|
"default: manage.py can execute user commands when default settings are appropriate"
|
||||||
|
@ -732,8 +732,8 @@ class ManageFullPathDefaultSettings(AdminScriptTestCase):
|
||||||
"fulldefault: manage.py builtin commands fail if settings file (from environment) doesn't exist"
|
"fulldefault: manage.py builtin commands fail if settings file (from environment) doesn't exist"
|
||||||
args = ['sqlall','admin_scripts']
|
args = ['sqlall','admin_scripts']
|
||||||
out, err = self.run_manage(args,'bad_settings')
|
out, err = self.run_manage(args,'bad_settings')
|
||||||
self.assertNoOutput(err)
|
self.assertNoOutput(out)
|
||||||
self.assertOutput(out, 'CREATE TABLE')
|
self.assertOutput(err, "Could not import settings 'bad_settings'")
|
||||||
|
|
||||||
def test_custom_command(self):
|
def test_custom_command(self):
|
||||||
"fulldefault: manage.py can execute user commands when default settings are appropriate"
|
"fulldefault: manage.py can execute user commands when default settings are appropriate"
|
||||||
|
@ -799,7 +799,7 @@ class ManageMinimalSettings(AdminScriptTestCase):
|
||||||
args = ['sqlall','admin_scripts']
|
args = ['sqlall','admin_scripts']
|
||||||
out, err = self.run_manage(args,'bad_settings')
|
out, err = self.run_manage(args,'bad_settings')
|
||||||
self.assertNoOutput(out)
|
self.assertNoOutput(out)
|
||||||
self.assertOutput(err, 'App with label admin_scripts could not be found')
|
self.assertOutput(err, "Could not import settings 'bad_settings'")
|
||||||
|
|
||||||
def test_custom_command(self):
|
def test_custom_command(self):
|
||||||
"minimal: manage.py can't execute user commands without appropriate settings"
|
"minimal: manage.py can't execute user commands without appropriate settings"
|
||||||
|
@ -918,12 +918,11 @@ class ManageMultipleSettings(AdminScriptTestCase):
|
||||||
self.assertOutput(out, 'CREATE TABLE')
|
self.assertOutput(out, 'CREATE TABLE')
|
||||||
|
|
||||||
def test_builtin_with_environment(self):
|
def test_builtin_with_environment(self):
|
||||||
"multiple: manage.py builtin commands fail if settings are provided in the environment"
|
"multiple: manage.py can execute builtin commands if settings are provided in the environment"
|
||||||
# FIXME: This doesn't seem to be the correct output.
|
|
||||||
args = ['sqlall','admin_scripts']
|
args = ['sqlall','admin_scripts']
|
||||||
out, err = self.run_manage(args,'alternate_settings')
|
out, err = self.run_manage(args,'alternate_settings')
|
||||||
self.assertNoOutput(out)
|
self.assertNoOutput(err)
|
||||||
self.assertOutput(err, 'App with label admin_scripts could not be found.')
|
self.assertOutput(out, 'CREATE TABLE')
|
||||||
|
|
||||||
def test_builtin_with_bad_settings(self):
|
def test_builtin_with_bad_settings(self):
|
||||||
"multiple: manage.py builtin commands fail if settings file (from argument) doesn't exist"
|
"multiple: manage.py builtin commands fail if settings file (from argument) doesn't exist"
|
||||||
|
@ -937,7 +936,7 @@ class ManageMultipleSettings(AdminScriptTestCase):
|
||||||
args = ['sqlall','admin_scripts']
|
args = ['sqlall','admin_scripts']
|
||||||
out, err = self.run_manage(args,'bad_settings')
|
out, err = self.run_manage(args,'bad_settings')
|
||||||
self.assertNoOutput(out)
|
self.assertNoOutput(out)
|
||||||
self.assertOutput(err, "App with label admin_scripts could not be found")
|
self.assertOutput(err, "Could not import settings 'bad_settings'")
|
||||||
|
|
||||||
def test_custom_command(self):
|
def test_custom_command(self):
|
||||||
"multiple: manage.py can't execute user commands using default settings"
|
"multiple: manage.py can't execute user commands using default settings"
|
||||||
|
@ -957,8 +956,8 @@ class ManageMultipleSettings(AdminScriptTestCase):
|
||||||
"multiple: manage.py can execute user commands if settings are provided in environment"
|
"multiple: manage.py can execute user commands if settings are provided in environment"
|
||||||
args = ['noargs_command']
|
args = ['noargs_command']
|
||||||
out, err = self.run_manage(args,'alternate_settings')
|
out, err = self.run_manage(args,'alternate_settings')
|
||||||
self.assertNoOutput(out)
|
self.assertNoOutput(err)
|
||||||
self.assertOutput(err, "Unknown command: 'noargs_command'")
|
self.assertOutput(out, "EXECUTE:NoArgsCommand")
|
||||||
|
|
||||||
class ManageSettingsWithImportError(AdminScriptTestCase):
|
class ManageSettingsWithImportError(AdminScriptTestCase):
|
||||||
"""Tests for manage.py when using the default settings.py file
|
"""Tests for manage.py when using the default settings.py file
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
from django.conf import settings
|
import os
|
||||||
|
from django.conf import settings, global_settings
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
class SettingsTests(TestCase):
|
class SettingsTests(TestCase):
|
||||||
|
@ -100,3 +101,64 @@ class TrailingSlashURLTests(TestCase):
|
||||||
self.settings_module.MEDIA_URL = 'http://media.foo.com/stupid//'
|
self.settings_module.MEDIA_URL = 'http://media.foo.com/stupid//'
|
||||||
self.assertEqual('http://media.foo.com/stupid//',
|
self.assertEqual('http://media.foo.com/stupid//',
|
||||||
self.settings_module.MEDIA_URL)
|
self.settings_module.MEDIA_URL)
|
||||||
|
|
||||||
|
|
||||||
|
class EnvironmentVariableTest(TestCase):
|
||||||
|
"""
|
||||||
|
Ensures proper settings file is used in setup_environ if
|
||||||
|
DJANGO_SETTINGS_MODULE is set in the environment.
|
||||||
|
"""
|
||||||
|
def setUp(self):
|
||||||
|
self.original_value = os.environ.get('DJANGO_SETTINGS_MODULE')
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
if self.original_value:
|
||||||
|
os.environ['DJANGO_SETTINGS_MODULE'] = self.original_value
|
||||||
|
elif 'DJANGO_SETTINGS_MODULE' in os.environ:
|
||||||
|
del(os.environ['DJANGO_SETTINGS_MODULE'])
|
||||||
|
|
||||||
|
def test_env_var_used(self):
|
||||||
|
"""
|
||||||
|
If the environment variable is set, do not ignore it. However, the
|
||||||
|
kwarg original_settings_path takes precedence.
|
||||||
|
|
||||||
|
This tests both plus the default (neither set).
|
||||||
|
"""
|
||||||
|
from django.core.management import setup_environ
|
||||||
|
|
||||||
|
# whatever was already there
|
||||||
|
original_module = os.environ.get(
|
||||||
|
'DJANGO_SETTINGS_MODULE',
|
||||||
|
'the default'
|
||||||
|
)
|
||||||
|
|
||||||
|
# environment variable set by user
|
||||||
|
user_override = 'custom.settings'
|
||||||
|
|
||||||
|
# optional argument to setup_environ
|
||||||
|
orig_path = 'original.path'
|
||||||
|
|
||||||
|
# expect default
|
||||||
|
setup_environ(global_settings)
|
||||||
|
self.assertEquals(
|
||||||
|
os.environ.get('DJANGO_SETTINGS_MODULE'),
|
||||||
|
original_module
|
||||||
|
)
|
||||||
|
|
||||||
|
# override with environment variable
|
||||||
|
os.environ['DJANGO_SETTINGS_MODULE'] = user_override
|
||||||
|
setup_environ(global_settings)
|
||||||
|
|
||||||
|
self.assertEquals(
|
||||||
|
os.environ.get('DJANGO_SETTINGS_MODULE'),
|
||||||
|
user_override
|
||||||
|
)
|
||||||
|
|
||||||
|
# pass in original_settings_path (should take precedence)
|
||||||
|
os.environ['DJANGO_SETTINGS_MODULE'] = user_override
|
||||||
|
setup_environ(global_settings, original_settings_path = orig_path)
|
||||||
|
|
||||||
|
self.assertEquals(
|
||||||
|
os.environ.get('DJANGO_SETTINGS_MODULE'),
|
||||||
|
orig_path
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue