Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Tests for fileinput module. |
| 3 | Nick Mathewson |
| 4 | ''' |
| 5 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test.support import verbose, TESTFN, run_unittest |
| 8 | from test.support import unlink as safe_unlink |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 9 | import sys, re |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 10 | from io import StringIO |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 11 | from fileinput import FileInput, hook_encoded |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 12 | |
| 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. |
| 17 | |
| 18 | # Write lines (a list of lines) to temp file number i, and return the |
| 19 | # temp file's name. |
Tim Peters | 4d7cad1 | 2006-02-19 21:22:10 +0000 | [diff] [blame] | 20 | 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] | 21 | name = TESTFN + str(i) |
Tim Peters | 4d7cad1 | 2006-02-19 21:22:10 +0000 | [diff] [blame] | 22 | f = open(name, mode) |
Guido van Rossum | c43e79f | 2007-06-18 18:26:36 +0000 | [diff] [blame] | 23 | for line in lines: |
| 24 | f.write(line) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 25 | f.close() |
| 26 | return name |
| 27 | |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 28 | def remove_tempfiles(*names): |
| 29 | for name in names: |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 30 | if name: |
| 31 | safe_unlink(name) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 33 | class BufferSizesTests(unittest.TestCase): |
| 34 | def test_buffer_sizes(self): |
| 35 | # First, run the tests with default and teeny buffer size. |
| 36 | for round, bs in (0, 0), (1, 30): |
Neal Norwitz | 2595e76 | 2008-03-24 06:10:13 +0000 | [diff] [blame] | 37 | t1 = t2 = t3 = t4 = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 38 | try: |
| 39 | t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)]) |
| 40 | t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)]) |
| 41 | t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)]) |
| 42 | t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)]) |
| 43 | self.buffer_size_test(t1, t2, t3, t4, bs, round) |
| 44 | finally: |
| 45 | remove_tempfiles(t1, t2, t3, t4) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 47 | def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0): |
| 48 | pat = re.compile(r'LINE (\d+) OF FILE (\d+)') |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 50 | start = 1 + round*6 |
| 51 | if verbose: |
| 52 | print('%s. Simple iteration (bs=%s)' % (start+0, bs)) |
| 53 | fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 54 | lines = list(fi) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 55 | fi.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 56 | self.assertEqual(len(lines), 31) |
| 57 | self.assertEqual(lines[4], 'Line 5 of file 1\n') |
| 58 | self.assertEqual(lines[30], 'Line 1 of file 4\n') |
| 59 | self.assertEqual(fi.lineno(), 31) |
| 60 | self.assertEqual(fi.filename(), t4) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 62 | if verbose: |
| 63 | print('%s. Status variables (bs=%s)' % (start+1, bs)) |
| 64 | fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) |
| 65 | s = "x" |
| 66 | while s and s != 'Line 6 of file 2\n': |
| 67 | s = fi.readline() |
| 68 | self.assertEqual(fi.filename(), t2) |
| 69 | self.assertEqual(fi.lineno(), 21) |
| 70 | self.assertEqual(fi.filelineno(), 6) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 71 | self.assertFalse(fi.isfirstline()) |
| 72 | self.assertFalse(fi.isstdin()) |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 74 | if verbose: |
| 75 | print('%s. Nextfile (bs=%s)' % (start+2, bs)) |
| 76 | fi.nextfile() |
| 77 | self.assertEqual(fi.readline(), 'Line 1 of file 3\n') |
| 78 | self.assertEqual(fi.lineno(), 22) |
| 79 | fi.close() |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 81 | if verbose: |
| 82 | print('%s. Stdin (bs=%s)' % (start+3, bs)) |
| 83 | fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs) |
| 84 | savestdin = sys.stdin |
| 85 | try: |
| 86 | sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n") |
| 87 | lines = list(fi) |
| 88 | self.assertEqual(len(lines), 33) |
| 89 | self.assertEqual(lines[32], 'Line 2 of stdin\n') |
| 90 | self.assertEqual(fi.filename(), '<stdin>') |
| 91 | fi.nextfile() |
| 92 | finally: |
| 93 | sys.stdin = savestdin |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 95 | if verbose: |
| 96 | print('%s. Boundary conditions (bs=%s)' % (start+4, bs)) |
| 97 | fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) |
| 98 | self.assertEqual(fi.lineno(), 0) |
| 99 | self.assertEqual(fi.filename(), None) |
| 100 | fi.nextfile() |
| 101 | self.assertEqual(fi.lineno(), 0) |
| 102 | self.assertEqual(fi.filename(), None) |
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. Inplace (bs=%s)' % (start+5, bs)) |
| 106 | savestdout = sys.stdout |
| 107 | try: |
| 108 | fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs) |
| 109 | for line in fi: |
| 110 | line = line[:-1].upper() |
| 111 | print(line) |
| 112 | fi.close() |
| 113 | finally: |
| 114 | sys.stdout = savestdout |
Tim Peters | 3230d5c | 2001-07-11 22:21:17 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 116 | fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) |
| 117 | for line in fi: |
| 118 | self.assertEqual(line[-1], '\n') |
| 119 | m = pat.match(line[:-1]) |
| 120 | self.assertNotEqual(m, None) |
| 121 | self.assertEqual(int(m.group(1)), fi.filelineno()) |
| 122 | fi.close() |
Georg Brandl | e466217 | 2006-02-19 09:51:27 +0000 | [diff] [blame] | 123 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 124 | class FileInputTests(unittest.TestCase): |
| 125 | def test_zero_byte_files(self): |
Neal Norwitz | 2595e76 | 2008-03-24 06:10:13 +0000 | [diff] [blame] | 126 | t1 = t2 = t3 = t4 = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 127 | try: |
| 128 | t1 = writeTmp(1, [""]) |
| 129 | t2 = writeTmp(2, [""]) |
| 130 | t3 = writeTmp(3, ["The only line there is.\n"]) |
| 131 | t4 = writeTmp(4, [""]) |
| 132 | fi = FileInput(files=(t1, t2, t3, t4)) |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 134 | line = fi.readline() |
| 135 | self.assertEqual(line, 'The only line there is.\n') |
| 136 | self.assertEqual(fi.lineno(), 1) |
| 137 | self.assertEqual(fi.filelineno(), 1) |
| 138 | self.assertEqual(fi.filename(), t3) |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 140 | line = fi.readline() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 141 | self.assertFalse(line) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 142 | self.assertEqual(fi.lineno(), 1) |
| 143 | self.assertEqual(fi.filelineno(), 0) |
| 144 | self.assertEqual(fi.filename(), t4) |
| 145 | fi.close() |
| 146 | finally: |
| 147 | remove_tempfiles(t1, t2, t3, t4) |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 149 | def test_files_that_dont_end_with_newline(self): |
Neal Norwitz | 2595e76 | 2008-03-24 06:10:13 +0000 | [diff] [blame] | 150 | t1 = t2 = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 151 | try: |
| 152 | t1 = writeTmp(1, ["A\nB\nC"]) |
| 153 | t2 = writeTmp(2, ["D\nE\nF"]) |
| 154 | fi = FileInput(files=(t1, t2)) |
| 155 | lines = list(fi) |
| 156 | self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"]) |
| 157 | self.assertEqual(fi.filelineno(), 3) |
| 158 | self.assertEqual(fi.lineno(), 6) |
| 159 | finally: |
| 160 | remove_tempfiles(t1, t2) |
| 161 | |
Guido van Rossum | c43e79f | 2007-06-18 18:26:36 +0000 | [diff] [blame] | 162 | ## def test_unicode_filenames(self): |
| 163 | ## # XXX A unicode string is always returned by writeTmp. |
| 164 | ## # So is this needed? |
| 165 | ## try: |
| 166 | ## t1 = writeTmp(1, ["A\nB"]) |
| 167 | ## encoding = sys.getfilesystemencoding() |
| 168 | ## if encoding is None: |
| 169 | ## encoding = 'ascii' |
| 170 | ## fi = FileInput(files=str(t1, encoding)) |
| 171 | ## lines = list(fi) |
| 172 | ## self.assertEqual(lines, ["A\n", "B"]) |
| 173 | ## finally: |
| 174 | ## remove_tempfiles(t1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 175 | |
| 176 | def test_fileno(self): |
Neal Norwitz | 2595e76 | 2008-03-24 06:10:13 +0000 | [diff] [blame] | 177 | t1 = t2 = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 178 | try: |
| 179 | t1 = writeTmp(1, ["A\nB"]) |
| 180 | t2 = writeTmp(2, ["C\nD"]) |
| 181 | fi = FileInput(files=(t1, t2)) |
| 182 | self.assertEqual(fi.fileno(), -1) |
| 183 | line =next( fi) |
| 184 | self.assertNotEqual(fi.fileno(), -1) |
| 185 | fi.nextfile() |
| 186 | self.assertEqual(fi.fileno(), -1) |
| 187 | line = list(fi) |
| 188 | self.assertEqual(fi.fileno(), -1) |
| 189 | finally: |
| 190 | remove_tempfiles(t1, t2) |
| 191 | |
| 192 | def test_opening_mode(self): |
| 193 | try: |
| 194 | # invalid mode, should raise ValueError |
| 195 | fi = FileInput(mode="w") |
| 196 | self.fail("FileInput should reject invalid mode argument") |
| 197 | except ValueError: |
| 198 | pass |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 199 | t1 = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 200 | try: |
| 201 | # try opening in universal newline mode |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 202 | t1 = writeTmp(1, [b"A\nB\r\nC\rD"], mode="wb") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 203 | fi = FileInput(files=t1, mode="U") |
| 204 | lines = list(fi) |
| 205 | self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"]) |
| 206 | finally: |
| 207 | remove_tempfiles(t1) |
| 208 | |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 209 | def test_file_opening_hook(self): |
| 210 | try: |
| 211 | # cannot use openhook and inplace mode |
| 212 | fi = FileInput(inplace=1, openhook=lambda f, m: None) |
| 213 | self.fail("FileInput should raise if both inplace " |
| 214 | "and openhook arguments are given") |
| 215 | except ValueError: |
| 216 | pass |
| 217 | try: |
| 218 | fi = FileInput(openhook=1) |
| 219 | self.fail("FileInput should check openhook for being callable") |
| 220 | except ValueError: |
| 221 | pass |
| 222 | # XXX The rot13 codec was removed. |
| 223 | # So this test needs to be changed to use something else. |
| 224 | # (Or perhaps the API needs to change so we can just pass |
| 225 | # an encoding rather than using a hook?) |
Guido van Rossum | c43e79f | 2007-06-18 18:26:36 +0000 | [diff] [blame] | 226 | ## try: |
| 227 | ## t1 = writeTmp(1, ["A\nB"], mode="wb") |
| 228 | ## fi = FileInput(files=t1, openhook=hook_encoded("rot13")) |
| 229 | ## lines = list(fi) |
| 230 | ## self.assertEqual(lines, ["N\n", "O"]) |
| 231 | ## finally: |
| 232 | ## remove_tempfiles(t1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 233 | |
Georg Brandl | 6cb7b65 | 2010-07-31 20:08:15 +0000 | [diff] [blame] | 234 | def test_context_manager(self): |
| 235 | try: |
| 236 | t1 = writeTmp(1, ["A\nB\nC"]) |
| 237 | t2 = writeTmp(2, ["D\nE\nF"]) |
| 238 | with FileInput(files=(t1, t2)) as fi: |
| 239 | lines = list(fi) |
| 240 | self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"]) |
| 241 | self.assertEqual(fi.filelineno(), 3) |
| 242 | self.assertEqual(fi.lineno(), 6) |
| 243 | self.assertEqual(fi._files, ()) |
| 244 | finally: |
| 245 | remove_tempfiles(t1, t2) |
| 246 | |
| 247 | def test_close_on_exception(self): |
| 248 | try: |
| 249 | t1 = writeTmp(1, [""]) |
| 250 | with FileInput(files=t1) as fi: |
| 251 | raise IOError |
| 252 | except IOError: |
| 253 | self.assertEqual(fi._files, ()) |
| 254 | finally: |
| 255 | remove_tempfiles(t1) |
| 256 | |
| 257 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 258 | def test_main(): |
| 259 | run_unittest(BufferSizesTests, FileInputTests) |
| 260 | |
| 261 | if __name__ == "__main__": |
| 262 | test_main() |