from test
import test_support
import sys
, os
, uu
, cStringIO
from StringIO
import StringIO
plaintext
= "The smooth-scaled python crept over the sleeping dog\n"
M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
encodedtextwrapped
= "begin %03o %s\n" + encodedtext
.replace("%", "%%") + "\n \nend\n"
class UUTest(unittest
.TestCase
):
inp
= cStringIO
.StringIO(plaintext
)
out
= cStringIO
.StringIO()
uu
.encode(inp
, out
, "t1")
self
.assertEqual(out
.getvalue(), encodedtextwrapped
% (0666, "t1"))
inp
= cStringIO
.StringIO(plaintext
)
out
= cStringIO
.StringIO()
uu
.encode(inp
, out
, "t1", 0644)
self
.assertEqual(out
.getvalue(), encodedtextwrapped
% (0644, "t1"))
inp
= cStringIO
.StringIO(encodedtextwrapped
% (0666, "t1"))
out
= cStringIO
.StringIO()
self
.assertEqual(out
.getvalue(), plaintext
)
inp
= cStringIO
.StringIO(
"UUencoded files may contain many lines,\n" +
"even some that have 'begin' in them.\n" +
encodedtextwrapped
% (0666, "t1")
out
= cStringIO
.StringIO()
self
.assertEqual(out
.getvalue(), plaintext
)
def test_truncatedinput(self
):
inp
= cStringIO
.StringIO("begin 644 t1\n" + encodedtext
)
out
= cStringIO
.StringIO()
self
.fail("No exception thrown")
self
.assertEqual(str(e
), "Truncated input file")
def test_missingbegin(self
):
inp
= cStringIO
.StringIO("")
out
= cStringIO
.StringIO()
self
.fail("No exception thrown")
self
.assertEqual(str(e
), "No valid begin line found in input file")
class UUStdIOTest(unittest
.TestCase
):
sys
.stdin
= cStringIO
.StringIO(plaintext
)
sys
.stdout
= cStringIO
.StringIO()
uu
.encode("-", "-", "t1", 0666)
encodedtextwrapped
% (0666, "t1")
sys
.stdin
= cStringIO
.StringIO(encodedtextwrapped
% (0666, "t1"))
sys
.stdout
= cStringIO
.StringIO()
self
.assertEqual(sys
.stdout
.getvalue(), plaintext
)
class UUFileTest(unittest
.TestCase
):
except (SystemExit, KeyboardInterrupt):
except (SystemExit, KeyboardInterrupt):
self
.tmpin
= test_support
.TESTFN
+ "i"
self
.tmpout
= test_support
.TESTFN
+ "o"
fin
= open(self
.tmpin
, 'wb')
fin
= open(self
.tmpin
, 'rb')
fout
= open(self
.tmpout
, 'w')
uu
.encode(fin
, fout
, self
.tmpin
, mode
=0644)
fout
= open(self
.tmpout
, 'r')
self
.assertEqual(s
, encodedtextwrapped
% (0644, self
.tmpin
))
f
= open(self
.tmpin
, 'wb')
f
.write(encodedtextwrapped
% (0644, self
.tmpout
))
f
= open(self
.tmpin
, 'rb')
f
= open(self
.tmpout
, 'r')
self
.assertEqual(s
, plaintext
)
# XXX is there an xp way to verify the mode?
def test_decodetwice(self
):
# Verify that decode() will refuse to overwrite an existing file
f
= cStringIO
.StringIO(encodedtextwrapped
% (0644, self
.tmpout
))
f
= open(self
.tmpin
, 'rb')
f
= open(self
.tmpin
, 'rb')
self
.assertRaises(uu
.Error
, uu
.decode
, f
)
test_support
.run_unittest(UUTest
, UUStdIOTest
, UUFileTest
)