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