Code

Ticket #15064: 15064_setup_teardown.diff

File 15064_setup_teardown.diff, 3.8 KB (added by ShawnMilo, 3 years ago)

TestCase setUp and tearDown to ensure environment is not changed.

Line 
1diff --git django/core/management/__init__.py django/core/management/__init__.py
2index edc36f4..ec1c0d1 100644
3--- django/core/management/__init__.py
4+++ django/core/management/__init__.py
5@@ -411,7 +411,11 @@ def setup_environ(settings_mod, original_settings_path=None):
6     if original_settings_path:
7         os.environ['DJANGO_SETTINGS_MODULE'] = original_settings_path
8     else:
9-        os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
10+        #If DJANGO_SETTINGS_MODULE is already set, use it.
11+        os.environ['DJANGO_SETTINGS_MODULE'] = os.environ.get(
12+            'DJANGO_SETTINGS_MODULE',
13+            '%s.%s' % (project_name, settings_name)
14+        )
15 
16     # Import the project module. We add the parent directory to PYTHONPATH to
17     # avoid some of the path errors new users can have.
18diff --git tests/regressiontests/settings_tests/tests.py tests/regressiontests/settings_tests/tests.py
19index 12e1e2d..db9ff5a 100644
20--- tests/regressiontests/settings_tests/tests.py
21+++ tests/regressiontests/settings_tests/tests.py
22@@ -1,4 +1,5 @@
23-from django.conf import settings
24+import os
25+from django.conf import settings, global_settings
26 from django.utils import unittest
27 
28 
29@@ -75,3 +76,83 @@ class TrailingSlashURLTests(unittest.TestCase):
30         self.settings_module.MEDIA_URL = 'http://media.foo.com/stupid//'
31         self.assertEqual('http://media.foo.com/stupid//',
32                          self.settings_module.MEDIA_URL)
33+
34+
35+class EnvironmentVariableTest(unittest.TestCase):
36+    """
37+    Ensures proper settings file is used in
38+    setup_environ if DJANGO_SETTINGS_MODULE
39+    is set in the environment.
40+    """
41+    def setUp(self):
42+        """
43+        Make sure DJANGO_SETTINGS_MODULE is not
44+        permanently changed by these tests.
45+        """
46+        def env_restore_func():
47+            original_value = os.environ.get('DJANGO_SETTINGS_MODULE')
48+
49+            def blank_key():
50+                if 'DJANGO_SETTINGS_MODULE' in os.environ:
51+                    del(os.environ['DJANGO_SETTINGS_MODULE'])
52+
53+            def restore_current():
54+                os.environ['DJANGO_SETTINGS_MODULE'] = original_value
55+
56+            if original_value:
57+                return restore_current
58+            else:
59+                return blank_key
60+               
61+        self.restore_env = env_restore_func()
62+
63+    def test_env_var_used(self):
64+        """
65+        If the environment variable is set, do
66+        not ignore it. However, the kwarg
67+        original_settings_path takes precedence.
68+
69+        This tests both plus the default (neither set).
70+        """
71+        from django.core.management import setup_environ
72+
73+        #whatever was already there
74+        original_module =  os.environ.get(
75+            'DJANGO_SETTINGS_MODULE',
76+            'the default'
77+        )
78+
79+        #environment variable set by user
80+        user_override = 'custom.settings'
81+
82+        #optional argument to setup_environ
83+        orig_path = 'original.path'
84+
85+        #expect default
86+        setup_environ(global_settings)
87+        self.assertEquals(
88+            os.environ.get('DJANGO_SETTINGS_MODULE'),
89+            original_module
90+        )
91+
92+        #override with environment variable
93+        os.environ['DJANGO_SETTINGS_MODULE'] = user_override
94+        setup_environ(global_settings)
95+
96+        self.assertEquals(
97+            os.environ.get('DJANGO_SETTINGS_MODULE'),
98+            user_override
99+        )
100+
101+        #pass in original_settings_path (takes precedence)
102+        os.environ['DJANGO_SETTINGS_MODULE'] = user_override
103+        setup_environ(global_settings, original_settings_path = orig_path)
104+
105+        self.assertEquals(
106+            os.environ.get('DJANGO_SETTINGS_MODULE'),
107+            orig_path
108+        )
109+
110+    def tearDown(self):
111+        self.restore_env()
112+