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:
Ramiro Morales 2011-05-13 12:15:52 +00:00
parent 5ecb88c146
commit 1d7fa75c97
3 changed files with 83 additions and 18 deletions

View File

@ -411,7 +411,11 @@ def setup_environ(settings_mod, original_settings_path=None):
if original_settings_path:
os.environ['DJANGO_SETTINGS_MODULE'] = original_settings_path
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
# avoid some of the path errors new users can have.

View File

@ -37,8 +37,7 @@ class AdminScriptTestCase(unittest.TestCase):
if apps is None:
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:
for k, v in sdict.items():
@ -119,11 +118,12 @@ class AdminScriptTestCase(unittest.TestCase):
os.chdir(test_dir)
try:
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)
stdin, stdout, stderr = (p.stdin, p.stdout, p.stderr)
p.wait()
except ImportError:
stdin, stdout, stderr = os.popen3(cmd)
out, err = stdout.read(), stderr.read()
# 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"
args = ['sqlall','admin_scripts']
out, err = self.run_manage(args,'bad_settings')
self.assertNoOutput(err)
self.assertOutput(out, 'CREATE TABLE')
self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'")
def test_custom_command(self):
"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"
args = ['sqlall','admin_scripts']
out, err = self.run_manage(args,'bad_settings')
self.assertNoOutput(err)
self.assertOutput(out, 'CREATE TABLE')
self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'")
def test_custom_command(self):
"fulldefault: manage.py can execute user commands when default settings are appropriate"
@ -799,7 +799,7 @@ class ManageMinimalSettings(AdminScriptTestCase):
args = ['sqlall','admin_scripts']
out, err = self.run_manage(args,'bad_settings')
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):
"minimal: manage.py can't execute user commands without appropriate settings"
@ -918,12 +918,11 @@ class ManageMultipleSettings(AdminScriptTestCase):
self.assertOutput(out, 'CREATE TABLE')
def test_builtin_with_environment(self):
"multiple: manage.py builtin commands fail if settings are provided in the environment"
# FIXME: This doesn't seem to be the correct output.
"multiple: manage.py can execute builtin commands if settings are provided in the environment"
args = ['sqlall','admin_scripts']
out, err = self.run_manage(args,'alternate_settings')
self.assertNoOutput(out)
self.assertOutput(err, 'App with label admin_scripts could not be found.')
self.assertNoOutput(err)
self.assertOutput(out, 'CREATE TABLE')
def test_builtin_with_bad_settings(self):
"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']
out, err = self.run_manage(args,'bad_settings')
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):
"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"
args = ['noargs_command']
out, err = self.run_manage(args,'alternate_settings')
self.assertNoOutput(out)
self.assertOutput(err, "Unknown command: 'noargs_command'")
self.assertNoOutput(err)
self.assertOutput(out, "EXECUTE:NoArgsCommand")
class ManageSettingsWithImportError(AdminScriptTestCase):
"""Tests for manage.py when using the default settings.py file

View File

@ -1,5 +1,6 @@
from __future__ import with_statement
from django.conf import settings
import os
from django.conf import settings, global_settings
from django.test import TestCase
class SettingsTests(TestCase):
@ -100,3 +101,64 @@ class TrailingSlashURLTests(TestCase):
self.settings_module.MEDIA_URL = 'http://media.foo.com/stupid//'
self.assertEqual('http://media.foo.com/stupid//',
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
)