Skip to content

Commit

Permalink
Fixed #9751: admin scripts now calculate the project directory correc…
Browse files Browse the repository at this point in the history
…tly when the settings module is a directory with an ``__init__.py``. Thanks to Eric Holscher.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10751 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed May 12, 2009
1 parent 9140f97 commit d20a083
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 12 deletions.
17 changes: 10 additions & 7 deletions django/core/management/__init__.py
Expand Up @@ -105,10 +105,9 @@ def get_commands():
# Find the project directory # Find the project directory
try: try:
from django.conf import settings from django.conf import settings
module = import_module(settings.SETTINGS_MODULE.split('.', 1)[0]) module = import_module(settings.SETTINGS_MODULE)
project_directory = setup_environ(module, project_directory = setup_environ(module, settings.SETTINGS_MODULE)
settings.SETTINGS_MODULE) except (AttributeError, EnvironmentError, ImportError, KeyError):
except (AttributeError, EnvironmentError, ImportError):
project_directory = None project_directory = None


# Find and load the management module for each installed app. # Find and load the management module for each installed app.
Expand Down Expand Up @@ -156,11 +155,11 @@ def call_command(name, *args, **options):
raise CommandError, "Unknown command: %r" % name raise CommandError, "Unknown command: %r" % name


# Grab out a list of defaults from the options. optparse does this for us # Grab out a list of defaults from the options. optparse does this for us
# when the script runs from the command line, but since call_command can # when the script runs from the command line, but since call_command can
# be called programatically, we need to simulate the loading and handling # be called programatically, we need to simulate the loading and handling
# of defaults (see #10080 for details). # of defaults (see #10080 for details).
defaults = dict([(o.dest, o.default) defaults = dict([(o.dest, o.default)
for o in klass.option_list for o in klass.option_list
if o.default is not NO_DEFAULT]) if o.default is not NO_DEFAULT])
defaults.update(options) defaults.update(options)


Expand Down Expand Up @@ -316,7 +315,11 @@ def setup_environ(settings_mod, original_settings_path=None):
# Add this project to sys.path so that it's importable in the conventional # Add this project to sys.path so that it's importable in the conventional
# way. For example, if this file (manage.py) lives in a directory # way. For example, if this file (manage.py) lives in a directory
# "myproject", this code would add "/path/to/myproject" to sys.path. # "myproject", this code would add "/path/to/myproject" to sys.path.
project_directory, settings_filename = os.path.split(settings_mod.__file__) if '__init__.py' in settings_mod.__file__:
p = os.path.dirname(settings_mod.__file__)
else:
p = settings_mod.__file__
project_directory, settings_filename = os.path.split(p)
if project_directory == os.curdir or not project_directory: if project_directory == os.curdir or not project_directory:
project_directory = os.getcwd() project_directory = os.getcwd()
project_name = os.path.basename(project_directory) project_name = os.path.basename(project_directory)
Expand Down
81 changes: 76 additions & 5 deletions tests/regressiontests/admin_scripts/tests.py
Expand Up @@ -13,9 +13,14 @@
from django.conf import settings from django.conf import settings


class AdminScriptTestCase(unittest.TestCase): class AdminScriptTestCase(unittest.TestCase):
def write_settings(self, filename, apps=None): def write_settings(self, filename, apps=None, is_dir=False):
test_dir = os.path.dirname(os.path.dirname(__file__)) test_dir = os.path.dirname(os.path.dirname(__file__))
settings_file = open(os.path.join(test_dir,filename), 'w') 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('# Settings file automatically generated by regressiontests.admin_scripts test case\n')
exports = [ exports = [
'DATABASE_ENGINE', 'DATABASE_ENGINE',
Expand All @@ -38,10 +43,13 @@ def write_settings(self, filename, apps=None):


settings_file.close() settings_file.close()


def remove_settings(self, filename): def remove_settings(self, filename, is_dir=False):
test_dir = os.path.dirname(os.path.dirname(__file__)) test_dir = os.path.dirname(os.path.dirname(__file__))
full_name = os.path.join(test_dir, filename) full_name = os.path.join(test_dir, filename)
os.remove(full_name) if is_dir:
shutil.rmtree(full_name)
else:
os.remove(full_name)


# Also try to remove the compiled file; if it exists, it could # Also try to remove the compiled file; if it exists, it could
# mess up later tests that depend upon the .py file not existing # mess up later tests that depend upon the .py file not existing
Expand Down Expand Up @@ -509,6 +517,70 @@ def test_custom_command_with_environment(self):
self.assertNoOutput(err) self.assertNoOutput(err)
self.assertOutput(out, "EXECUTE:NoArgsCommand") self.assertOutput(out, "EXECUTE:NoArgsCommand")



class DjangoAdminSettingsDirectory(AdminScriptTestCase):
"""
A series of tests for django-admin.py when the settings file is in a
directory. (see #9751).
"""

def setUp(self):
self.write_settings('settings', is_dir=True)

def tearDown(self):
self.remove_settings('settings', is_dir=True)

def test_setup_environ(self):
"directory: startapp creates the correct directory"
test_dir = os.path.dirname(os.path.dirname(__file__))
args = ['startapp','settings_test']
out, err = self.run_django_admin(args,'settings')
self.assertNoOutput(err)
self.assertTrue(os.path.exists(os.path.join(test_dir, 'settings_test')))
shutil.rmtree(os.path.join(test_dir, 'settings_test'))

def test_builtin_command(self):
"directory: django-admin builtin commands fail with an import error when no settings provided"
args = ['sqlall','admin_scripts']
out, err = self.run_django_admin(args)
self.assertNoOutput(out)
self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')

def test_builtin_with_bad_settings(self):
"directory: django-admin builtin commands fail if settings file (from argument) doesn't exist"
args = ['sqlall','--settings=bad_settings', 'admin_scripts']
out, err = self.run_django_admin(args)
self.assertOutput(err, "Could not import settings 'bad_settings'")

def test_builtin_with_bad_environment(self):
"directory: django-admin builtin commands fail if settings file (from environment) doesn't exist"
args = ['sqlall','admin_scripts']
out, err = self.run_django_admin(args,'bad_settings')
self.assertNoOutput(out)
self.assertOutput(err, "Could not import settings 'bad_settings'")

def test_custom_command(self):
"directory: django-admin can't execute user commands unless settings are provided"
args = ['noargs_command']
out, err = self.run_django_admin(args)
self.assertNoOutput(out)
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"
args = ['sqlall','--settings=settings', 'admin_scripts']
out, err = self.run_django_admin(args)
self.assertNoOutput(err)
self.assertOutput(out, 'CREATE TABLE')

def test_builtin_with_environment(self):
"directory: django-admin builtin commands succeed if settings are provided in the environment"
args = ['sqlall','admin_scripts']
out, err = self.run_django_admin(args,'settings')
self.assertNoOutput(err)
self.assertOutput(out, 'CREATE TABLE')


########################################################################## ##########################################################################
# MANAGE.PY TESTS # MANAGE.PY TESTS
# This next series of test classes checks the environment processing # This next series of test classes checks the environment processing
Expand Down Expand Up @@ -1075,4 +1147,3 @@ def test_option_then_setting_then_option(self):
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(err) self.assertNoOutput(err)
self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]") self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")

0 comments on commit d20a083

Please sign in to comment.