Fixed #21122 -- Improved clean up of test temp directory on Windows
- Fixed test that didn't close the files it opened - Caught and handled exception when temp directory cannot be removed
This commit is contained in:
parent
5381317fe3
commit
978e1351a6
|
@ -174,3 +174,6 @@ class FileMoveSafeTests(unittest.TestCase):
|
||||||
|
|
||||||
# should allow it and continue on if allow_overwrite is True
|
# should allow it and continue on if allow_overwrite is True
|
||||||
self.assertIsNone(file_move_safe(self.file_a, self.file_b, allow_overwrite=True))
|
self.assertIsNone(file_move_safe(self.file_a, self.file_b, allow_overwrite=True))
|
||||||
|
|
||||||
|
os.close(handle_a)
|
||||||
|
os.close(handle_b)
|
||||||
|
|
|
@ -9,6 +9,7 @@ import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
def upath(path):
|
def upath(path):
|
||||||
"""
|
"""
|
||||||
Separate version of django.utils._os.upath. The django.utils version isn't
|
Separate version of django.utils._os.upath. The django.utils version isn't
|
||||||
|
@ -181,17 +182,24 @@ def setup(verbosity, test_labels):
|
||||||
|
|
||||||
return state
|
return state
|
||||||
|
|
||||||
|
|
||||||
def teardown(state):
|
def teardown(state):
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
try:
|
||||||
# Removing the temporary TEMP_DIR. Ensure we pass in unicode
|
# Removing the temporary TEMP_DIR. Ensure we pass in unicode
|
||||||
# so that it will successfully remove temp trees containing
|
# so that it will successfully remove temp trees containing
|
||||||
# non-ASCII filenames on Windows. (We're assuming the temp dir
|
# non-ASCII filenames on Windows. (We're assuming the temp dir
|
||||||
# name itself does not contain non-ASCII characters.)
|
# name itself does not contain non-ASCII characters.)
|
||||||
shutil.rmtree(six.text_type(TEMP_DIR))
|
shutil.rmtree(six.text_type(TEMP_DIR))
|
||||||
|
except OSError:
|
||||||
|
print('Failed to remove temp directory: %s' % TEMP_DIR)
|
||||||
|
|
||||||
# Restore the old settings.
|
# Restore the old settings.
|
||||||
for key, value in state.items():
|
for key, value in state.items():
|
||||||
setattr(settings, key, value)
|
setattr(settings, key, value)
|
||||||
|
|
||||||
|
|
||||||
def django_tests(verbosity, interactive, failfast, test_labels):
|
def django_tests(verbosity, interactive, failfast, test_labels):
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
state = setup(verbosity, test_labels)
|
state = setup(verbosity, test_labels)
|
||||||
|
@ -280,6 +288,7 @@ def bisect_tests(bisection_label, options, test_labels):
|
||||||
print("***** Source of error: %s" % test_labels[0])
|
print("***** Source of error: %s" % test_labels[0])
|
||||||
teardown(state)
|
teardown(state)
|
||||||
|
|
||||||
|
|
||||||
def paired_tests(paired_test, options, test_labels):
|
def paired_tests(paired_test, options, test_labels):
|
||||||
state = setup(int(options.verbosity), test_labels)
|
state = setup(int(options.verbosity), test_labels)
|
||||||
|
|
||||||
|
@ -315,6 +324,7 @@ def paired_tests(paired_test, options, test_labels):
|
||||||
print('***** No problem pair found')
|
print('***** No problem pair found')
|
||||||
teardown(state)
|
teardown(state)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
usage = "%prog [options] [module module module ...]"
|
usage = "%prog [options] [module module module ...]"
|
||||||
|
|
Loading…
Reference in New Issue