Avoided modifying current environment in admin_scripts tests
This commit is contained in:
parent
8446a2438f
commit
56c80e8b44
|
@ -122,35 +122,29 @@ class AdminScriptTestCase(unittest.TestCase):
|
||||||
django_dir = os.path.dirname(tests_dir)
|
django_dir = os.path.dirname(tests_dir)
|
||||||
ext_backend_base_dirs = self._ext_backend_paths()
|
ext_backend_base_dirs = self._ext_backend_paths()
|
||||||
|
|
||||||
# Remember the old environment
|
# Define a temporary environment for the subprocess
|
||||||
old_django_settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', None)
|
test_environ = os.environ.copy()
|
||||||
if sys.platform.startswith('java'):
|
if sys.platform.startswith('java'):
|
||||||
python_path_var_name = 'JYTHONPATH'
|
python_path_var_name = 'JYTHONPATH'
|
||||||
else:
|
else:
|
||||||
python_path_var_name = 'PYTHONPATH'
|
python_path_var_name = 'PYTHONPATH'
|
||||||
|
|
||||||
old_python_path = os.environ.get(python_path_var_name, None)
|
|
||||||
old_cwd = os.getcwd()
|
old_cwd = os.getcwd()
|
||||||
|
|
||||||
# Set the test environment
|
# Set the test environment
|
||||||
if settings_file:
|
if settings_file:
|
||||||
os.environ['DJANGO_SETTINGS_MODULE'] = settings_file
|
test_environ['DJANGO_SETTINGS_MODULE'] = settings_file
|
||||||
elif 'DJANGO_SETTINGS_MODULE' in os.environ:
|
elif 'DJANGO_SETTINGS_MODULE' in test_environ:
|
||||||
del os.environ['DJANGO_SETTINGS_MODULE']
|
del test_environ['DJANGO_SETTINGS_MODULE']
|
||||||
python_path = [base_dir, django_dir, tests_dir]
|
python_path = [base_dir, django_dir, tests_dir]
|
||||||
python_path.extend(ext_backend_base_dirs)
|
python_path.extend(ext_backend_base_dirs)
|
||||||
os.environ[python_path_var_name] = os.pathsep.join(python_path)
|
test_environ[python_path_var_name] = os.pathsep.join(python_path)
|
||||||
|
|
||||||
# Move to the test directory and run
|
# Move to the test directory and run
|
||||||
os.chdir(test_dir)
|
os.chdir(test_dir)
|
||||||
out, err = subprocess.Popen([sys.executable, script] + args,
|
out, err = subprocess.Popen([sys.executable, script] + args,
|
||||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||||
universal_newlines=True).communicate()
|
env=test_environ, universal_newlines=True).communicate()
|
||||||
# Restore the old environment
|
|
||||||
if old_django_settings_module:
|
|
||||||
os.environ['DJANGO_SETTINGS_MODULE'] = old_django_settings_module
|
|
||||||
if old_python_path:
|
|
||||||
os.environ[python_path_var_name] = old_python_path
|
|
||||||
# Move back to the old working directory
|
# Move back to the old working directory
|
||||||
os.chdir(old_cwd)
|
os.chdir(old_cwd)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue