blob: b5f80a4f43e22fe8d2680f511855227055cf88f9 [file] [log] [blame]
Georg Brandl7c26d762009-05-05 08:28:49 +00001""" Tests for the linecache module """
2
3import linecache
4import unittest
5import os.path
6from test import test_support as support
7
8
9FILENAME = linecache.__file__
10INVALID_NAME = '!@$)(!@#_1'
11EMPTY = ''
Victor Stinner72bb99d2011-05-17 01:18:33 +020012TESTS = 'inspect_fodder inspect_fodder2 mapping_tests'
Georg Brandl7c26d762009-05-05 08:28:49 +000013TESTS = TESTS.split()
14TEST_PATH = os.path.dirname(support.__file__)
Benjamin Petersond7b0eeb2009-07-19 20:18:21 +000015MODULES = "linecache abc".split()
Georg Brandl7c26d762009-05-05 08:28:49 +000016MODULE_PATH = os.path.dirname(FILENAME)
17
18SOURCE_1 = '''
19" Docstring "
20
21def function():
22 return result
23
24'''
25
26SOURCE_2 = '''
27def f():
28 return 1 + 1
29
30a = f()
31
32'''
33
Benjamin Peterson266e4542010-05-21 21:31:24 +000034SOURCE_3 = '''
35def f():
36 return 3''' # No ending newline
37
38
Georg Brandl7c26d762009-05-05 08:28:49 +000039class 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 Melotti2623a372010-11-21 13:34:58 +000045 self.assertEqual(getline(FILENAME, 2**15), EMPTY)
46 self.assertEqual(getline(FILENAME, -1), EMPTY)
Georg Brandl7c26d762009-05-05 08:28:49 +000047
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 Melotti2623a372010-11-21 13:34:58 +000052 self.assertEqual(getline(EMPTY, 1), EMPTY)
53 self.assertEqual(getline(INVALID_NAME, 1), EMPTY)
Georg Brandl7c26d762009-05-05 08:28:49 +000054
55 # Check whether lines correspond to those from file iteration
56 for entry in TESTS:
57 filename = os.path.join(TEST_PATH, entry) + '.py'
58 for index, line in enumerate(open(filename)):
Ezio Melotti2623a372010-11-21 13:34:58 +000059 self.assertEqual(line, getline(filename, index + 1))
Georg Brandl7c26d762009-05-05 08:28:49 +000060
61 # Check module loading
62 for entry in MODULES:
63 filename = os.path.join(MODULE_PATH, entry) + '.py'
64 for index, line in enumerate(open(filename)):
Ezio Melotti2623a372010-11-21 13:34:58 +000065 self.assertEqual(line, getline(filename, index + 1))
Georg Brandl7c26d762009-05-05 08:28:49 +000066
67 # Check that bogus data isn't returned (issue #1309567)
68 empty = linecache.getlines('a/b/c/__init__.py')
Ezio Melotti2623a372010-11-21 13:34:58 +000069 self.assertEqual(empty, [])
Georg Brandl7c26d762009-05-05 08:28:49 +000070
Benjamin Peterson266e4542010-05-21 21:31:24 +000071 def test_no_ending_newline(self):
72 self.addCleanup(support.unlink, support.TESTFN)
73 with open(support.TESTFN, "w") as fp:
74 fp.write(SOURCE_3)
75 lines = linecache.getlines(support.TESTFN)
76 self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"])
77
Georg Brandl7c26d762009-05-05 08:28:49 +000078 def test_clearcache(self):
79 cached = []
80 for entry in TESTS:
81 filename = os.path.join(TEST_PATH, entry) + '.py'
82 cached.append(filename)
83 linecache.getline(filename, 1)
84
85 # Are all files cached?
86 cached_empty = [fn for fn in cached if fn not in linecache.cache]
Ezio Melotti2623a372010-11-21 13:34:58 +000087 self.assertEqual(cached_empty, [])
Georg Brandl7c26d762009-05-05 08:28:49 +000088
89 # Can we clear the cache?
90 linecache.clearcache()
91 cached_empty = [fn for fn in cached if fn in linecache.cache]
Ezio Melotti2623a372010-11-21 13:34:58 +000092 self.assertEqual(cached_empty, [])
Georg Brandl7c26d762009-05-05 08:28:49 +000093
94 def test_checkcache(self):
95 getline = linecache.getline
Benjamin Peterson6722ac22010-05-21 21:16:12 +000096 # Create a source file and cache its contents
97 source_name = support.TESTFN + '.py'
Benjamin Petersonbd289da2010-05-21 21:17:22 +000098 self.addCleanup(support.unlink, source_name)
Benjamin Peterson6722ac22010-05-21 21:16:12 +000099 with open(source_name, 'w') as source:
100 source.write(SOURCE_1)
101 getline(source_name, 1)
Georg Brandl7c26d762009-05-05 08:28:49 +0000102
Benjamin Peterson6722ac22010-05-21 21:16:12 +0000103 # Keep a copy of the old contents
104 source_list = []
105 with open(source_name) as source:
106 for index, line in enumerate(source):
Ezio Melotti2623a372010-11-21 13:34:58 +0000107 self.assertEqual(line, getline(source_name, index + 1))
Benjamin Peterson6722ac22010-05-21 21:16:12 +0000108 source_list.append(line)
Georg Brandl7c26d762009-05-05 08:28:49 +0000109
Benjamin Peterson6722ac22010-05-21 21:16:12 +0000110 with open(source_name, 'w') as source:
111 source.write(SOURCE_2)
Georg Brandl7c26d762009-05-05 08:28:49 +0000112
Benjamin Peterson6722ac22010-05-21 21:16:12 +0000113 # Try to update a bogus cache entry
114 linecache.checkcache('dummy')
115
116 # Check that the cache matches the old contents
117 for index, line in enumerate(source_list):
Ezio Melotti2623a372010-11-21 13:34:58 +0000118 self.assertEqual(line, getline(source_name, index + 1))
Benjamin Peterson6722ac22010-05-21 21:16:12 +0000119
120 # Update the cache and check whether it matches the new source file
121 linecache.checkcache(source_name)
122 with open(source_name) as source:
123 for index, line in enumerate(source):
Ezio Melotti2623a372010-11-21 13:34:58 +0000124 self.assertEqual(line, getline(source_name, index + 1))
Benjamin Peterson6722ac22010-05-21 21:16:12 +0000125 source_list.append(line)
Georg Brandl7c26d762009-05-05 08:28:49 +0000126
127def test_main():
128 support.run_unittest(LineCacheTests)
129
130if __name__ == "__main__":
131 test_main()