Ticket #9751: 9751-with-tests.diff

File 9751-with-tests.diff, 7.5 KB (added by Eric Holscher, 15 years ago)

Basic patch, removed the sys.modules import, and it seems to work without all that split() stuff, but if there's a reason that was there, feel free to add it back.

  • django/core/management/__init__.py

    diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
    index f1192fe..026e426 100644
    a b def get_commands():  
    105105        # Find the project directory
    106106        try:
    107107            from django.conf import settings
    108             module = import_module(settings.SETTINGS_MODULE.split('.', 1)[0])
    109             project_directory = setup_environ(module,
    110                                                 settings.SETTINGS_MODULE)
    111         except (AttributeError, EnvironmentError, ImportError):
     108            module = import_module(settings.SETTINGS_MODULE)
     109            project_directory = setup_environ(module, settings.SETTINGS_MODULE)
     110        except (AttributeError, EnvironmentError, ImportError, KeyError):
    112111            project_directory = None
    113112
    114113        # Find and load the management module for each installed app.
    def call_command(name, *args, **options):  
    156155        raise CommandError, "Unknown command: %r" % name
    157156
    158157    # Grab out a list of defaults from the options. optparse does this for us
    159     # when the script runs from the command line, but since call_command can 
     158    # when the script runs from the command line, but since call_command can
    160159    # be called programatically, we need to simulate the loading and handling
    161160    # of defaults (see #10080 for details).
    162161    defaults = dict([(o.dest, o.default)
    163                      for o in klass.option_list 
     162                     for o in klass.option_list
    164163                     if o.default is not NO_DEFAULT])
    165164    defaults.update(options)
    166165
    def setup_environ(settings_mod, original_settings_path=None):  
    316315    # Add this project to sys.path so that it's importable in the conventional
    317316    # way. For example, if this file (manage.py) lives in a directory
    318317    # "myproject", this code would add "/path/to/myproject" to sys.path.
    319     project_directory, settings_filename = os.path.split(settings_mod.__file__)
     318    if '__init__.py' in settings_mod.__file__:
     319        p = os.path.dirname(settings_mod.__file__)
     320    else:
     321        p = settings_mod.__file__
     322    project_directory, settings_filename = os.path.split(p)
    320323    if project_directory == os.curdir or not project_directory:
    321324        project_directory = os.getcwd()
    322325    project_name = os.path.basename(project_directory)
  • tests/regressiontests/admin_scripts/tests.py

    diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py
    index dd2f515..1e15f97 100644
    a b from django import conf, bin, get_version  
    1313from django.conf import settings
    1414
    1515class AdminScriptTestCase(unittest.TestCase):
    16     def write_settings(self, filename, apps=None):
     16    def write_settings(self, filename, apps=None, is_dir=False):
    1717        test_dir = os.path.dirname(os.path.dirname(__file__))
    18         settings_file = open(os.path.join(test_dir,filename), 'w')
     18        if is_dir:
     19            settings_dir = os.path.join(test_dir,filename)
     20            os.mkdir(settings_dir)
     21            settings_file = open(os.path.join(settings_dir,'__init__.py'), 'w')
     22        else:
     23            settings_file = open(os.path.join(test_dir, filename), 'w')
    1924        settings_file.write('# Settings file automatically generated by regressiontests.admin_scripts test case\n')
    2025        exports = [
    2126            'DATABASE_ENGINE',
    class AdminScriptTestCase(unittest.TestCase):  
    3843
    3944        settings_file.close()
    4045
    41     def remove_settings(self, filename):
     46    def remove_settings(self, filename, is_dir=False):
    4247        test_dir = os.path.dirname(os.path.dirname(__file__))
    4348        full_name = os.path.join(test_dir, filename)
    44         os.remove(full_name)
     49        if is_dir:
     50            shutil.rmtree(full_name)
     51        else:
     52            os.remove(full_name)
    4553
    4654        # Also try to remove the compiled file; if it exists, it could
    4755        # mess up later tests that depend upon the .py file not existing
    class DjangoAdminMultipleSettings(AdminScriptTestCase):  
    509517        self.assertNoOutput(err)
    510518        self.assertOutput(out, "EXECUTE:NoArgsCommand")
    511519
     520
     521class DjangoAdminSettingsDirectory(AdminScriptTestCase):
     522    """A series of tests for django-admin.py when the settings file
     523    is in a directory.
     524    """
     525    def setUp(self):
     526        self.write_settings('settings', is_dir=True)
     527
     528    def tearDown(self):
     529        self.remove_settings('settings', is_dir=True)
     530
     531    #Regression test for 9751
     532    def test_setup_environ(self):
     533        "directory: startapp creates the correct directory"
     534        test_dir = os.path.dirname(os.path.dirname(__file__))
     535        args = ['startapp','settings_test']
     536        out, err = self.run_django_admin(args,'settings')
     537        self.assertNoOutput(err)
     538        self.assertTrue(os.path.exists(os.path.join(test_dir, 'settings_test')))
     539        shutil.rmtree(os.path.join(test_dir, 'settings_test'))
     540
     541    def test_builtin_command(self):
     542        "directory: django-admin builtin commands fail with an import error when no settings provided"
     543        args = ['sqlall','admin_scripts']
     544        out, err = self.run_django_admin(args)
     545        self.assertNoOutput(out)
     546        self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
     547
     548    def test_builtin_with_bad_settings(self):
     549        "directory: django-admin builtin commands fail if settings file (from argument) doesn't exist"
     550        args = ['sqlall','--settings=bad_settings', 'admin_scripts']
     551        out, err = self.run_django_admin(args)
     552        self.assertOutput(err, "Could not import settings 'bad_settings'")
     553
     554    def test_builtin_with_bad_environment(self):
     555        "directory: django-admin builtin commands fail if settings file (from environment) doesn't exist"
     556        args = ['sqlall','admin_scripts']
     557        out, err = self.run_django_admin(args,'bad_settings')
     558        self.assertNoOutput(out)
     559        self.assertOutput(err, "Could not import settings 'bad_settings'")
     560
     561    def test_custom_command(self):
     562        "directory: django-admin can't execute user commands unless settings are provided"
     563        args = ['noargs_command']
     564        out, err = self.run_django_admin(args)
     565        self.assertNoOutput(out)
     566        self.assertOutput(err, "Unknown command: 'noargs_command'")
     567
     568    def test_builtin_with_settings(self):
     569        "directory: django-admin builtin commands succeed if settings are provided as argument"
     570        args = ['sqlall','--settings=settings', 'admin_scripts']
     571        out, err = self.run_django_admin(args)
     572        self.assertNoOutput(err)
     573        self.assertOutput(out, 'CREATE TABLE')
     574
     575    def test_builtin_with_environment(self):
     576        "directory: django-admin builtin commands succeed if settings are provided in the environment"
     577        args = ['sqlall','admin_scripts']
     578        out, err = self.run_django_admin(args,'settings')
     579        self.assertNoOutput(err)
     580        self.assertOutput(out, 'CREATE TABLE')
     581
     582
    512583##########################################################################
    513584# MANAGE.PY TESTS
    514585# This next series of test classes checks the environment processing
    class ArgumentOrder(AdminScriptTestCase):  
    10751146        out, err = self.run_manage(args)
    10761147        self.assertNoOutput(err)
    10771148        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')]")
    1078 
Back to Top