blob: a96d48a5619b0163bc74149b0efd15b674e20a54 [file] [log] [blame]
Tim Peters3230d5c2001-07-11 22:21:17 +00001'''
2Tests for fileinput module.
3Nick Mathewson
4'''
Benjamin Petersoneb462882011-03-15 09:50:18 -05005import os
6import sys
7import re
briancurtin906f0c42011-03-15 10:29:41 -04008import fileinput
9import collections
briancurtin906f0c42011-03-15 10:29:41 -040010import types
11import codecs
Benjamin Petersoneb462882011-03-15 09:50:18 -050012import unittest
13
briancurtinf84f3c32011-03-18 13:03:17 -050014try:
15 import bz2
16except ImportError:
17 bz2 = None
Ezio Melottic3afbb92011-05-14 10:10:53 +030018try:
19 import gzip
20except ImportError:
21 gzip = None
briancurtinf84f3c32011-03-18 13:03:17 -050022
Benjamin Petersoneb462882011-03-15 09:50:18 -050023from io import StringIO
24from fileinput import FileInput, hook_encoded
25
26from test.support import verbose, TESTFN, run_unittest
27from test.support import unlink as safe_unlink
28
Tim Peters3230d5c2001-07-11 22:21:17 +000029
30# The fileinput module has 2 interfaces: the FileInput class which does
31# all the work, and a few functions (input, etc.) that use a global _state
briancurtin906f0c42011-03-15 10:29:41 -040032# variable.
Tim Peters3230d5c2001-07-11 22:21:17 +000033
34# Write lines (a list of lines) to temp file number i, and return the
35# temp file's name.
Tim Peters4d7cad12006-02-19 21:22:10 +000036def writeTmp(i, lines, mode='w'): # opening in text mode is the default
Tim Peters3230d5c2001-07-11 22:21:17 +000037 name = TESTFN + str(i)
Tim Peters4d7cad12006-02-19 21:22:10 +000038 f = open(name, mode)
Guido van Rossumc43e79f2007-06-18 18:26:36 +000039 for line in lines:
40 f.write(line)
Tim Peters3230d5c2001-07-11 22:21:17 +000041 f.close()
42 return name
43
Tim Peters3230d5c2001-07-11 22:21:17 +000044def remove_tempfiles(*names):
45 for name in names:
Guido van Rossume22905a2007-08-27 23:09:25 +000046 if name:
47 safe_unlink(name)
Tim Peters3230d5c2001-07-11 22:21:17 +000048
Guido van Rossumd8faa362007-04-27 19:54:29 +000049class BufferSizesTests(unittest.TestCase):
50 def test_buffer_sizes(self):
51 # First, run the tests with default and teeny buffer size.
52 for round, bs in (0, 0), (1, 30):
Neal Norwitz2595e762008-03-24 06:10:13 +000053 t1 = t2 = t3 = t4 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +000054 try:
55 t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)])
56 t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)])
57 t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)])
58 t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)])
59 self.buffer_size_test(t1, t2, t3, t4, bs, round)
60 finally:
61 remove_tempfiles(t1, t2, t3, t4)
Tim Peters3230d5c2001-07-11 22:21:17 +000062
Guido van Rossumd8faa362007-04-27 19:54:29 +000063 def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
64 pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
Tim Peters3230d5c2001-07-11 22:21:17 +000065
Guido van Rossumd8faa362007-04-27 19:54:29 +000066 start = 1 + round*6
67 if verbose:
68 print('%s. Simple iteration (bs=%s)' % (start+0, bs))
69 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
Tim Peters3230d5c2001-07-11 22:21:17 +000070 lines = list(fi)
Tim Peters3230d5c2001-07-11 22:21:17 +000071 fi.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +000072 self.assertEqual(len(lines), 31)
73 self.assertEqual(lines[4], 'Line 5 of file 1\n')
74 self.assertEqual(lines[30], 'Line 1 of file 4\n')
75 self.assertEqual(fi.lineno(), 31)
76 self.assertEqual(fi.filename(), t4)
Tim Peters3230d5c2001-07-11 22:21:17 +000077
Guido van Rossumd8faa362007-04-27 19:54:29 +000078 if verbose:
79 print('%s. Status variables (bs=%s)' % (start+1, bs))
80 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
81 s = "x"
82 while s and s != 'Line 6 of file 2\n':
83 s = fi.readline()
84 self.assertEqual(fi.filename(), t2)
85 self.assertEqual(fi.lineno(), 21)
86 self.assertEqual(fi.filelineno(), 6)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000087 self.assertFalse(fi.isfirstline())
88 self.assertFalse(fi.isstdin())
Tim Peters3230d5c2001-07-11 22:21:17 +000089
Guido van Rossumd8faa362007-04-27 19:54:29 +000090 if verbose:
91 print('%s. Nextfile (bs=%s)' % (start+2, bs))
92 fi.nextfile()
93 self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
94 self.assertEqual(fi.lineno(), 22)
95 fi.close()
Tim Peters3230d5c2001-07-11 22:21:17 +000096
Guido van Rossumd8faa362007-04-27 19:54:29 +000097 if verbose:
98 print('%s. Stdin (bs=%s)' % (start+3, bs))
99 fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
100 savestdin = sys.stdin
101 try:
102 sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
103 lines = list(fi)
104 self.assertEqual(len(lines), 33)
105 self.assertEqual(lines[32], 'Line 2 of stdin\n')
106 self.assertEqual(fi.filename(), '<stdin>')
107 fi.nextfile()
108 finally:
109 sys.stdin = savestdin
Tim Peters3230d5c2001-07-11 22:21:17 +0000110
Guido van Rossumd8faa362007-04-27 19:54:29 +0000111 if verbose:
112 print('%s. Boundary conditions (bs=%s)' % (start+4, bs))
113 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
114 self.assertEqual(fi.lineno(), 0)
115 self.assertEqual(fi.filename(), None)
116 fi.nextfile()
117 self.assertEqual(fi.lineno(), 0)
118 self.assertEqual(fi.filename(), None)
Tim Peters3230d5c2001-07-11 22:21:17 +0000119
Guido van Rossumd8faa362007-04-27 19:54:29 +0000120 if verbose:
121 print('%s. Inplace (bs=%s)' % (start+5, bs))
122 savestdout = sys.stdout
123 try:
124 fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
125 for line in fi:
126 line = line[:-1].upper()
127 print(line)
128 fi.close()
129 finally:
130 sys.stdout = savestdout
Tim Peters3230d5c2001-07-11 22:21:17 +0000131
Guido van Rossumd8faa362007-04-27 19:54:29 +0000132 fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
133 for line in fi:
134 self.assertEqual(line[-1], '\n')
135 m = pat.match(line[:-1])
136 self.assertNotEqual(m, None)
137 self.assertEqual(int(m.group(1)), fi.filelineno())
138 fi.close()
Georg Brandle4662172006-02-19 09:51:27 +0000139
briancurtin906f0c42011-03-15 10:29:41 -0400140class UnconditionallyRaise:
141 def __init__(self, exception_type):
142 self.exception_type = exception_type
143 self.invoked = False
144 def __call__(self, *args, **kwargs):
145 self.invoked = True
146 raise self.exception_type()
147
Guido van Rossumd8faa362007-04-27 19:54:29 +0000148class FileInputTests(unittest.TestCase):
briancurtin906f0c42011-03-15 10:29:41 -0400149
Guido van Rossumd8faa362007-04-27 19:54:29 +0000150 def test_zero_byte_files(self):
Neal Norwitz2595e762008-03-24 06:10:13 +0000151 t1 = t2 = t3 = t4 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000152 try:
153 t1 = writeTmp(1, [""])
154 t2 = writeTmp(2, [""])
155 t3 = writeTmp(3, ["The only line there is.\n"])
156 t4 = writeTmp(4, [""])
157 fi = FileInput(files=(t1, t2, t3, t4))
Georg Brandl67e9fb92006-02-19 13:56:17 +0000158
Guido van Rossumd8faa362007-04-27 19:54:29 +0000159 line = fi.readline()
160 self.assertEqual(line, 'The only line there is.\n')
161 self.assertEqual(fi.lineno(), 1)
162 self.assertEqual(fi.filelineno(), 1)
163 self.assertEqual(fi.filename(), t3)
Georg Brandlc029f872006-02-19 14:12:34 +0000164
Guido van Rossumd8faa362007-04-27 19:54:29 +0000165 line = fi.readline()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000166 self.assertFalse(line)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000167 self.assertEqual(fi.lineno(), 1)
168 self.assertEqual(fi.filelineno(), 0)
169 self.assertEqual(fi.filename(), t4)
170 fi.close()
171 finally:
172 remove_tempfiles(t1, t2, t3, t4)
Georg Brandlc98eeed2006-02-19 14:57:47 +0000173
Guido van Rossumd8faa362007-04-27 19:54:29 +0000174 def test_files_that_dont_end_with_newline(self):
Neal Norwitz2595e762008-03-24 06:10:13 +0000175 t1 = t2 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000176 try:
177 t1 = writeTmp(1, ["A\nB\nC"])
178 t2 = writeTmp(2, ["D\nE\nF"])
179 fi = FileInput(files=(t1, t2))
180 lines = list(fi)
181 self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
182 self.assertEqual(fi.filelineno(), 3)
183 self.assertEqual(fi.lineno(), 6)
184 finally:
185 remove_tempfiles(t1, t2)
186
Guido van Rossumc43e79f2007-06-18 18:26:36 +0000187## def test_unicode_filenames(self):
188## # XXX A unicode string is always returned by writeTmp.
189## # So is this needed?
190## try:
191## t1 = writeTmp(1, ["A\nB"])
192## encoding = sys.getfilesystemencoding()
193## if encoding is None:
194## encoding = 'ascii'
195## fi = FileInput(files=str(t1, encoding))
196## lines = list(fi)
197## self.assertEqual(lines, ["A\n", "B"])
198## finally:
199## remove_tempfiles(t1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000200
201 def test_fileno(self):
Neal Norwitz2595e762008-03-24 06:10:13 +0000202 t1 = t2 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000203 try:
204 t1 = writeTmp(1, ["A\nB"])
205 t2 = writeTmp(2, ["C\nD"])
206 fi = FileInput(files=(t1, t2))
207 self.assertEqual(fi.fileno(), -1)
208 line =next( fi)
209 self.assertNotEqual(fi.fileno(), -1)
210 fi.nextfile()
211 self.assertEqual(fi.fileno(), -1)
212 line = list(fi)
213 self.assertEqual(fi.fileno(), -1)
214 finally:
215 remove_tempfiles(t1, t2)
216
217 def test_opening_mode(self):
218 try:
219 # invalid mode, should raise ValueError
220 fi = FileInput(mode="w")
221 self.fail("FileInput should reject invalid mode argument")
222 except ValueError:
223 pass
Guido van Rossume22905a2007-08-27 23:09:25 +0000224 t1 = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000225 try:
226 # try opening in universal newline mode
Guido van Rossume22905a2007-08-27 23:09:25 +0000227 t1 = writeTmp(1, [b"A\nB\r\nC\rD"], mode="wb")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000228 fi = FileInput(files=t1, mode="U")
229 lines = list(fi)
230 self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
231 finally:
232 remove_tempfiles(t1)
233
Guido van Rossume22905a2007-08-27 23:09:25 +0000234 def test_file_opening_hook(self):
235 try:
236 # cannot use openhook and inplace mode
237 fi = FileInput(inplace=1, openhook=lambda f, m: None)
238 self.fail("FileInput should raise if both inplace "
239 "and openhook arguments are given")
240 except ValueError:
241 pass
242 try:
243 fi = FileInput(openhook=1)
244 self.fail("FileInput should check openhook for being callable")
245 except ValueError:
246 pass
briancurtin906f0c42011-03-15 10:29:41 -0400247
248 class CustomOpenHook:
249 def __init__(self):
250 self.invoked = False
251 def __call__(self, *args):
252 self.invoked = True
253 return open(*args)
254
255 t = writeTmp(1, ["\n"])
256 self.addCleanup(remove_tempfiles, t)
257 custom_open_hook = CustomOpenHook()
258 with FileInput([t], openhook=custom_open_hook) as fi:
259 fi.readline()
260 self.assertTrue(custom_open_hook.invoked, "openhook not invoked")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000261
Georg Brandl6cb7b652010-07-31 20:08:15 +0000262 def test_context_manager(self):
263 try:
264 t1 = writeTmp(1, ["A\nB\nC"])
265 t2 = writeTmp(2, ["D\nE\nF"])
266 with FileInput(files=(t1, t2)) as fi:
267 lines = list(fi)
268 self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
269 self.assertEqual(fi.filelineno(), 3)
270 self.assertEqual(fi.lineno(), 6)
271 self.assertEqual(fi._files, ())
272 finally:
273 remove_tempfiles(t1, t2)
274
275 def test_close_on_exception(self):
276 try:
277 t1 = writeTmp(1, [""])
278 with FileInput(files=t1) as fi:
279 raise IOError
280 except IOError:
281 self.assertEqual(fi._files, ())
282 finally:
283 remove_tempfiles(t1)
284
briancurtin906f0c42011-03-15 10:29:41 -0400285 def test_empty_files_list_specified_to_constructor(self):
286 with FileInput(files=[]) as fi:
Brett Cannond47af532011-03-15 15:55:12 -0400287 self.assertEqual(fi._files, ('-',))
briancurtin906f0c42011-03-15 10:29:41 -0400288
289 def test__getitem__(self):
290 """Tests invoking FileInput.__getitem__() with the current
291 line number"""
292 t = writeTmp(1, ["line1\n", "line2\n"])
293 self.addCleanup(remove_tempfiles, t)
294 with FileInput(files=[t]) as fi:
295 retval1 = fi[0]
296 self.assertEqual(retval1, "line1\n")
297 retval2 = fi[1]
298 self.assertEqual(retval2, "line2\n")
299
300 def test__getitem__invalid_key(self):
301 """Tests invoking FileInput.__getitem__() with an index unequal to
302 the line number"""
303 t = writeTmp(1, ["line1\n", "line2\n"])
304 self.addCleanup(remove_tempfiles, t)
305 with FileInput(files=[t]) as fi:
306 with self.assertRaises(RuntimeError) as cm:
307 fi[1]
Brett Cannond47af532011-03-15 15:55:12 -0400308 self.assertEqual(cm.exception.args, ("accessing lines out of order",))
briancurtin906f0c42011-03-15 10:29:41 -0400309
310 def test__getitem__eof(self):
311 """Tests invoking FileInput.__getitem__() with the line number but at
312 end-of-input"""
313 t = writeTmp(1, [])
314 self.addCleanup(remove_tempfiles, t)
315 with FileInput(files=[t]) as fi:
316 with self.assertRaises(IndexError) as cm:
317 fi[0]
Brett Cannond47af532011-03-15 15:55:12 -0400318 self.assertEqual(cm.exception.args, ("end of input reached",))
briancurtin906f0c42011-03-15 10:29:41 -0400319
320 def test_nextfile_oserror_deleting_backup(self):
321 """Tests invoking FileInput.nextfile() when the attempt to delete
322 the backup file would raise OSError. This error is expected to be
323 silently ignored"""
324
325 os_unlink_orig = os.unlink
326 os_unlink_replacement = UnconditionallyRaise(OSError)
327 try:
328 t = writeTmp(1, ["\n"])
329 self.addCleanup(remove_tempfiles, t)
330 with FileInput(files=[t], inplace=True) as fi:
331 next(fi) # make sure the file is opened
332 os.unlink = os_unlink_replacement
333 fi.nextfile()
334 finally:
335 os.unlink = os_unlink_orig
336
337 # sanity check to make sure that our test scenario was actually hit
338 self.assertTrue(os_unlink_replacement.invoked,
339 "os.unlink() was not invoked")
340
341 def test_readline_os_fstat_raises_OSError(self):
342 """Tests invoking FileInput.readline() when os.fstat() raises OSError.
343 This exception should be silently discarded."""
344
345 os_fstat_orig = os.fstat
346 os_fstat_replacement = UnconditionallyRaise(OSError)
347 try:
348 t = writeTmp(1, ["\n"])
349 self.addCleanup(remove_tempfiles, t)
350 with FileInput(files=[t], inplace=True) as fi:
351 os.fstat = os_fstat_replacement
352 fi.readline()
353 finally:
354 os.fstat = os_fstat_orig
355
356 # sanity check to make sure that our test scenario was actually hit
357 self.assertTrue(os_fstat_replacement.invoked,
358 "os.fstat() was not invoked")
359
360 @unittest.skipIf(not hasattr(os, "chmod"), "os.chmod does not exist")
361 def test_readline_os_chmod_raises_OSError(self):
362 """Tests invoking FileInput.readline() when os.chmod() raises OSError.
363 This exception should be silently discarded."""
364
365 os_chmod_orig = os.chmod
366 os_chmod_replacement = UnconditionallyRaise(OSError)
367 try:
368 t = writeTmp(1, ["\n"])
369 self.addCleanup(remove_tempfiles, t)
370 with FileInput(files=[t], inplace=True) as fi:
371 os.chmod = os_chmod_replacement
372 fi.readline()
373 finally:
374 os.chmod = os_chmod_orig
375
376 # sanity check to make sure that our test scenario was actually hit
377 self.assertTrue(os_chmod_replacement.invoked,
378 "os.fstat() was not invoked")
379
380 def test_fileno_when_ValueError_raised(self):
381 class FilenoRaisesValueError(UnconditionallyRaise):
382 def __init__(self):
383 UnconditionallyRaise.__init__(self, ValueError)
384 def fileno(self):
385 self.__call__()
386
387 unconditionally_raise_ValueError = FilenoRaisesValueError()
388 t = writeTmp(1, ["\n"])
389 self.addCleanup(remove_tempfiles, t)
390 with FileInput(files=[t]) as fi:
391 file_backup = fi._file
392 try:
393 fi._file = unconditionally_raise_ValueError
394 result = fi.fileno()
395 finally:
396 fi._file = file_backup # make sure the file gets cleaned up
397
398 # sanity check to make sure that our test scenario was actually hit
399 self.assertTrue(unconditionally_raise_ValueError.invoked,
400 "_file.fileno() was not invoked")
401
402 self.assertEqual(result, -1, "fileno() should return -1")
403
404class MockFileInput:
405 """A class that mocks out fileinput.FileInput for use during unit tests"""
406
407 def __init__(self, files=None, inplace=False, backup="", bufsize=0,
408 mode="r", openhook=None):
409 self.files = files
410 self.inplace = inplace
411 self.backup = backup
412 self.bufsize = bufsize
413 self.mode = mode
414 self.openhook = openhook
415 self._file = None
416 self.invocation_counts = collections.defaultdict(lambda: 0)
417 self.return_values = {}
418
419 def close(self):
420 self.invocation_counts["close"] += 1
421
422 def nextfile(self):
423 self.invocation_counts["nextfile"] += 1
424 return self.return_values["nextfile"]
425
426 def filename(self):
427 self.invocation_counts["filename"] += 1
428 return self.return_values["filename"]
429
430 def lineno(self):
431 self.invocation_counts["lineno"] += 1
432 return self.return_values["lineno"]
433
434 def filelineno(self):
435 self.invocation_counts["filelineno"] += 1
436 return self.return_values["filelineno"]
437
438 def fileno(self):
439 self.invocation_counts["fileno"] += 1
440 return self.return_values["fileno"]
441
442 def isfirstline(self):
443 self.invocation_counts["isfirstline"] += 1
444 return self.return_values["isfirstline"]
445
446 def isstdin(self):
447 self.invocation_counts["isstdin"] += 1
448 return self.return_values["isstdin"]
449
450class BaseFileInputGlobalMethodsTest(unittest.TestCase):
451 """Base class for unit tests for the global function of
452 the fileinput module."""
453
454 def setUp(self):
455 self._orig_state = fileinput._state
456 self._orig_FileInput = fileinput.FileInput
457 fileinput.FileInput = MockFileInput
458
459 def tearDown(self):
460 fileinput.FileInput = self._orig_FileInput
461 fileinput._state = self._orig_state
462
463 def assertExactlyOneInvocation(self, mock_file_input, method_name):
464 # assert that the method with the given name was invoked once
465 actual_count = mock_file_input.invocation_counts[method_name]
466 self.assertEqual(actual_count, 1, method_name)
467 # assert that no other unexpected methods were invoked
468 actual_total_count = len(mock_file_input.invocation_counts)
469 self.assertEqual(actual_total_count, 1)
470
471class Test_fileinput_input(BaseFileInputGlobalMethodsTest):
472 """Unit tests for fileinput.input()"""
473
474 def test_state_is_not_None_and_state_file_is_not_None(self):
475 """Tests invoking fileinput.input() when fileinput._state is not None
476 and its _file attribute is also not None. Expect RuntimeError to
477 be raised with a meaningful error message and for fileinput._state
478 to *not* be modified."""
479 instance = MockFileInput()
480 instance._file = object()
481 fileinput._state = instance
482 with self.assertRaises(RuntimeError) as cm:
483 fileinput.input()
484 self.assertEqual(("input() already active",), cm.exception.args)
485 self.assertIs(instance, fileinput._state, "fileinput._state")
486
487 def test_state_is_not_None_and_state_file_is_None(self):
488 """Tests invoking fileinput.input() when fileinput._state is not None
489 but its _file attribute *is* None. Expect it to create and return
490 a new fileinput.FileInput object with all method parameters passed
491 explicitly to the __init__() method; also ensure that
492 fileinput._state is set to the returned instance."""
493 instance = MockFileInput()
494 instance._file = None
495 fileinput._state = instance
496 self.do_test_call_input()
497
498 def test_state_is_None(self):
499 """Tests invoking fileinput.input() when fileinput._state is None
500 Expect it to create and return a new fileinput.FileInput object
501 with all method parameters passed explicitly to the __init__()
502 method; also ensure that fileinput._state is set to the returned
503 instance."""
504 fileinput._state = None
505 self.do_test_call_input()
506
507 def do_test_call_input(self):
508 """Tests that fileinput.input() creates a new fileinput.FileInput
509 object, passing the given parameters unmodified to
510 fileinput.FileInput.__init__(). Note that this test depends on the
511 monkey patching of fileinput.FileInput done by setUp()."""
512 files = object()
513 inplace = object()
514 backup = object()
515 bufsize = object()
516 mode = object()
517 openhook = object()
518
519 # call fileinput.input() with different values for each argument
520 result = fileinput.input(files=files, inplace=inplace, backup=backup,
521 bufsize=bufsize,
522 mode=mode, openhook=openhook)
523
524 # ensure fileinput._state was set to the returned object
525 self.assertIs(result, fileinput._state, "fileinput._state")
526
527 # ensure the parameters to fileinput.input() were passed directly
528 # to FileInput.__init__()
529 self.assertIs(files, result.files, "files")
530 self.assertIs(inplace, result.inplace, "inplace")
531 self.assertIs(backup, result.backup, "backup")
532 self.assertIs(bufsize, result.bufsize, "bufsize")
533 self.assertIs(mode, result.mode, "mode")
534 self.assertIs(openhook, result.openhook, "openhook")
535
536class Test_fileinput_close(BaseFileInputGlobalMethodsTest):
537 """Unit tests for fileinput.close()"""
538
539 def test_state_is_None(self):
540 """Tests that fileinput.close() does nothing if fileinput._state
541 is None"""
542 fileinput._state = None
543 fileinput.close()
544 self.assertIsNone(fileinput._state)
545
546 def test_state_is_not_None(self):
547 """Tests that fileinput.close() invokes close() on fileinput._state
548 and sets _state=None"""
549 instance = MockFileInput()
550 fileinput._state = instance
551 fileinput.close()
552 self.assertExactlyOneInvocation(instance, "close")
553 self.assertIsNone(fileinput._state)
554
555class Test_fileinput_nextfile(BaseFileInputGlobalMethodsTest):
556 """Unit tests for fileinput.nextfile()"""
557
558 def test_state_is_None(self):
559 """Tests fileinput.nextfile() when fileinput._state is None.
560 Ensure that it raises RuntimeError with a meaningful error message
561 and does not modify fileinput._state"""
562 fileinput._state = None
563 with self.assertRaises(RuntimeError) as cm:
564 fileinput.nextfile()
565 self.assertEqual(("no active input()",), cm.exception.args)
566 self.assertIsNone(fileinput._state)
567
568 def test_state_is_not_None(self):
569 """Tests fileinput.nextfile() when fileinput._state is not None.
570 Ensure that it invokes fileinput._state.nextfile() exactly once,
571 returns whatever it returns, and does not modify fileinput._state
572 to point to a different object."""
573 nextfile_retval = object()
574 instance = MockFileInput()
575 instance.return_values["nextfile"] = nextfile_retval
576 fileinput._state = instance
577 retval = fileinput.nextfile()
578 self.assertExactlyOneInvocation(instance, "nextfile")
579 self.assertIs(retval, nextfile_retval)
580 self.assertIs(fileinput._state, instance)
581
582class Test_fileinput_filename(BaseFileInputGlobalMethodsTest):
583 """Unit tests for fileinput.filename()"""
584
585 def test_state_is_None(self):
586 """Tests fileinput.filename() when fileinput._state is None.
587 Ensure that it raises RuntimeError with a meaningful error message
588 and does not modify fileinput._state"""
589 fileinput._state = None
590 with self.assertRaises(RuntimeError) as cm:
591 fileinput.filename()
592 self.assertEqual(("no active input()",), cm.exception.args)
593 self.assertIsNone(fileinput._state)
594
595 def test_state_is_not_None(self):
596 """Tests fileinput.filename() when fileinput._state is not None.
597 Ensure that it invokes fileinput._state.filename() exactly once,
598 returns whatever it returns, and does not modify fileinput._state
599 to point to a different object."""
600 filename_retval = object()
601 instance = MockFileInput()
602 instance.return_values["filename"] = filename_retval
603 fileinput._state = instance
604 retval = fileinput.filename()
605 self.assertExactlyOneInvocation(instance, "filename")
606 self.assertIs(retval, filename_retval)
607 self.assertIs(fileinput._state, instance)
608
609class Test_fileinput_lineno(BaseFileInputGlobalMethodsTest):
610 """Unit tests for fileinput.lineno()"""
611
612 def test_state_is_None(self):
613 """Tests fileinput.lineno() when fileinput._state is None.
614 Ensure that it raises RuntimeError with a meaningful error message
615 and does not modify fileinput._state"""
616 fileinput._state = None
617 with self.assertRaises(RuntimeError) as cm:
618 fileinput.lineno()
619 self.assertEqual(("no active input()",), cm.exception.args)
620 self.assertIsNone(fileinput._state)
621
622 def test_state_is_not_None(self):
623 """Tests fileinput.lineno() when fileinput._state is not None.
624 Ensure that it invokes fileinput._state.lineno() exactly once,
625 returns whatever it returns, and does not modify fileinput._state
626 to point to a different object."""
627 lineno_retval = object()
628 instance = MockFileInput()
629 instance.return_values["lineno"] = lineno_retval
630 fileinput._state = instance
631 retval = fileinput.lineno()
632 self.assertExactlyOneInvocation(instance, "lineno")
633 self.assertIs(retval, lineno_retval)
634 self.assertIs(fileinput._state, instance)
635
636class Test_fileinput_filelineno(BaseFileInputGlobalMethodsTest):
637 """Unit tests for fileinput.filelineno()"""
638
639 def test_state_is_None(self):
640 """Tests fileinput.filelineno() when fileinput._state is None.
641 Ensure that it raises RuntimeError with a meaningful error message
642 and does not modify fileinput._state"""
643 fileinput._state = None
644 with self.assertRaises(RuntimeError) as cm:
645 fileinput.filelineno()
646 self.assertEqual(("no active input()",), cm.exception.args)
647 self.assertIsNone(fileinput._state)
648
649 def test_state_is_not_None(self):
650 """Tests fileinput.filelineno() when fileinput._state is not None.
651 Ensure that it invokes fileinput._state.filelineno() exactly once,
652 returns whatever it returns, and does not modify fileinput._state
653 to point to a different object."""
654 filelineno_retval = object()
655 instance = MockFileInput()
656 instance.return_values["filelineno"] = filelineno_retval
657 fileinput._state = instance
658 retval = fileinput.filelineno()
659 self.assertExactlyOneInvocation(instance, "filelineno")
660 self.assertIs(retval, filelineno_retval)
661 self.assertIs(fileinput._state, instance)
662
663class Test_fileinput_fileno(BaseFileInputGlobalMethodsTest):
664 """Unit tests for fileinput.fileno()"""
665
666 def test_state_is_None(self):
667 """Tests fileinput.fileno() when fileinput._state is None.
668 Ensure that it raises RuntimeError with a meaningful error message
669 and does not modify fileinput._state"""
670 fileinput._state = None
671 with self.assertRaises(RuntimeError) as cm:
672 fileinput.fileno()
673 self.assertEqual(("no active input()",), cm.exception.args)
674 self.assertIsNone(fileinput._state)
675
676 def test_state_is_not_None(self):
677 """Tests fileinput.fileno() when fileinput._state is not None.
678 Ensure that it invokes fileinput._state.fileno() exactly once,
679 returns whatever it returns, and does not modify fileinput._state
680 to point to a different object."""
681 fileno_retval = object()
682 instance = MockFileInput()
683 instance.return_values["fileno"] = fileno_retval
684 instance.fileno_retval = fileno_retval
685 fileinput._state = instance
686 retval = fileinput.fileno()
687 self.assertExactlyOneInvocation(instance, "fileno")
688 self.assertIs(retval, fileno_retval)
689 self.assertIs(fileinput._state, instance)
690
691class Test_fileinput_isfirstline(BaseFileInputGlobalMethodsTest):
692 """Unit tests for fileinput.isfirstline()"""
693
694 def test_state_is_None(self):
695 """Tests fileinput.isfirstline() when fileinput._state is None.
696 Ensure that it raises RuntimeError with a meaningful error message
697 and does not modify fileinput._state"""
698 fileinput._state = None
699 with self.assertRaises(RuntimeError) as cm:
700 fileinput.isfirstline()
701 self.assertEqual(("no active input()",), cm.exception.args)
702 self.assertIsNone(fileinput._state)
703
704 def test_state_is_not_None(self):
705 """Tests fileinput.isfirstline() when fileinput._state is not None.
706 Ensure that it invokes fileinput._state.isfirstline() exactly once,
707 returns whatever it returns, and does not modify fileinput._state
708 to point to a different object."""
709 isfirstline_retval = object()
710 instance = MockFileInput()
711 instance.return_values["isfirstline"] = isfirstline_retval
712 fileinput._state = instance
713 retval = fileinput.isfirstline()
714 self.assertExactlyOneInvocation(instance, "isfirstline")
715 self.assertIs(retval, isfirstline_retval)
716 self.assertIs(fileinput._state, instance)
717
718class Test_fileinput_isstdin(BaseFileInputGlobalMethodsTest):
719 """Unit tests for fileinput.isstdin()"""
720
721 def test_state_is_None(self):
722 """Tests fileinput.isstdin() when fileinput._state is None.
723 Ensure that it raises RuntimeError with a meaningful error message
724 and does not modify fileinput._state"""
725 fileinput._state = None
726 with self.assertRaises(RuntimeError) as cm:
727 fileinput.isstdin()
728 self.assertEqual(("no active input()",), cm.exception.args)
729 self.assertIsNone(fileinput._state)
730
731 def test_state_is_not_None(self):
732 """Tests fileinput.isstdin() when fileinput._state is not None.
733 Ensure that it invokes fileinput._state.isstdin() exactly once,
734 returns whatever it returns, and does not modify fileinput._state
735 to point to a different object."""
736 isstdin_retval = object()
737 instance = MockFileInput()
738 instance.return_values["isstdin"] = isstdin_retval
739 fileinput._state = instance
740 retval = fileinput.isstdin()
741 self.assertExactlyOneInvocation(instance, "isstdin")
742 self.assertIs(retval, isstdin_retval)
743 self.assertIs(fileinput._state, instance)
744
745class InvocationRecorder:
746 def __init__(self):
747 self.invocation_count = 0
748 def __call__(self, *args, **kwargs):
749 self.invocation_count += 1
750 self.last_invocation = (args, kwargs)
751
752class Test_hook_compressed(unittest.TestCase):
753 """Unit tests for fileinput.hook_compressed()"""
754
755 def setUp(self):
756 self.fake_open = InvocationRecorder()
757
758 def test_empty_string(self):
759 self.do_test_use_builtin_open("", 1)
760
761 def test_no_ext(self):
762 self.do_test_use_builtin_open("abcd", 2)
763
Ezio Melottic3afbb92011-05-14 10:10:53 +0300764 @unittest.skipUnless(gzip, "Requires gzip and zlib")
briancurtin5eb35912011-03-15 10:59:36 -0400765 def test_gz_ext_fake(self):
briancurtin906f0c42011-03-15 10:29:41 -0400766 original_open = gzip.open
767 gzip.open = self.fake_open
768 try:
769 result = fileinput.hook_compressed("test.gz", 3)
770 finally:
771 gzip.open = original_open
772
773 self.assertEqual(self.fake_open.invocation_count, 1)
774 self.assertEqual(self.fake_open.last_invocation, (("test.gz", 3), {}))
775
briancurtinf84f3c32011-03-18 13:03:17 -0500776 @unittest.skipUnless(bz2, "Requires bz2")
briancurtin5eb35912011-03-15 10:59:36 -0400777 def test_bz2_ext_fake(self):
briancurtin906f0c42011-03-15 10:29:41 -0400778 original_open = bz2.BZ2File
779 bz2.BZ2File = self.fake_open
780 try:
781 result = fileinput.hook_compressed("test.bz2", 4)
782 finally:
783 bz2.BZ2File = original_open
784
785 self.assertEqual(self.fake_open.invocation_count, 1)
786 self.assertEqual(self.fake_open.last_invocation, (("test.bz2", 4), {}))
787
788 def test_blah_ext(self):
789 self.do_test_use_builtin_open("abcd.blah", 5)
790
briancurtin5eb35912011-03-15 10:59:36 -0400791 def test_gz_ext_builtin(self):
briancurtin906f0c42011-03-15 10:29:41 -0400792 self.do_test_use_builtin_open("abcd.Gz", 6)
793
briancurtin5eb35912011-03-15 10:59:36 -0400794 def test_bz2_ext_builtin(self):
briancurtin906f0c42011-03-15 10:29:41 -0400795 self.do_test_use_builtin_open("abcd.Bz2", 7)
796
797 def do_test_use_builtin_open(self, filename, mode):
798 original_open = self.replace_builtin_open(self.fake_open)
799 try:
800 result = fileinput.hook_compressed(filename, mode)
801 finally:
802 self.replace_builtin_open(original_open)
803
804 self.assertEqual(self.fake_open.invocation_count, 1)
805 self.assertEqual(self.fake_open.last_invocation,
806 ((filename, mode), {}))
807
808 @staticmethod
809 def replace_builtin_open(new_open_func):
810 builtins_type = type(__builtins__)
811 if builtins_type is dict:
812 original_open = __builtins__["open"]
813 __builtins__["open"] = new_open_func
814 elif builtins_type is types.ModuleType:
815 original_open = __builtins__.open
816 __builtins__.open = new_open_func
817 else:
818 raise RuntimeError(
819 "unknown __builtins__ type: %r (unable to replace open)" %
820 builtins_type)
821
822 return original_open
823
824class Test_hook_encoded(unittest.TestCase):
825 """Unit tests for fileinput.hook_encoded()"""
826
827 def test(self):
828 encoding = object()
829 result = fileinput.hook_encoded(encoding)
830
831 fake_open = InvocationRecorder()
832 original_open = codecs.open
833 codecs.open = fake_open
834 try:
835 filename = object()
836 mode = object()
837 open_result = result(filename, mode)
838 finally:
839 codecs.open = original_open
840
841 self.assertEqual(fake_open.invocation_count, 1)
842
843 args = fake_open.last_invocation[0]
844 self.assertIs(args[0], filename)
845 self.assertIs(args[1], mode)
846 self.assertIs(args[2], encoding)
Georg Brandl6cb7b652010-07-31 20:08:15 +0000847
Guido van Rossumd8faa362007-04-27 19:54:29 +0000848def test_main():
briancurtin906f0c42011-03-15 10:29:41 -0400849 run_unittest(
850 BufferSizesTests,
851 FileInputTests,
852 Test_fileinput_input,
853 Test_fileinput_close,
854 Test_fileinput_nextfile,
855 Test_fileinput_filename,
856 Test_fileinput_lineno,
857 Test_fileinput_filelineno,
858 Test_fileinput_fileno,
859 Test_fileinput_isfirstline,
860 Test_fileinput_isstdin,
861 Test_hook_compressed,
862 Test_hook_encoded,
863 )
Guido van Rossumd8faa362007-04-27 19:54:29 +0000864
865if __name__ == "__main__":
866 test_main()