From a797b7dba0644558ca7bf825b22268f2b4d9d3b5 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sun, 13 Feb 2011 01:56:12 +0000 Subject: [PATCH] Fixed #14130 -- Made manage.py error reporting more useful when the settings.py file triggers import errors (in new projects). Thanks Setok for the report, mk and steph for their work. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15522 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/conf/project_template/manage.py | 7 +++-- tests/regressiontests/admin_scripts/tests.py | 29 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/django/conf/project_template/manage.py b/django/conf/project_template/manage.py index 5e78ea979e..3e4eedc9ff 100755 --- a/django/conf/project_template/manage.py +++ b/django/conf/project_template/manage.py @@ -1,11 +1,14 @@ #!/usr/bin/env python from django.core.management import execute_manager +import imp try: - import settings # Assumed to be in the same directory. + imp.find_module('settings') # Assumed to be in the same directory. except ImportError: import sys - sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__) + sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__) sys.exit(1) +import settings + if __name__ == "__main__": execute_manager(settings) diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py index 87a6877e04..5c2f5d7fb6 100644 --- a/tests/regressiontests/admin_scripts/tests.py +++ b/tests/regressiontests/admin_scripts/tests.py @@ -958,6 +958,35 @@ class ManageMultipleSettings(AdminScriptTestCase): self.assertNoOutput(out) self.assertOutput(err, "Unknown command: 'noargs_command'") +class ManageSettingsWithImportError(AdminScriptTestCase): + """Tests for manage.py when using the default settings.py file + with an import error. Ticket #14130. + """ + def setUp(self): + self.write_settings_with_import_error('settings.py') + + def tearDown(self): + self.remove_settings('settings.py') + + def write_settings_with_import_error(self, filename, apps=None, is_dir=False, sdict=None): + test_dir = os.path.dirname(os.path.dirname(__file__)) + if is_dir: + settings_dir = os.path.join(test_dir,filename) + os.mkdir(settings_dir) + settings_file = open(os.path.join(settings_dir,'__init__.py'), 'w') + else: + settings_file = open(os.path.join(test_dir, filename), 'w') + settings_file.write('# Settings file automatically generated by regressiontests.admin_scripts test case\n') + settings_file.write('# The next line will cause an import error:\nimport foo42bar\n') + + settings_file.close() + + def test_builtin_command(self): + "import error: manage.py builtin commands shows useful diagnostic info when settings with import errors is provided" + args = ['sqlall','admin_scripts'] + out, err = self.run_manage(args) + self.assertNoOutput(out) + self.assertOutput(err, "ImportError: No module named foo42bar") class ManageValidate(AdminScriptTestCase): def tearDown(self):