Commit 411e5a2c authored by Walter Dörwald's avatar Walter Dörwald

Port test_uu.py to PyUnit. From SF patch #662807.

parent 3539f6b8
...@@ -3,155 +3,170 @@ Tests for uu module. ...@@ -3,155 +3,170 @@ Tests for uu module.
Nick Mathewson Nick Mathewson
""" """
from test.test_support import verify, TestFailed, verbose, TESTFN import unittest
import sys, os from test import test_support
import sys, os, uu, cStringIO
import uu import uu
from StringIO import StringIO from StringIO import StringIO
teststr = "The smooth-scaled python crept over the sleeping dog\n" plaintext = "The smooth-scaled python crept over the sleeping dog\n"
expected = """\
encodedtext = """\
M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
(:6YG(&1O9PH """ (:6YG(&1O9PH """
encoded1 = "begin 666 t1\n"+expected+"\n \nend\n"
if verbose: encodedtextwrapped = "begin %03o %s\n" + encodedtext.replace("%", "%%") + "\n \nend\n"
print '1. encode file->file'
inp = StringIO(teststr) class UUTest(unittest.TestCase):
out = StringIO()
uu.encode(inp, out, "t1") def test_encode(self):
verify(out.getvalue() == encoded1) inp = cStringIO.StringIO(plaintext)
inp = StringIO(teststr) out = cStringIO.StringIO()
out = StringIO() uu.encode(inp, out, "t1")
uu.encode(inp, out, "t1", 0644) self.assertEqual(out.getvalue(), encodedtextwrapped % (0666, "t1"))
verify(out.getvalue() == "begin 644 t1\n"+expected+"\n \nend\n") inp = cStringIO.StringIO(plaintext)
out = cStringIO.StringIO()
if verbose: uu.encode(inp, out, "t1", 0644)
print '2. decode file->file' self.assertEqual(out.getvalue(), encodedtextwrapped % (0644, "t1"))
inp = StringIO(encoded1)
out = StringIO() def test_decode(self):
uu.decode(inp, out) inp = cStringIO.StringIO(encodedtextwrapped % (0666, "t1"))
verify(out.getvalue() == teststr) out = cStringIO.StringIO()
inp = StringIO("""UUencoded files may contain many lines, uu.decode(inp, out)
even some that have 'begin' in them.\n"""+encoded1) self.assertEqual(out.getvalue(), plaintext)
out = StringIO() inp = cStringIO.StringIO(
uu.decode(inp, out) "UUencoded files may contain many lines,\n" +
verify(out.getvalue() == teststr) "even some that have 'begin' in them.\n" +
encodedtextwrapped % (0666, "t1")
stdinsave = sys.stdin )
stdoutsave = sys.stdout out = cStringIO.StringIO()
try: uu.decode(inp, out)
if verbose: self.assertEqual(out.getvalue(), plaintext)
print '3. encode stdin->stdout'
sys.stdin = StringIO(teststr) def test_truncatedinput(self):
sys.stdout = StringIO() inp = cStringIO.StringIO("begin 644 t1\n" + encodedtext)
uu.encode("-", "-", "t1", 0666) out = cStringIO.StringIO()
verify(sys.stdout.getvalue() == encoded1) try:
if verbose: uu.decode(inp, out)
print >>stdoutsave, '4. decode stdin->stdout' self.fail("No exception thrown")
sys.stdin = StringIO(encoded1) except uu.Error, e:
sys.stdout = StringIO() self.assertEqual(str(e), "Truncated input file")
uu.decode("-", "-")
verify(sys.stdout.getvalue() == teststr) def test_missingbegin(self):
finally: inp = cStringIO.StringIO("")
sys.stdin = stdinsave out = cStringIO.StringIO()
sys.stdout = stdoutsave try:
uu.decode(inp, out)
if verbose: self.fail("No exception thrown")
print '5. encode file->file' except uu.Error, e:
tmpIn = TESTFN + "i" self.assertEqual(str(e), "No valid begin line found in input file")
tmpOut = TESTFN + "o"
try: class UUStdIOTest(unittest.TestCase):
fin = open(tmpIn, 'wb')
fin.write(teststr) def setUp(self):
fin.close() self.stdin = sys.stdin
self.stdout = sys.stdout
fin = open(tmpIn, 'rb')
fout = open(tmpOut, 'w') def tearDown(self):
uu.encode(fin, fout, tmpIn, mode=0644) sys.stdin = self.stdin
fin.close() sys.stdout = self.stdout
fout.close()
def test_encode(self):
fout = open(tmpOut, 'r') sys.stdin = cStringIO.StringIO(plaintext)
s = fout.read() sys.stdout = cStringIO.StringIO()
fout.close() uu.encode("-", "-", "t1", 0666)
verify(s == 'begin 644 ' + tmpIn + '\n' + expected + '\n \nend\n') self.assertEqual(
sys.stdout.getvalue(),
os.unlink(tmpIn) encodedtextwrapped % (0666, "t1")
if verbose: )
print '6. decode file-> file'
uu.decode(tmpOut) def test_decode(self):
fin = open(tmpIn, 'rb') sys.stdin = cStringIO.StringIO(encodedtextwrapped % (0666, "t1"))
s = fin.read() sys.stdout = cStringIO.StringIO()
fin.close() uu.decode("-", "-")
verify(s == teststr) self.assertEqual(sys.stdout.getvalue(), plaintext)
# XXX is there an xp way to verify the mode?
class UUFileTest(unittest.TestCase):
finally:
try: def _kill(self, f):
fin.close() # close and remove file
except: try:
pass f.close()
try: except (SystemExit, KeyboardInterrupt):
fout.close() raise
except: except:
pass pass
try: try:
os.unlink(tmpIn) os.unlink(f.name)
except: except (SystemExit, KeyboardInterrupt):
pass raise
try: except:
os.unlink(tmpOut) pass
except:
pass def setUp(self):
self.tmpin = test_support.TESTFN + "i"
if verbose: self.tmpout = test_support.TESTFN + "o"
print '7. error: truncated input'
inp = StringIO("begin 644 t1\n"+expected) def tearDown(self):
out = StringIO() del self.tmpin
try: del self.tmpout
uu.decode(inp, out)
raise TestFailed("No exception thrown") def test_encode(self):
except uu.Error, e: try:
verify(str(e) == 'Truncated input file') fin = open(self.tmpin, 'wb')
fin.write(plaintext)
if verbose: fin.close()
print '8. error: missing begin'
inp = StringIO("") fin = open(self.tmpin, 'rb')
out = StringIO() fout = open(self.tmpout, 'w')
try: uu.encode(fin, fout, self.tmpin, mode=0644)
uu.decode(inp, out) fin.close()
raise TestFailed("No exception thrown") fout.close()
except uu.Error, e:
verify(str(e) == 'No valid begin line found in input file') fout = open(self.tmpout, 'r')
s = fout.read()
# Test to verify that decode() will refuse to overwrite an existing file fout.close()
outfile = TESTFN + "out" self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin))
inp = StringIO('Here is a message to be uuencoded') finally:
out = StringIO() self._kill(fin)
uu.encode(inp, out, outfile) self._kill(fout)
out.seek(0)
try: def test_decode(self):
if verbose: try:
print '9. decode w/file not exists is okay' f = open(self.tmpin, 'wb')
uu.decode(out) f.write(encodedtextwrapped % (0644, self.tmpout))
if not os.path.exists(outfile): f.close()
raise TestFailed('uudecode w/ out_file=None failed')
fp = open(outfile) f = open(self.tmpin, 'rb')
data = fp.read() uu.decode(f)
fp.close() f.close()
if data <> inp.getvalue():
raise TestFailed('uudecode stored something weird') f = open(self.tmpout, 'r')
# Try to write it again, which should cause a failure s = f.read()
if verbose: f.close()
print '10. uudecode w/file exists fails' self.assertEqual(s, plaintext)
out.seek(0) # XXX is there an xp way to verify the mode?
try: finally:
uu.decode(out) self._kill(f)
except uu.Error:
pass def test_decodetwice(self):
else: # Verify that decode() will refuse to overwrite an existing file
raise TestFailed('expected to get a "file exists" error') try:
finally: f = cStringIO.StringIO(encodedtextwrapped % (0644, self.tmpout))
try:
os.unlink(outfile) f = open(self.tmpin, 'rb')
except OSError: uu.decode(f)
pass f.close()
f = open(self.tmpin, 'rb')
self.assertRaises(uu.Error, uu.decode, f)
f.close()
finally:
self._kill(f)
def test_main():
test_support.run_unittest(UUTest, UUStdIOTest, UUFileTest)
if __name__=="__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