Replaced manual current directory management with subprocess's cwd argument.

This commit is contained in:
Jon Dufresne 2019-02-05 08:01:36 -08:00 committed by Tim Graham
parent 4d425abc84
commit 099c36d546
2 changed files with 3 additions and 11 deletions

View File

@ -128,7 +128,6 @@ class AdminScriptTestCase(SimpleTestCase):
# Define a temporary environment for the subprocess # Define a temporary environment for the subprocess
test_environ = os.environ.copy() test_environ = os.environ.copy()
old_cwd = os.getcwd()
# Set the test environment # Set the test environment
if settings_file: if settings_file:
@ -140,17 +139,12 @@ class AdminScriptTestCase(SimpleTestCase):
test_environ['PYTHONPATH'] = os.pathsep.join(python_path) test_environ['PYTHONPATH'] = os.pathsep.join(python_path)
test_environ['PYTHONWARNINGS'] = '' test_environ['PYTHONWARNINGS'] = ''
# Move to the test directory and run return subprocess.Popen(
os.chdir(self.test_dir)
out, err = subprocess.Popen(
[sys.executable, script] + args, [sys.executable, script] + args,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
cwd=self.test_dir,
env=test_environ, universal_newlines=True, env=test_environ, universal_newlines=True,
).communicate() ).communicate()
# Move back to the old working directory
os.chdir(old_cwd)
return out, err
def run_django_admin(self, args, settings_file=None): def run_django_admin(self, args, settings_file=None):
script_dir = os.path.abspath(os.path.join(os.path.dirname(django.__file__), 'bin')) script_dir = os.path.abspath(os.path.join(os.path.dirname(django.__file__), 'bin'))

View File

@ -7,13 +7,11 @@ from . import PostgreSQLSimpleTestCase
class PostgresIntegrationTests(PostgreSQLSimpleTestCase): class PostgresIntegrationTests(PostgreSQLSimpleTestCase):
def test_check(self): def test_check(self):
old_cwd = os.getcwd()
self.addCleanup(lambda: os.chdir(old_cwd))
os.chdir(os.path.dirname(__file__))
result = subprocess.run( result = subprocess.run(
[sys.executable, '-m', 'django', 'check', '--settings', 'integration_settings'], [sys.executable, '-m', 'django', 'check', '--settings', 'integration_settings'],
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
cwd=os.path.dirname(__file__),
) )
stderr = '\n'.join([e.decode() for e in result.stderr.splitlines()]) stderr = '\n'.join([e.decode() for e in result.stderr.splitlines()])
self.assertEqual(result.returncode, 0, msg=stderr) self.assertEqual(result.returncode, 0, msg=stderr)