Ticket #15064: 15064_DJANGO_SETTINGS_MODULE_ignored_2.diff

File 15064_DJANGO_SETTINGS_MODULE_ignored_2.diff, 3.0 KB (added by ShawnMilo, 14 years ago)

Restored os.environ variable to original value after tests. Removed whitespace.

  • django/core/management/__init__.py

    diff --git django/core/management/__init__.py django/core/management/__init__.py
    index edc36f4..ec1c0d1 100644
    def setup_environ(settings_mod, original_settings_path=None):  
    411411    if original_settings_path:
    412412        os.environ['DJANGO_SETTINGS_MODULE'] = original_settings_path
    413413    else:
    414         os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
     414        #If DJANGO_SETTINGS_MODULE is already set, use it.
     415        os.environ['DJANGO_SETTINGS_MODULE'] = os.environ.get(
     416            'DJANGO_SETTINGS_MODULE',
     417            '%s.%s' % (project_name, settings_name)
     418        )
    415419
    416420    # Import the project module. We add the parent directory to PYTHONPATH to
    417421    # avoid some of the path errors new users can have.
  • tests/regressiontests/settings_tests/tests.py

    diff --git tests/regressiontests/settings_tests/tests.py tests/regressiontests/settings_tests/tests.py
    index 5a4e8a7..edc0b85 100644
    class TrailingSlashURLTests(unittest.TestCase):  
    7676        self.settings_module.MEDIA_URL = 'http://media.foo.com/stupid//'
    7777        self.assertEqual('http://media.foo.com/stupid//',
    7878                         self.settings_module.MEDIA_URL)
     79
     80
     81class EnvironmentVariableTest(unittest.TestCase):
     82    """
     83    Ensures proper settings file is used.
     84    """
     85
     86    def test_env_var_used(self):
     87        """
     88        If the environment variable is set, do
     89        not ignore it. However, the kwarg
     90        original_settings_path takes precedence.
     91
     92        This tests both plus the default (neither set).
     93        """
     94        import os
     95        from django.core.management import setup_environ
     96
     97        #whatever was already there
     98        original_module =  os.environ.get(
     99            'DJANGO_SETTINGS_MODULE',
     100            'the default'
     101        )
     102
     103        #environment variable set by user
     104        user_override = 'custom.settings'
     105
     106        #optional argument to setup_environ
     107        orig_path = 'original.path'
     108
     109        #expect default
     110        setup_environ(global_settings)
     111        self.assertEquals(
     112            os.environ.get('DJANGO_SETTINGS_MODULE'),
     113            original_module
     114        )
     115
     116        #override with environment variable
     117        os.environ['DJANGO_SETTINGS_MODULE'] = user_override
     118        setup_environ(global_settings)
     119
     120        self.assertEquals(
     121            os.environ.get('DJANGO_SETTINGS_MODULE'),
     122            user_override
     123        )
     124
     125        #pass in original_settings_path (takes precedence)
     126        os.environ['DJANGO_SETTINGS_MODULE'] = user_override
     127        setup_environ(global_settings, original_settings_path = orig_path)
     128
     129        self.assertEquals(
     130            os.environ.get('DJANGO_SETTINGS_MODULE'),
     131            orig_path
     132        )
     133
     134        #return to factory defaults, since this changes
     135        #things beyond just in this test
     136        os.environ['DJANGO_SETTINGS_MODULE'] = original_module
     137
Back to Top