2 Tests for fileinput module.
7 from test
.test_support
import verbose
, TESTFN
, run_unittest
8 from test
.test_support
import unlink
as safe_unlink
10 from StringIO
import StringIO
11 from fileinput
import FileInput
, hook_encoded
13 # The fileinput module has 2 interfaces: the FileInput class which does
14 # all the work, and a few functions (input, etc.) that use a global _state
15 # variable. We only test the FileInput class, since the other functions
16 # only provide a thin facade over FileInput.
18 # Write lines (a list of lines) to temp file number i, and return the
20 def writeTmp(i
, lines
, mode
='w'): # opening in text mode is the default
21 name
= TESTFN
+ str(i
)
27 def remove_tempfiles(*names
):
31 class BufferSizesTests(unittest
.TestCase
):
32 def test_buffer_sizes(self
):
33 # First, run the tests with default and teeny buffer size.
34 for round, bs
in (0, 0), (1, 30):
36 t1
= writeTmp(1, ["Line %s of file 1\n" % (i
+1) for i
in range(15)])
37 t2
= writeTmp(2, ["Line %s of file 2\n" % (i
+1) for i
in range(10)])
38 t3
= writeTmp(3, ["Line %s of file 3\n" % (i
+1) for i
in range(5)])
39 t4
= writeTmp(4, ["Line %s of file 4\n" % (i
+1) for i
in range(1)])
40 self
.buffer_size_test(t1
, t2
, t3
, t4
, bs
, round)
42 remove_tempfiles(t1
, t2
, t3
, t4
)
44 def buffer_size_test(self
, t1
, t2
, t3
, t4
, bs
=0, round=0):
45 pat
= re
.compile(r
'LINE (\d+) OF FILE (\d+)')
49 print '%s. Simple iteration (bs=%s)' % (start
+0, bs
)
50 fi
= FileInput(files
=(t1
, t2
, t3
, t4
), bufsize
=bs
)
53 self
.assertEqual(len(lines
), 31)
54 self
.assertEqual(lines
[4], 'Line 5 of file 1\n')
55 self
.assertEqual(lines
[30], 'Line 1 of file 4\n')
56 self
.assertEqual(fi
.lineno(), 31)
57 self
.assertEqual(fi
.filename(), t4
)
60 print '%s. Status variables (bs=%s)' % (start
+1, bs
)
61 fi
= FileInput(files
=(t1
, t2
, t3
, t4
), bufsize
=bs
)
63 while s
and s
!= 'Line 6 of file 2\n':
65 self
.assertEqual(fi
.filename(), t2
)
66 self
.assertEqual(fi
.lineno(), 21)
67 self
.assertEqual(fi
.filelineno(), 6)
68 self
.assertFalse(fi
.isfirstline())
69 self
.assertFalse(fi
.isstdin())
72 print '%s. Nextfile (bs=%s)' % (start
+2, bs
)
74 self
.assertEqual(fi
.readline(), 'Line 1 of file 3\n')
75 self
.assertEqual(fi
.lineno(), 22)
79 print '%s. Stdin (bs=%s)' % (start
+3, bs
)
80 fi
= FileInput(files
=(t1
, t2
, t3
, t4
, '-'), bufsize
=bs
)
83 sys
.stdin
= StringIO("Line 1 of stdin\nLine 2 of stdin\n")
85 self
.assertEqual(len(lines
), 33)
86 self
.assertEqual(lines
[32], 'Line 2 of stdin\n')
87 self
.assertEqual(fi
.filename(), '<stdin>')
93 print '%s. Boundary conditions (bs=%s)' % (start
+4, bs
)
94 fi
= FileInput(files
=(t1
, t2
, t3
, t4
), bufsize
=bs
)
95 self
.assertEqual(fi
.lineno(), 0)
96 self
.assertEqual(fi
.filename(), None)
98 self
.assertEqual(fi
.lineno(), 0)
99 self
.assertEqual(fi
.filename(), None)
102 print '%s. Inplace (bs=%s)' % (start
+5, bs
)
103 savestdout
= sys
.stdout
105 fi
= FileInput(files
=(t1
, t2
, t3
, t4
), inplace
=1, bufsize
=bs
)
107 line
= line
[:-1].upper()
111 sys
.stdout
= savestdout
113 fi
= FileInput(files
=(t1
, t2
, t3
, t4
), bufsize
=bs
)
115 self
.assertEqual(line
[-1], '\n')
116 m
= pat
.match(line
[:-1])
117 self
.assertNotEqual(m
, None)
118 self
.assertEqual(int(m
.group(1)), fi
.filelineno())
121 class FileInputTests(unittest
.TestCase
):
122 def test_zero_byte_files(self
):
124 t1
= writeTmp(1, [""])
125 t2
= writeTmp(2, [""])
126 t3
= writeTmp(3, ["The only line there is.\n"])
127 t4
= writeTmp(4, [""])
128 fi
= FileInput(files
=(t1
, t2
, t3
, t4
))
131 self
.assertEqual(line
, 'The only line there is.\n')
132 self
.assertEqual(fi
.lineno(), 1)
133 self
.assertEqual(fi
.filelineno(), 1)
134 self
.assertEqual(fi
.filename(), t3
)
137 self
.assertFalse(line
)
138 self
.assertEqual(fi
.lineno(), 1)
139 self
.assertEqual(fi
.filelineno(), 0)
140 self
.assertEqual(fi
.filename(), t4
)
143 remove_tempfiles(t1
, t2
, t3
, t4
)
145 def test_files_that_dont_end_with_newline(self
):
147 t1
= writeTmp(1, ["A\nB\nC"])
148 t2
= writeTmp(2, ["D\nE\nF"])
149 fi
= FileInput(files
=(t1
, t2
))
151 self
.assertEqual(lines
, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
152 self
.assertEqual(fi
.filelineno(), 3)
153 self
.assertEqual(fi
.lineno(), 6)
155 remove_tempfiles(t1
, t2
)
157 def test_unicode_filenames(self
):
159 t1
= writeTmp(1, ["A\nB"])
160 encoding
= sys
.getfilesystemencoding()
163 fi
= FileInput(files
=unicode(t1
, encoding
))
165 self
.assertEqual(lines
, ["A\n", "B"])
169 def test_fileno(self
):
171 t1
= writeTmp(1, ["A\nB"])
172 t2
= writeTmp(2, ["C\nD"])
173 fi
= FileInput(files
=(t1
, t2
))
174 self
.assertEqual(fi
.fileno(), -1)
176 self
.assertNotEqual(fi
.fileno(), -1)
178 self
.assertEqual(fi
.fileno(), -1)
180 self
.assertEqual(fi
.fileno(), -1)
182 remove_tempfiles(t1
, t2
)
184 def test_opening_mode(self
):
186 # invalid mode, should raise ValueError
187 fi
= FileInput(mode
="w")
188 self
.fail("FileInput should reject invalid mode argument")
192 # try opening in universal newline mode
193 t1
= writeTmp(1, ["A\nB\r\nC\rD"], mode
="wb")
194 fi
= FileInput(files
=t1
, mode
="U")
196 self
.assertEqual(lines
, ["A\n", "B\n", "C\n", "D"])
200 def test_file_opening_hook(self
):
202 # cannot use openhook and inplace mode
203 fi
= FileInput(inplace
=1, openhook
=lambda f
,m
: None)
204 self
.fail("FileInput should raise if both inplace "
205 "and openhook arguments are given")
209 fi
= FileInput(openhook
=1)
210 self
.fail("FileInput should check openhook for being callable")
214 t1
= writeTmp(1, ["A\nB"], mode
="wb")
215 fi
= FileInput(files
=t1
, openhook
=hook_encoded("rot13"))
217 self
.assertEqual(lines
, ["N\n", "O"])
222 run_unittest(BufferSizesTests
, FileInputTests
)
224 if __name__
== "__main__":