Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 1 | """ Tests for the linecache module """ |
| 2 | |
| 3 | import linecache |
| 4 | import unittest |
| 5 | import os.path |
| 6 | from test import support |
| 7 | |
| 8 | |
| 9 | FILENAME = linecache.__file__ |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 10 | NONEXISTENT_FILENAME = FILENAME + '.missing' |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 11 | INVALID_NAME = '!@$)(!@#_1' |
| 12 | EMPTY = '' |
Victor Stinner | e98f177 | 2011-05-16 17:18:51 +0200 | [diff] [blame] | 13 | TESTS = 'inspect_fodder inspect_fodder2 mapping_tests' |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 14 | TESTS = TESTS.split() |
Nick Coghlan | fb15aa1 | 2013-07-28 20:56:19 +1000 | [diff] [blame] | 15 | TEST_PATH = os.path.dirname(__file__) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 16 | MODULES = "linecache abc".split() |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 17 | MODULE_PATH = os.path.dirname(FILENAME) |
| 18 | |
| 19 | SOURCE_1 = ''' |
| 20 | " Docstring " |
| 21 | |
| 22 | def function(): |
| 23 | return result |
| 24 | |
| 25 | ''' |
| 26 | |
| 27 | SOURCE_2 = ''' |
| 28 | def f(): |
| 29 | return 1 + 1 |
| 30 | |
| 31 | a = f() |
| 32 | |
| 33 | ''' |
| 34 | |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 35 | SOURCE_3 = ''' |
| 36 | def f(): |
| 37 | return 3''' # No ending newline |
| 38 | |
| 39 | |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 40 | class LineCacheTests(unittest.TestCase): |
| 41 | |
| 42 | def test_getline(self): |
| 43 | getline = linecache.getline |
| 44 | |
| 45 | # Bad values for line number should return an empty string |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 46 | self.assertEqual(getline(FILENAME, 2**15), EMPTY) |
| 47 | self.assertEqual(getline(FILENAME, -1), EMPTY) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 48 | |
| 49 | # Float values currently raise TypeError, should it? |
| 50 | self.assertRaises(TypeError, getline, FILENAME, 1.1) |
| 51 | |
| 52 | # Bad filenames should return an empty string |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 53 | self.assertEqual(getline(EMPTY, 1), EMPTY) |
| 54 | self.assertEqual(getline(INVALID_NAME, 1), EMPTY) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 55 | |
| 56 | # Check whether lines correspond to those from file iteration |
| 57 | for entry in TESTS: |
| 58 | filename = os.path.join(TEST_PATH, entry) + '.py' |
Brett Cannon | 0174368 | 2010-10-29 23:55:51 +0000 | [diff] [blame] | 59 | with open(filename) as file: |
| 60 | for index, line in enumerate(file): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 61 | self.assertEqual(line, getline(filename, index + 1)) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 62 | |
| 63 | # Check module loading |
| 64 | for entry in MODULES: |
| 65 | filename = os.path.join(MODULE_PATH, entry) + '.py' |
Brett Cannon | 0174368 | 2010-10-29 23:55:51 +0000 | [diff] [blame] | 66 | with open(filename) as file: |
| 67 | for index, line in enumerate(file): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 68 | self.assertEqual(line, getline(filename, index + 1)) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 69 | |
| 70 | # Check that bogus data isn't returned (issue #1309567) |
| 71 | empty = linecache.getlines('a/b/c/__init__.py') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 72 | self.assertEqual(empty, []) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 73 | |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 74 | def test_no_ending_newline(self): |
| 75 | self.addCleanup(support.unlink, support.TESTFN) |
| 76 | with open(support.TESTFN, "w") as fp: |
| 77 | fp.write(SOURCE_3) |
| 78 | lines = linecache.getlines(support.TESTFN) |
| 79 | self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"]) |
| 80 | |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 81 | def test_clearcache(self): |
| 82 | cached = [] |
| 83 | for entry in TESTS: |
| 84 | filename = os.path.join(TEST_PATH, entry) + '.py' |
| 85 | cached.append(filename) |
| 86 | linecache.getline(filename, 1) |
| 87 | |
| 88 | # Are all files cached? |
| 89 | cached_empty = [fn for fn in cached if fn not in linecache.cache] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 90 | self.assertEqual(cached_empty, []) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 91 | |
| 92 | # Can we clear the cache? |
| 93 | linecache.clearcache() |
| 94 | cached_empty = [fn for fn in cached if fn in linecache.cache] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 95 | self.assertEqual(cached_empty, []) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 96 | |
| 97 | def test_checkcache(self): |
| 98 | getline = linecache.getline |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 99 | # Create a source file and cache its contents |
| 100 | source_name = support.TESTFN + '.py' |
| 101 | self.addCleanup(support.unlink, source_name) |
| 102 | with open(source_name, 'w') as source: |
| 103 | source.write(SOURCE_1) |
| 104 | getline(source_name, 1) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 105 | |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 106 | # Keep a copy of the old contents |
| 107 | source_list = [] |
| 108 | with open(source_name) as source: |
| 109 | for index, line in enumerate(source): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 110 | self.assertEqual(line, getline(source_name, index + 1)) |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 111 | source_list.append(line) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 112 | |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 113 | with open(source_name, 'w') as source: |
| 114 | source.write(SOURCE_2) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 115 | |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 116 | # Try to update a bogus cache entry |
| 117 | linecache.checkcache('dummy') |
| 118 | |
| 119 | # Check that the cache matches the old contents |
| 120 | for index, line in enumerate(source_list): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 121 | self.assertEqual(line, getline(source_name, index + 1)) |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 122 | |
| 123 | # Update the cache and check whether it matches the new source file |
| 124 | linecache.checkcache(source_name) |
| 125 | with open(source_name) as source: |
| 126 | for index, line in enumerate(source): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 127 | self.assertEqual(line, getline(source_name, index + 1)) |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 128 | source_list.append(line) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 129 | |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 130 | def test_lazycache_no_globals(self): |
| 131 | lines = linecache.getlines(FILENAME) |
| 132 | linecache.clearcache() |
| 133 | self.assertEqual(False, linecache.lazycache(FILENAME, None)) |
| 134 | self.assertEqual(lines, linecache.getlines(FILENAME)) |
| 135 | |
| 136 | def test_lazycache_smoke(self): |
| 137 | lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) |
| 138 | linecache.clearcache() |
| 139 | self.assertEqual( |
| 140 | True, linecache.lazycache(NONEXISTENT_FILENAME, globals())) |
| 141 | self.assertEqual(1, len(linecache.cache[NONEXISTENT_FILENAME])) |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 142 | # Note here that we're looking up a nonexistent filename with no |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 143 | # globals: this would error if the lazy value wasn't resolved. |
| 144 | self.assertEqual(lines, linecache.getlines(NONEXISTENT_FILENAME)) |
| 145 | |
| 146 | def test_lazycache_provide_after_failed_lookup(self): |
| 147 | linecache.clearcache() |
| 148 | lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) |
| 149 | linecache.clearcache() |
| 150 | linecache.getlines(NONEXISTENT_FILENAME) |
| 151 | linecache.lazycache(NONEXISTENT_FILENAME, globals()) |
| 152 | self.assertEqual(lines, linecache.updatecache(NONEXISTENT_FILENAME)) |
| 153 | |
| 154 | def test_lazycache_check(self): |
| 155 | linecache.clearcache() |
| 156 | linecache.lazycache(NONEXISTENT_FILENAME, globals()) |
| 157 | linecache.checkcache() |
| 158 | |
| 159 | def test_lazycache_bad_filename(self): |
| 160 | linecache.clearcache() |
| 161 | self.assertEqual(False, linecache.lazycache('', globals())) |
| 162 | self.assertEqual(False, linecache.lazycache('<foo>', globals())) |
| 163 | |
| 164 | def test_lazycache_already_cached(self): |
| 165 | linecache.clearcache() |
| 166 | lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) |
| 167 | self.assertEqual( |
| 168 | False, |
| 169 | linecache.lazycache(NONEXISTENT_FILENAME, globals())) |
| 170 | self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME])) |
| 171 | |
Serhiy Storchaka | c512adc | 2015-04-01 16:54:05 +0300 | [diff] [blame] | 172 | def test_memoryerror(self): |
| 173 | lines = linecache.getlines(FILENAME) |
| 174 | self.assertTrue(lines) |
| 175 | def raise_memoryerror(*args, **kwargs): |
| 176 | raise MemoryError |
| 177 | with support.swap_attr(linecache, 'updatecache', raise_memoryerror): |
| 178 | lines2 = linecache.getlines(FILENAME) |
| 179 | self.assertEqual(lines2, lines) |
Robert Collins | 6bc2c1e | 2015-03-05 12:07:57 +1300 | [diff] [blame] | 180 | |
Serhiy Storchaka | c512adc | 2015-04-01 16:54:05 +0300 | [diff] [blame] | 181 | linecache.clearcache() |
| 182 | with support.swap_attr(linecache, 'updatecache', raise_memoryerror): |
| 183 | lines3 = linecache.getlines(FILENAME) |
| 184 | self.assertEqual(lines3, []) |
| 185 | self.assertEqual(linecache.getlines(FILENAME), lines) |
| 186 | |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 187 | |
| 188 | if __name__ == "__main__": |
Serhiy Storchaka | c512adc | 2015-04-01 16:54:05 +0300 | [diff] [blame] | 189 | unittest.main() |