bpo-33744: Fix test_uu. (GH-7350) (GH-7352)

Separate tests leaked files or were depended on files leaked in other tests.
(cherry picked from commit 027f95c736)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2018-06-03 08:23:53 -07:00 committed by Serhiy Storchaka
parent 949da9e44b
commit ad4c7954df
1 changed files with 26 additions and 78 deletions

View File

@ -6,7 +6,7 @@ Nick Mathewson
import unittest import unittest
from test import support from test import support
import sys, os import sys
import uu import uu
import io import io
@ -162,113 +162,61 @@ class UUStdIOTest(unittest.TestCase):
class UUFileTest(unittest.TestCase): class UUFileTest(unittest.TestCase):
def _kill(self, f):
# close and remove file
if f is None:
return
try:
f.close()
except (SystemExit, KeyboardInterrupt):
raise
except:
pass
try:
os.unlink(f.name)
except (SystemExit, KeyboardInterrupt):
raise
except:
pass
def setUp(self): def setUp(self):
self.tmpin = support.TESTFN + "i" self.tmpin = support.TESTFN + "i"
self.tmpout = support.TESTFN + "o" self.tmpout = support.TESTFN + "o"
self.addCleanup(support.unlink, self.tmpin)
def tearDown(self): self.addCleanup(support.unlink, self.tmpout)
del self.tmpin
del self.tmpout
def test_encode(self): def test_encode(self):
fin = fout = None with open(self.tmpin, 'wb') as fin:
try:
support.unlink(self.tmpin)
fin = open(self.tmpin, 'wb')
fin.write(plaintext) fin.write(plaintext)
fin.close()
fin = open(self.tmpin, 'rb') with open(self.tmpin, 'rb') as fin:
fout = open(self.tmpout, 'wb') with open(self.tmpout, 'wb') as fout:
uu.encode(fin, fout, self.tmpin, mode=0o644) uu.encode(fin, fout, self.tmpin, mode=0o644)
fin.close()
fout.close()
fout = open(self.tmpout, 'rb') with open(self.tmpout, 'rb') as fout:
s = fout.read() s = fout.read()
fout.close()
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
# in_file and out_file as filenames # in_file and out_file as filenames
uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644) uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644)
fout = open(self.tmpout, 'rb') with open(self.tmpout, 'rb') as fout:
s = fout.read() s = fout.read()
fout.close()
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
finally:
self._kill(fin)
self._kill(fout)
def test_decode(self): def test_decode(self):
f = None with open(self.tmpin, 'wb') as f:
try:
support.unlink(self.tmpin)
f = open(self.tmpin, 'wb')
f.write(encodedtextwrapped(0o644, self.tmpout)) f.write(encodedtextwrapped(0o644, self.tmpout))
f.close()
f = open(self.tmpin, 'rb') with open(self.tmpin, 'rb') as f:
uu.decode(f) uu.decode(f)
f.close()
f = open(self.tmpout, 'rb') with open(self.tmpout, 'rb') as f:
s = f.read() s = f.read()
f.close()
self.assertEqual(s, plaintext) self.assertEqual(s, plaintext)
# XXX is there an xp way to verify the mode? # XXX is there an xp way to verify the mode?
finally:
self._kill(f)
def test_decode_filename(self): def test_decode_filename(self):
f = None with open(self.tmpin, 'wb') as f:
try:
support.unlink(self.tmpin)
f = open(self.tmpin, 'wb')
f.write(encodedtextwrapped(0o644, self.tmpout)) f.write(encodedtextwrapped(0o644, self.tmpout))
f.close()
uu.decode(self.tmpin) uu.decode(self.tmpin)
f = open(self.tmpout, 'rb') with open(self.tmpout, 'rb') as f:
s = f.read() s = f.read()
f.close()
self.assertEqual(s, plaintext) self.assertEqual(s, plaintext)
finally:
self._kill(f)
def test_decodetwice(self): def test_decodetwice(self):
# Verify that decode() will refuse to overwrite an existing file # Verify that decode() will refuse to overwrite an existing file
f = None with open(self.tmpin, 'wb') as f:
try: f.write(encodedtextwrapped(0o644, self.tmpout))
f = io.BytesIO(encodedtextwrapped(0o644, self.tmpout)) with open(self.tmpin, 'rb') as f:
f = open(self.tmpin, 'rb')
uu.decode(f) uu.decode(f)
f.close()
f = open(self.tmpin, 'rb') with open(self.tmpin, 'rb') as f:
self.assertRaises(uu.Error, uu.decode, f) self.assertRaises(uu.Error, uu.decode, f)
f.close()
finally:
self._kill(f)
if __name__=="__main__": if __name__=="__main__":