Ticket #15064: 15064_DJANGO_SETTINGS_MODULE_ignored_update.diff

File 15064_DJANGO_SETTINGS_MODULE_ignored_update.diff, 3.8 KB (added by ShawnMilo, 4 years ago)

Update due to conflict with current trunk.

  • 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 a890460..c06eafb 100644
     
    11from __future__ import with_statement
    2 from django.conf import settings
     2import os
     3from django.conf import settings, global_settings
    34from django.test import TestCase
    45
    56class SettingsTests(TestCase):
    class TrailingSlashURLTests(TestCase): 
    100101        self.settings_module.MEDIA_URL = 'http://media.foo.com/stupid//'
    101102        self.assertEqual('http://media.foo.com/stupid//',
    102103                         self.settings_module.MEDIA_URL)
     104
     105
     106class EnvironmentVariableTest(TestCase):
     107    """
     108    Ensures proper settings file is used in
     109    setup_environ if DJANGO_SETTINGS_MODULE
     110    is set in the environment.
     111    """
     112    def setUp(self):
     113        """
     114        Make sure DJANGO_SETTINGS_MODULE is not
     115        permanently changed by these tests.
     116        """
     117        def env_restore_func():
     118            original_value = os.environ.get('DJANGO_SETTINGS_MODULE')
     119
     120            def blank_key():
     121                if 'DJANGO_SETTINGS_MODULE' in os.environ:
     122                    del(os.environ['DJANGO_SETTINGS_MODULE'])
     123
     124            def restore_current():
     125                os.environ['DJANGO_SETTINGS_MODULE'] = original_value
     126
     127            if original_value:
     128                return restore_current
     129            else:
     130                return blank_key
     131               
     132        self.restore_env = env_restore_func()
     133
     134    def test_env_var_used(self):
     135        """
     136        If the environment variable is set, do
     137        not ignore it. However, the kwarg
     138        original_settings_path takes precedence.
     139
     140        This tests both plus the default (neither set).
     141        """
     142        from django.core.management import setup_environ
     143
     144        #whatever was already there
     145        original_module =  os.environ.get(
     146            'DJANGO_SETTINGS_MODULE',
     147            'the default'
     148        )
     149
     150        #environment variable set by user
     151        user_override = 'custom.settings'
     152
     153        #optional argument to setup_environ
     154        orig_path = 'original.path'
     155
     156        #expect default
     157        setup_environ(global_settings)
     158        self.assertEquals(
     159            os.environ.get('DJANGO_SETTINGS_MODULE'),
     160            original_module
     161        )
     162
     163        #override with environment variable
     164        os.environ['DJANGO_SETTINGS_MODULE'] = user_override
     165        setup_environ(global_settings)
     166
     167        self.assertEquals(
     168            os.environ.get('DJANGO_SETTINGS_MODULE'),
     169            user_override
     170        )
     171
     172        #pass in original_settings_path (takes precedence)
     173        os.environ['DJANGO_SETTINGS_MODULE'] = user_override
     174        setup_environ(global_settings, original_settings_path = orig_path)
     175
     176        self.assertEquals(
     177            os.environ.get('DJANGO_SETTINGS_MODULE'),
     178            orig_path
     179        )
     180
     181    def tearDown(self):
     182        self.restore_env()
     183
Back to Top