Commit 23999158 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

[2.7] bpo-33751: Fix test_file. (GH-7378) (GH-7445)

testModeStrings and testTruncateOnWindows were depended on
a file leaked in other tests.

Also improve cleaning up after tests.
(cherry picked from commit c2745d2d)
parent 02b4d67a
...@@ -12,7 +12,8 @@ from weakref import proxy ...@@ -12,7 +12,8 @@ from weakref import proxy
import io import io
import _pyio as pyio import _pyio as pyio
from test.test_support import TESTFN, run_unittest from test.support import TESTFN, run_unittest
from test import support
from UserList import UserList from UserList import UserList
class AutoFileTests(unittest.TestCase): class AutoFileTests(unittest.TestCase):
...@@ -24,7 +25,7 @@ class AutoFileTests(unittest.TestCase): ...@@ -24,7 +25,7 @@ class AutoFileTests(unittest.TestCase):
def tearDown(self): def tearDown(self):
if self.f: if self.f:
self.f.close() self.f.close()
os.remove(TESTFN) support.unlink(TESTFN)
def testWeakRefs(self): def testWeakRefs(self):
# verify weak references # verify weak references
...@@ -143,8 +144,12 @@ class PyAutoFileTests(AutoFileTests): ...@@ -143,8 +144,12 @@ class PyAutoFileTests(AutoFileTests):
class OtherFileTests(unittest.TestCase): class OtherFileTests(unittest.TestCase):
def tearDown(self):
support.unlink(TESTFN)
def testModeStrings(self): def testModeStrings(self):
# check invalid mode strings # check invalid mode strings
self.open(TESTFN, 'wb').close()
for mode in ("", "aU", "wU+"): for mode in ("", "aU", "wU+"):
try: try:
f = self.open(TESTFN, mode) f = self.open(TESTFN, mode)
...@@ -191,7 +196,6 @@ class OtherFileTests(unittest.TestCase): ...@@ -191,7 +196,6 @@ class OtherFileTests(unittest.TestCase):
# SF bug <http://www.python.org/sf/801631> # SF bug <http://www.python.org/sf/801631>
# "file.truncate fault on windows" # "file.truncate fault on windows"
os.unlink(TESTFN)
f = self.open(TESTFN, 'wb') f = self.open(TESTFN, 'wb')
try: try:
...@@ -215,7 +219,6 @@ class OtherFileTests(unittest.TestCase): ...@@ -215,7 +219,6 @@ class OtherFileTests(unittest.TestCase):
self.fail("File size after ftruncate wrong %d" % size) self.fail("File size after ftruncate wrong %d" % size)
finally: finally:
f.close() f.close()
os.unlink(TESTFN)
def testIteration(self): def testIteration(self):
# Test the complex interaction when mixing file-iteration and the # Test the complex interaction when mixing file-iteration and the
...@@ -236,7 +239,6 @@ class OtherFileTests(unittest.TestCase): ...@@ -236,7 +239,6 @@ class OtherFileTests(unittest.TestCase):
methods = [("readline", ()), ("read", ()), ("readlines", ()), methods = [("readline", ()), ("read", ()), ("readlines", ()),
("readinto", (array("b", b" "*100),))] ("readinto", (array("b", b" "*100),))]
try:
# Prepare the testfile # Prepare the testfile
bag = self.open(TESTFN, "wb") bag = self.open(TESTFN, "wb")
bag.write(filler * nchunks) bag.write(filler * nchunks)
...@@ -314,8 +316,6 @@ class OtherFileTests(unittest.TestCase): ...@@ -314,8 +316,6 @@ class OtherFileTests(unittest.TestCase):
self.fail("read* failed after next() consumed file") self.fail("read* failed after next() consumed file")
finally: finally:
f.close() f.close()
finally:
os.unlink(TESTFN)
class COtherFileTests(OtherFileTests): class COtherFileTests(OtherFileTests):
open = io.open open = io.open
...@@ -325,14 +325,8 @@ class PyOtherFileTests(OtherFileTests): ...@@ -325,14 +325,8 @@ class PyOtherFileTests(OtherFileTests):
def test_main(): def test_main():
# Historically, these tests have been sloppy about removing TESTFN.
# So get rid of it no matter what.
try:
run_unittest(CAutoFileTests, PyAutoFileTests, run_unittest(CAutoFileTests, PyAutoFileTests,
COtherFileTests, PyOtherFileTests) COtherFileTests, PyOtherFileTests)
finally:
if os.path.exists(TESTFN):
os.unlink(TESTFN)
if __name__ == '__main__': if __name__ == '__main__':
test_main() test_main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment