Fixed #23495 -- Stopped swallowing real settings import error

Thanks papaloizouc for the report.
This commit is contained in:
Claude Paroz 2014-09-17 10:43:45 +02:00
parent 023a3d9915
commit fc2b8ddcc8
2 changed files with 36 additions and 39 deletions

View File

@ -90,14 +90,7 @@ class Settings(BaseSettings):
# store the settings module in case someone later cares # store the settings module in case someone later cares
self.SETTINGS_MODULE = settings_module self.SETTINGS_MODULE = settings_module
try:
mod = importlib.import_module(self.SETTINGS_MODULE) mod = importlib.import_module(self.SETTINGS_MODULE)
except ImportError as e:
raise ImportError(
"Could not import settings '%s' (Is it on sys.path? Is there an "
"import error in the settings file?): %s"
% (self.SETTINGS_MODULE, e)
)
tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS", "LOCALE_PATHS") tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS", "LOCALE_PATHS")
self._explicit_settings = set() self._explicit_settings = set()

View File

@ -183,9 +183,13 @@ class AdminScriptTestCase(unittest.TestCase):
"Utility assertion: assert that the given stream is empty" "Utility assertion: assert that the given stream is empty"
self.assertEqual(len(stream), 0, "Stream should be empty: actually contains '%s'" % stream) self.assertEqual(len(stream), 0, "Stream should be empty: actually contains '%s'" % stream)
def assertOutput(self, stream, msg): def assertOutput(self, stream, msg, regex=False):
"Utility assertion: assert that the given message exists in the output" "Utility assertion: assert that the given message exists in the output"
stream = force_text(stream) stream = force_text(stream)
if regex:
self.assertIsNotNone(re.search(msg, stream),
"'%s' does not match actual output text '%s'" % (msg, stream))
else:
self.assertTrue(msg in stream, "'%s' does not match actual output text '%s'" % (msg, stream)) self.assertTrue(msg in stream, "'%s' does not match actual output text '%s'" % (msg, stream))
def assertNotInOutput(self, stream, msg): def assertNotInOutput(self, stream, msg):
@ -215,14 +219,14 @@ class DjangoAdminNoSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"no settings: django-admin builtin commands fail if settings file (from environment) doesn't exist" "no settings: django-admin builtin commands fail if settings file (from environment) doesn't exist"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args, 'bad_settings') out, err = self.run_django_admin(args, 'bad_settings')
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
class DjangoAdminDefaultSettings(AdminScriptTestCase): class DjangoAdminDefaultSettings(AdminScriptTestCase):
@ -261,14 +265,14 @@ class DjangoAdminDefaultSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"default: django-admin builtin commands fail if settings file (from environment) doesn't exist" "default: django-admin builtin commands fail if settings file (from environment) doesn't exist"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args, 'bad_settings') out, err = self.run_django_admin(args, 'bad_settings')
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_custom_command(self): def test_custom_command(self):
"default: django-admin can't execute user commands if it isn't provided settings" "default: django-admin can't execute user commands if it isn't provided settings"
@ -336,14 +340,14 @@ class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"fulldefault: django-admin builtin commands fail if settings file (from environment) doesn't exist" "fulldefault: django-admin builtin commands fail if settings file (from environment) doesn't exist"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args, 'bad_settings') out, err = self.run_django_admin(args, 'bad_settings')
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_custom_command(self): def test_custom_command(self):
"fulldefault: django-admin can't execute user commands unless settings are provided" "fulldefault: django-admin can't execute user commands unless settings are provided"
@ -403,14 +407,14 @@ class DjangoAdminMinimalSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"minimal: django-admin builtin commands fail if settings file (from environment) doesn't exist" "minimal: django-admin builtin commands fail if settings file (from environment) doesn't exist"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args, 'bad_settings') out, err = self.run_django_admin(args, 'bad_settings')
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_custom_command(self): def test_custom_command(self):
"minimal: django-admin can't execute user commands unless settings are provided" "minimal: django-admin can't execute user commands unless settings are provided"
@ -470,14 +474,14 @@ class DjangoAdminAlternateSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist" "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args, 'bad_settings') out, err = self.run_django_admin(args, 'bad_settings')
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_custom_command(self): def test_custom_command(self):
"alternate: django-admin can't execute user commands unless settings are provided" "alternate: django-admin can't execute user commands unless settings are provided"
@ -540,14 +544,14 @@ class DjangoAdminMultipleSettings(AdminScriptTestCase):
"alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist" "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist" "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args, 'bad_settings') out, err = self.run_django_admin(args, 'bad_settings')
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_custom_command(self): def test_custom_command(self):
"alternate: django-admin can't execute user commands unless settings are provided" "alternate: django-admin can't execute user commands unless settings are provided"
@ -614,14 +618,14 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase):
"directory: django-admin builtin commands fail if settings file (from argument) doesn't exist" "directory: django-admin builtin commands fail if settings file (from argument) doesn't exist"
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"directory: django-admin builtin commands fail if settings file (from environment) doesn't exist" "directory: django-admin builtin commands fail if settings file (from environment) doesn't exist"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args, 'bad_settings') out, err = self.run_django_admin(args, 'bad_settings')
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_custom_command(self): def test_custom_command(self):
"directory: django-admin can't execute user commands unless settings are provided" "directory: django-admin can't execute user commands unless settings are provided"
@ -659,21 +663,21 @@ class ManageNoSettings(AdminScriptTestCase):
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'test_project.settings'") self.assertOutput(err, "No module named '?(test_project\.)?settings'?", regex=True)
def test_builtin_with_bad_settings(self): def test_builtin_with_bad_settings(self):
"no settings: manage.py builtin commands fail if settings file (from argument) doesn't exist" "no settings: manage.py builtin commands fail if settings file (from argument) doesn't exist"
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"no settings: manage.py builtin commands fail if settings file (from environment) doesn't exist" "no settings: 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(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
class ManageDefaultSettings(AdminScriptTestCase): class ManageDefaultSettings(AdminScriptTestCase):
@ -712,14 +716,14 @@ class ManageDefaultSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"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(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
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"
@ -779,14 +783,14 @@ class ManageFullPathDefaultSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"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(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
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"
@ -846,14 +850,14 @@ class ManageMinimalSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"minimal: manage.py builtin commands fail if settings file (from environment) doesn't exist" "minimal: 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(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
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"
@ -892,7 +896,7 @@ class ManageAlternateSettings(AdminScriptTestCase):
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'test_project.settings'") self.assertOutput(err, "No module named '?(test_project\.)?settings'?", regex=True)
def test_builtin_with_settings(self): def test_builtin_with_settings(self):
"alternate: manage.py builtin commands work with settings provided as argument" "alternate: manage.py builtin commands work with settings provided as argument"
@ -917,21 +921,21 @@ class ManageAlternateSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"alternate: manage.py builtin commands fail if settings file (from environment) doesn't exist" "alternate: 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(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_custom_command(self): def test_custom_command(self):
"alternate: manage.py can't execute user commands without settings" "alternate: manage.py can't execute user commands without settings"
args = ['noargs_command'] args = ['noargs_command']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'test_project.settings'") self.assertOutput(err, "No module named '?(test_project\.)?settings'?", regex=True)
def test_custom_command_with_settings(self): def test_custom_command_with_settings(self):
"alternate: manage.py can execute user commands if settings are provided as argument" "alternate: manage.py can execute user commands if settings are provided as argument"
@ -995,14 +999,14 @@ class ManageMultipleSettings(AdminScriptTestCase):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
def test_builtin_with_bad_environment(self): def test_builtin_with_bad_environment(self):
"multiple: manage.py builtin commands fail if settings file (from environment) doesn't exist" "multiple: 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(out) self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'") self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
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"