From c1893e2839245e75b35597a9200d9ef9c1ad9b46 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 19 Aug 2015 11:58:16 +0200 Subject: [PATCH] Fixed #25285 -- Provided unknown command message with plain django-admin.py Thanks Maxime Lorant for the report and Tim Graham for suggesting the improved comment. --- django/core/management/__init__.py | 10 ++++++++-- tests/admin_scripts/tests.py | 18 ++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index aa43a8a9ca..051747947e 100644 --- a/django/core/management/__init__.py +++ b/django/core/management/__init__.py @@ -177,8 +177,14 @@ class ManagementUtility(object): try: app_name = commands[subcommand] except KeyError: - # This might trigger ImproperlyConfigured (masked in get_commands) - settings.INSTALLED_APPS + if os.environ.get('DJANGO_SETTINGS_MODULE'): + # If `subcommand` is missing due to misconfigured settings, the + # following line will retrigger an ImproperlyConfigured exception + # (get_commands() swallows the original one) so the user is + # informed about it. + settings.INSTALLED_APPS + else: + sys.stderr.write("No Django settings specified.\n") sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % (subcommand, self.prog_name)) sys.exit(1) diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index c635de05df..c9faa82977 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -283,7 +283,8 @@ class DjangoAdminDefaultSettings(AdminScriptTestCase): args = ['noargs_command'] out, err = self.run_django_admin(args) self.assertNoOutput(out) - self.assertOutput(err, "settings are not configured") + self.assertOutput(err, "No Django settings specified") + self.assertOutput(err, "Unknown command: 'noargs_command'") def test_custom_command_with_settings(self): "default: django-admin can execute user commands if settings are provided as argument" @@ -351,7 +352,8 @@ class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase): args = ['noargs_command'] out, err = self.run_django_admin(args) self.assertNoOutput(out) - self.assertOutput(err, "settings are not configured") + self.assertOutput(err, "No Django settings specified") + self.assertOutput(err, "Unknown command: 'noargs_command'") def test_custom_command_with_settings(self): "fulldefault: django-admin can execute user commands if settings are provided as argument" @@ -418,7 +420,8 @@ class DjangoAdminMinimalSettings(AdminScriptTestCase): args = ['noargs_command'] out, err = self.run_django_admin(args) self.assertNoOutput(out) - self.assertOutput(err, "settings are not configured") + self.assertOutput(err, "No Django settings specified") + self.assertOutput(err, "Unknown command: 'noargs_command'") def test_custom_command_with_settings(self): "minimal: django-admin can't execute user commands, even if settings are provided as argument" @@ -485,7 +488,8 @@ class DjangoAdminAlternateSettings(AdminScriptTestCase): args = ['noargs_command'] out, err = self.run_django_admin(args) self.assertNoOutput(out) - self.assertOutput(err, "settings are not configured") + self.assertOutput(err, "No Django settings specified") + self.assertOutput(err, "Unknown command: 'noargs_command'") def test_custom_command_with_settings(self): "alternate: django-admin can execute user commands if settings are provided as argument" @@ -555,7 +559,8 @@ class DjangoAdminMultipleSettings(AdminScriptTestCase): args = ['noargs_command'] out, err = self.run_django_admin(args) self.assertNoOutput(out) - self.assertOutput(err, "settings are not configured") + self.assertOutput(err, "No Django settings specified") + self.assertOutput(err, "Unknown command: 'noargs_command'") def test_custom_command_with_settings(self): "alternate: django-admin can execute user commands if settings are provided as argument" @@ -644,7 +649,8 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase): args = ['noargs_command'] out, err = self.run_django_admin(args) self.assertNoOutput(out) - self.assertOutput(err, "settings are not configured") + self.assertOutput(err, "No Django settings specified") + self.assertOutput(err, "Unknown command: 'noargs_command'") def test_builtin_with_settings(self): "directory: django-admin builtin commands succeed if settings are provided as argument"