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__ |
| 10 | INVALID_NAME = '!@$)(!@#_1' |
| 11 | EMPTY = '' |
| 12 | TESTS = 'cjkencodings_test inspect_fodder inspect_fodder2 mapping_tests' |
| 13 | TESTS = TESTS.split() |
| 14 | TEST_PATH = os.path.dirname(support.__file__) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 15 | MODULES = "linecache abc".split() |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 16 | MODULE_PATH = os.path.dirname(FILENAME) |
| 17 | |
| 18 | SOURCE_1 = ''' |
| 19 | " Docstring " |
| 20 | |
| 21 | def function(): |
| 22 | return result |
| 23 | |
| 24 | ''' |
| 25 | |
| 26 | SOURCE_2 = ''' |
| 27 | def f(): |
| 28 | return 1 + 1 |
| 29 | |
| 30 | a = f() |
| 31 | |
| 32 | ''' |
| 33 | |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 34 | SOURCE_3 = ''' |
| 35 | def f(): |
| 36 | return 3''' # No ending newline |
| 37 | |
| 38 | |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 39 | class LineCacheTests(unittest.TestCase): |
| 40 | |
| 41 | def test_getline(self): |
| 42 | getline = linecache.getline |
| 43 | |
| 44 | # Bad values for line number should return an empty string |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 45 | self.assertEqual(getline(FILENAME, 2**15), EMPTY) |
| 46 | self.assertEqual(getline(FILENAME, -1), EMPTY) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 47 | |
| 48 | # Float values currently raise TypeError, should it? |
| 49 | self.assertRaises(TypeError, getline, FILENAME, 1.1) |
| 50 | |
| 51 | # Bad filenames should return an empty string |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 52 | self.assertEqual(getline(EMPTY, 1), EMPTY) |
| 53 | self.assertEqual(getline(INVALID_NAME, 1), EMPTY) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 54 | |
| 55 | # Check whether lines correspond to those from file iteration |
| 56 | for entry in TESTS: |
| 57 | filename = os.path.join(TEST_PATH, entry) + '.py' |
Brett Cannon | 0174368 | 2010-10-29 23:55:51 +0000 | [diff] [blame] | 58 | with open(filename) as file: |
| 59 | for index, line in enumerate(file): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 60 | self.assertEqual(line, getline(filename, index + 1)) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 61 | |
| 62 | # Check module loading |
| 63 | for entry in MODULES: |
| 64 | filename = os.path.join(MODULE_PATH, entry) + '.py' |
Brett Cannon | 0174368 | 2010-10-29 23:55:51 +0000 | [diff] [blame] | 65 | with open(filename) as file: |
| 66 | for index, line in enumerate(file): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 67 | self.assertEqual(line, getline(filename, index + 1)) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 68 | |
| 69 | # Check that bogus data isn't returned (issue #1309567) |
| 70 | empty = linecache.getlines('a/b/c/__init__.py') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 71 | self.assertEqual(empty, []) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 72 | |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 73 | def test_no_ending_newline(self): |
| 74 | self.addCleanup(support.unlink, support.TESTFN) |
| 75 | with open(support.TESTFN, "w") as fp: |
| 76 | fp.write(SOURCE_3) |
| 77 | lines = linecache.getlines(support.TESTFN) |
| 78 | self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"]) |
| 79 | |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 80 | def test_clearcache(self): |
| 81 | cached = [] |
| 82 | for entry in TESTS: |
| 83 | filename = os.path.join(TEST_PATH, entry) + '.py' |
| 84 | cached.append(filename) |
| 85 | linecache.getline(filename, 1) |
| 86 | |
| 87 | # Are all files cached? |
| 88 | 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] | 89 | self.assertEqual(cached_empty, []) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 90 | |
| 91 | # Can we clear the cache? |
| 92 | linecache.clearcache() |
| 93 | cached_empty = [fn for fn in cached if fn in linecache.cache] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 94 | self.assertEqual(cached_empty, []) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 95 | |
| 96 | def test_checkcache(self): |
| 97 | getline = linecache.getline |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 98 | # Create a source file and cache its contents |
| 99 | source_name = support.TESTFN + '.py' |
| 100 | self.addCleanup(support.unlink, source_name) |
| 101 | with open(source_name, 'w') as source: |
| 102 | source.write(SOURCE_1) |
| 103 | getline(source_name, 1) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 104 | |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 105 | # Keep a copy of the old contents |
| 106 | source_list = [] |
| 107 | with open(source_name) as source: |
| 108 | for index, line in enumerate(source): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 109 | self.assertEqual(line, getline(source_name, index + 1)) |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 110 | source_list.append(line) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 111 | |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 112 | with open(source_name, 'w') as source: |
| 113 | source.write(SOURCE_2) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 114 | |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 115 | # Try to update a bogus cache entry |
| 116 | linecache.checkcache('dummy') |
| 117 | |
| 118 | # Check that the cache matches the old contents |
| 119 | for index, line in enumerate(source_list): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 120 | self.assertEqual(line, getline(source_name, index + 1)) |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 121 | |
| 122 | # Update the cache and check whether it matches the new source file |
| 123 | linecache.checkcache(source_name) |
| 124 | with open(source_name) as source: |
| 125 | for index, line in enumerate(source): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 126 | self.assertEqual(line, getline(source_name, index + 1)) |
Benjamin Peterson | aada7b8 | 2010-05-21 21:45:06 +0000 | [diff] [blame] | 127 | source_list.append(line) |
Georg Brandl | 991f920 | 2009-05-05 08:31:54 +0000 | [diff] [blame] | 128 | |
| 129 | def test_main(): |
| 130 | support.run_unittest(LineCacheTests) |
| 131 | |
| 132 | if __name__ == "__main__": |
| 133 | test_main() |