Convert test_fileinput to use unittest.
This commit is contained in:
parent
69c955f05d
commit
bf61856427
|
@ -3,7 +3,9 @@ Tests for fileinput module.
|
||||||
Nick Mathewson
|
Nick Mathewson
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from test.test_support import verify, verbose, TESTFN, TestFailed
|
import unittest
|
||||||
|
from test.test_support import verbose, TESTFN, run_unittest
|
||||||
|
from test.test_support import unlink as safe_unlink
|
||||||
import sys, os, re
|
import sys, os, re
|
||||||
from StringIO import StringIO
|
from StringIO import StringIO
|
||||||
from fileinput import FileInput, hook_encoded
|
from fileinput import FileInput, hook_encoded
|
||||||
|
@ -22,206 +24,202 @@ def writeTmp(i, lines, mode='w'): # opening in text mode is the default
|
||||||
f.close()
|
f.close()
|
||||||
return name
|
return name
|
||||||
|
|
||||||
pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
|
|
||||||
|
|
||||||
def remove_tempfiles(*names):
|
def remove_tempfiles(*names):
|
||||||
for name in names:
|
for name in names:
|
||||||
try:
|
safe_unlink(name)
|
||||||
os.unlink(name)
|
|
||||||
except:
|
class BufferSizesTests(unittest.TestCase):
|
||||||
pass
|
def test_buffer_sizes(self):
|
||||||
|
# First, run the tests with default and teeny buffer size.
|
||||||
def runTests(t1, t2, t3, t4, bs=0, round=0):
|
for round, bs in (0, 0), (1, 30):
|
||||||
start = 1 + round*6
|
try:
|
||||||
if verbose:
|
t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)])
|
||||||
print '%s. Simple iteration (bs=%s)' % (start+0, bs)
|
t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)])
|
||||||
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
|
t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)])
|
||||||
lines = list(fi)
|
t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)])
|
||||||
fi.close()
|
self.buffer_size_test(t1, t2, t3, t4, bs, round)
|
||||||
verify(len(lines) == 31)
|
finally:
|
||||||
verify(lines[4] == 'Line 5 of file 1\n')
|
remove_tempfiles(t1, t2, t3, t4)
|
||||||
verify(lines[30] == 'Line 1 of file 4\n')
|
|
||||||
verify(fi.lineno() == 31)
|
def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
|
||||||
verify(fi.filename() == t4)
|
pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
|
||||||
|
|
||||||
if verbose:
|
start = 1 + round*6
|
||||||
print '%s. Status variables (bs=%s)' % (start+1, bs)
|
if verbose:
|
||||||
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
|
print '%s. Simple iteration (bs=%s)' % (start+0, bs)
|
||||||
s = "x"
|
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
|
||||||
while s and s != 'Line 6 of file 2\n':
|
|
||||||
s = fi.readline()
|
|
||||||
verify(fi.filename() == t2)
|
|
||||||
verify(fi.lineno() == 21)
|
|
||||||
verify(fi.filelineno() == 6)
|
|
||||||
verify(not fi.isfirstline())
|
|
||||||
verify(not fi.isstdin())
|
|
||||||
|
|
||||||
if verbose:
|
|
||||||
print '%s. Nextfile (bs=%s)' % (start+2, bs)
|
|
||||||
fi.nextfile()
|
|
||||||
verify(fi.readline() == 'Line 1 of file 3\n')
|
|
||||||
verify(fi.lineno() == 22)
|
|
||||||
fi.close()
|
|
||||||
|
|
||||||
if verbose:
|
|
||||||
print '%s. Stdin (bs=%s)' % (start+3, bs)
|
|
||||||
fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
|
|
||||||
savestdin = sys.stdin
|
|
||||||
try:
|
|
||||||
sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
|
|
||||||
lines = list(fi)
|
lines = list(fi)
|
||||||
verify(len(lines) == 33)
|
|
||||||
verify(lines[32] == 'Line 2 of stdin\n')
|
|
||||||
verify(fi.filename() == '<stdin>')
|
|
||||||
fi.nextfile()
|
|
||||||
finally:
|
|
||||||
sys.stdin = savestdin
|
|
||||||
|
|
||||||
if verbose:
|
|
||||||
print '%s. Boundary conditions (bs=%s)' % (start+4, bs)
|
|
||||||
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
|
|
||||||
verify(fi.lineno() == 0)
|
|
||||||
verify(fi.filename() == None)
|
|
||||||
fi.nextfile()
|
|
||||||
verify(fi.lineno() == 0)
|
|
||||||
verify(fi.filename() == None)
|
|
||||||
|
|
||||||
if verbose:
|
|
||||||
print '%s. Inplace (bs=%s)' % (start+5, bs)
|
|
||||||
savestdout = sys.stdout
|
|
||||||
try:
|
|
||||||
fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
|
|
||||||
for line in fi:
|
|
||||||
line = line[:-1].upper()
|
|
||||||
print line
|
|
||||||
fi.close()
|
fi.close()
|
||||||
finally:
|
self.assertEqual(len(lines), 31)
|
||||||
sys.stdout = savestdout
|
self.assertEqual(lines[4], 'Line 5 of file 1\n')
|
||||||
|
self.assertEqual(lines[30], 'Line 1 of file 4\n')
|
||||||
|
self.assertEqual(fi.lineno(), 31)
|
||||||
|
self.assertEqual(fi.filename(), t4)
|
||||||
|
|
||||||
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
|
if verbose:
|
||||||
for line in fi:
|
print '%s. Status variables (bs=%s)' % (start+1, bs)
|
||||||
verify(line[-1] == '\n')
|
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
|
||||||
m = pat.match(line[:-1])
|
s = "x"
|
||||||
verify(m != None)
|
while s and s != 'Line 6 of file 2\n':
|
||||||
verify(int(m.group(1)) == fi.filelineno())
|
s = fi.readline()
|
||||||
fi.close()
|
self.assertEqual(fi.filename(), t2)
|
||||||
|
self.assertEqual(fi.lineno(), 21)
|
||||||
|
self.assertEqual(fi.filelineno(), 6)
|
||||||
|
self.failIf(fi.isfirstline())
|
||||||
|
self.failIf(fi.isstdin())
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print '%s. Nextfile (bs=%s)' % (start+2, bs)
|
||||||
|
fi.nextfile()
|
||||||
|
self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
|
||||||
|
self.assertEqual(fi.lineno(), 22)
|
||||||
|
fi.close()
|
||||||
|
|
||||||
def writeFiles():
|
if verbose:
|
||||||
global t1, t2, t3, t4
|
print '%s. Stdin (bs=%s)' % (start+3, bs)
|
||||||
t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)])
|
fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
|
||||||
t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)])
|
savestdin = sys.stdin
|
||||||
t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)])
|
try:
|
||||||
t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)])
|
sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
|
||||||
|
lines = list(fi)
|
||||||
|
self.assertEqual(len(lines), 33)
|
||||||
|
self.assertEqual(lines[32], 'Line 2 of stdin\n')
|
||||||
|
self.assertEqual(fi.filename(), '<stdin>')
|
||||||
|
fi.nextfile()
|
||||||
|
finally:
|
||||||
|
sys.stdin = savestdin
|
||||||
|
|
||||||
# First, run the tests with default and teeny buffer size.
|
if verbose:
|
||||||
for round, bs in (0, 0), (1, 30):
|
print '%s. Boundary conditions (bs=%s)' % (start+4, bs)
|
||||||
try:
|
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
|
||||||
writeFiles()
|
self.assertEqual(fi.lineno(), 0)
|
||||||
runTests(t1, t2, t3, t4, bs, round)
|
self.assertEqual(fi.filename(), None)
|
||||||
finally:
|
fi.nextfile()
|
||||||
remove_tempfiles(t1, t2, t3, t4)
|
self.assertEqual(fi.lineno(), 0)
|
||||||
|
self.assertEqual(fi.filename(), None)
|
||||||
|
|
||||||
# Next, check for proper behavior with 0-byte files.
|
if verbose:
|
||||||
if verbose:
|
print '%s. Inplace (bs=%s)' % (start+5, bs)
|
||||||
print "13. 0-byte files"
|
savestdout = sys.stdout
|
||||||
try:
|
try:
|
||||||
t1 = writeTmp(1, [""])
|
fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
|
||||||
t2 = writeTmp(2, [""])
|
for line in fi:
|
||||||
t3 = writeTmp(3, ["The only line there is.\n"])
|
line = line[:-1].upper()
|
||||||
t4 = writeTmp(4, [""])
|
print line
|
||||||
fi = FileInput(files=(t1, t2, t3, t4))
|
fi.close()
|
||||||
line = fi.readline()
|
finally:
|
||||||
verify(line == 'The only line there is.\n')
|
sys.stdout = savestdout
|
||||||
verify(fi.lineno() == 1)
|
|
||||||
verify(fi.filelineno() == 1)
|
|
||||||
verify(fi.filename() == t3)
|
|
||||||
line = fi.readline()
|
|
||||||
verify(not line)
|
|
||||||
verify(fi.lineno() == 1)
|
|
||||||
verify(fi.filelineno() == 0)
|
|
||||||
verify(fi.filename() == t4)
|
|
||||||
fi.close()
|
|
||||||
finally:
|
|
||||||
remove_tempfiles(t1, t2, t3, t4)
|
|
||||||
|
|
||||||
if verbose:
|
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
|
||||||
print "14. Files that don't end with newline"
|
for line in fi:
|
||||||
try:
|
self.assertEqual(line[-1], '\n')
|
||||||
t1 = writeTmp(1, ["A\nB\nC"])
|
m = pat.match(line[:-1])
|
||||||
t2 = writeTmp(2, ["D\nE\nF"])
|
self.assertNotEqual(m, None)
|
||||||
fi = FileInput(files=(t1, t2))
|
self.assertEqual(int(m.group(1)), fi.filelineno())
|
||||||
lines = list(fi)
|
fi.close()
|
||||||
verify(lines == ["A\n", "B\n", "C", "D\n", "E\n", "F"])
|
|
||||||
verify(fi.filelineno() == 3)
|
class FileInputTests(unittest.TestCase):
|
||||||
verify(fi.lineno() == 6)
|
def test_zero_byte_files(self):
|
||||||
finally:
|
try:
|
||||||
remove_tempfiles(t1, t2)
|
t1 = writeTmp(1, [""])
|
||||||
|
t2 = writeTmp(2, [""])
|
||||||
|
t3 = writeTmp(3, ["The only line there is.\n"])
|
||||||
|
t4 = writeTmp(4, [""])
|
||||||
|
fi = FileInput(files=(t1, t2, t3, t4))
|
||||||
|
|
||||||
|
line = fi.readline()
|
||||||
|
self.assertEqual(line, 'The only line there is.\n')
|
||||||
|
self.assertEqual(fi.lineno(), 1)
|
||||||
|
self.assertEqual(fi.filelineno(), 1)
|
||||||
|
self.assertEqual(fi.filename(), t3)
|
||||||
|
|
||||||
|
line = fi.readline()
|
||||||
|
self.failIf(line)
|
||||||
|
self.assertEqual(fi.lineno(), 1)
|
||||||
|
self.assertEqual(fi.filelineno(), 0)
|
||||||
|
self.assertEqual(fi.filename(), t4)
|
||||||
|
fi.close()
|
||||||
|
finally:
|
||||||
|
remove_tempfiles(t1, t2, t3, t4)
|
||||||
|
|
||||||
if verbose:
|
def test_files_that_dont_end_with_newline(self):
|
||||||
print "15. Unicode filenames"
|
try:
|
||||||
try:
|
t1 = writeTmp(1, ["A\nB\nC"])
|
||||||
t1 = writeTmp(1, ["A\nB"])
|
t2 = writeTmp(2, ["D\nE\nF"])
|
||||||
encoding = sys.getfilesystemencoding()
|
fi = FileInput(files=(t1, t2))
|
||||||
if encoding is None:
|
lines = list(fi)
|
||||||
encoding = 'ascii'
|
self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
|
||||||
fi = FileInput(files=unicode(t1, encoding))
|
self.assertEqual(fi.filelineno(), 3)
|
||||||
lines = list(fi)
|
self.assertEqual(fi.lineno(), 6)
|
||||||
verify(lines == ["A\n", "B"])
|
finally:
|
||||||
finally:
|
remove_tempfiles(t1, t2)
|
||||||
remove_tempfiles(t1)
|
|
||||||
|
|
||||||
if verbose:
|
def test_unicode_filenames(self):
|
||||||
print "16. fileno()"
|
try:
|
||||||
try:
|
t1 = writeTmp(1, ["A\nB"])
|
||||||
t1 = writeTmp(1, ["A\nB"])
|
encoding = sys.getfilesystemencoding()
|
||||||
t2 = writeTmp(2, ["C\nD"])
|
if encoding is None:
|
||||||
fi = FileInput(files=(t1, t2))
|
encoding = 'ascii'
|
||||||
verify(fi.fileno() == -1)
|
fi = FileInput(files=unicode(t1, encoding))
|
||||||
line = fi.next()
|
lines = list(fi)
|
||||||
verify(fi.fileno() != -1)
|
self.assertEqual(lines, ["A\n", "B"])
|
||||||
fi.nextfile()
|
finally:
|
||||||
verify(fi.fileno() == -1)
|
remove_tempfiles(t1)
|
||||||
line = list(fi)
|
|
||||||
verify(fi.fileno() == -1)
|
|
||||||
finally:
|
|
||||||
remove_tempfiles(t1, t2)
|
|
||||||
|
|
||||||
if verbose:
|
def test_fileno(self):
|
||||||
print "17. Specify opening mode"
|
try:
|
||||||
try:
|
t1 = writeTmp(1, ["A\nB"])
|
||||||
# invalid mode, should raise ValueError
|
t2 = writeTmp(2, ["C\nD"])
|
||||||
fi = FileInput(mode="w")
|
fi = FileInput(files=(t1, t2))
|
||||||
raise TestFailed("FileInput should reject invalid mode argument")
|
self.assertEqual(fi.fileno(), -1)
|
||||||
except ValueError:
|
line = fi.next()
|
||||||
pass
|
self.assertNotEqual(fi.fileno(), -1)
|
||||||
try:
|
fi.nextfile()
|
||||||
# try opening in universal newline mode
|
self.assertEqual(fi.fileno(), -1)
|
||||||
t1 = writeTmp(1, ["A\nB\r\nC\rD"], mode="wb")
|
line = list(fi)
|
||||||
fi = FileInput(files=t1, mode="U")
|
self.assertEqual(fi.fileno(), -1)
|
||||||
lines = list(fi)
|
finally:
|
||||||
verify(lines == ["A\n", "B\n", "C\n", "D"])
|
remove_tempfiles(t1, t2)
|
||||||
finally:
|
|
||||||
remove_tempfiles(t1)
|
|
||||||
|
|
||||||
if verbose:
|
def test_opening_mode(self):
|
||||||
print "18. Test file opening hook"
|
try:
|
||||||
try:
|
# invalid mode, should raise ValueError
|
||||||
# cannot use openhook and inplace mode
|
fi = FileInput(mode="w")
|
||||||
fi = FileInput(inplace=1, openhook=lambda f,m: None)
|
self.fail("FileInput should reject invalid mode argument")
|
||||||
raise TestFailed("FileInput should raise if both inplace "
|
except ValueError:
|
||||||
"and openhook arguments are given")
|
pass
|
||||||
except ValueError:
|
try:
|
||||||
pass
|
# try opening in universal newline mode
|
||||||
try:
|
t1 = writeTmp(1, ["A\nB\r\nC\rD"], mode="wb")
|
||||||
fi = FileInput(openhook=1)
|
fi = FileInput(files=t1, mode="U")
|
||||||
raise TestFailed("FileInput should check openhook for being callable")
|
lines = list(fi)
|
||||||
except ValueError:
|
self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
|
||||||
pass
|
finally:
|
||||||
try:
|
remove_tempfiles(t1)
|
||||||
t1 = writeTmp(1, ["A\nB"], mode="wb")
|
|
||||||
fi = FileInput(files=t1, openhook=hook_encoded("rot13"))
|
def test_file_opening_hook(self):
|
||||||
lines = list(fi)
|
try:
|
||||||
verify(lines == ["N\n", "O"])
|
# cannot use openhook and inplace mode
|
||||||
finally:
|
fi = FileInput(inplace=1, openhook=lambda f,m: None)
|
||||||
remove_tempfiles(t1)
|
self.fail("FileInput should raise if both inplace "
|
||||||
|
"and openhook arguments are given")
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
fi = FileInput(openhook=1)
|
||||||
|
self.fail("FileInput should check openhook for being callable")
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
t1 = writeTmp(1, ["A\nB"], mode="wb")
|
||||||
|
fi = FileInput(files=t1, openhook=hook_encoded("rot13"))
|
||||||
|
lines = list(fi)
|
||||||
|
self.assertEqual(lines, ["N\n", "O"])
|
||||||
|
finally:
|
||||||
|
remove_tempfiles(t1)
|
||||||
|
|
||||||
|
def test_main():
|
||||||
|
run_unittest(BufferSizesTests, FileInputTests)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_main()
|
||||||
|
|
Loading…
Reference in New Issue