blob: 2dd3672dad785a3e9d2ae1753c61d770afaf889e [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 = ''
12TESTS = 'cjkencodings_test inspect_fodder inspect_fodder2 mapping_tests'
13TESTS = 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
34class LineCacheTests(unittest.TestCase):
35
36 def test_getline(self):
37 getline = linecache.getline
38
39 # Bad values for line number should return an empty string
40 self.assertEquals(getline(FILENAME, 2**15), EMPTY)
41 self.assertEquals(getline(FILENAME, -1), EMPTY)
42
43 # Float values currently raise TypeError, should it?
44 self.assertRaises(TypeError, getline, FILENAME, 1.1)
45
46 # Bad filenames should return an empty string
47 self.assertEquals(getline(EMPTY, 1), EMPTY)
48 self.assertEquals(getline(INVALID_NAME, 1), EMPTY)
49
50 # Check whether lines correspond to those from file iteration
51 for entry in TESTS:
52 filename = os.path.join(TEST_PATH, entry) + '.py'
53 for index, line in enumerate(open(filename)):
54 self.assertEquals(line, getline(filename, index + 1))
55
56 # Check module loading
57 for entry in MODULES:
58 filename = os.path.join(MODULE_PATH, entry) + '.py'
59 for index, line in enumerate(open(filename)):
60 self.assertEquals(line, getline(filename, index + 1))
61
62 # Check that bogus data isn't returned (issue #1309567)
63 empty = linecache.getlines('a/b/c/__init__.py')
64 self.assertEquals(empty, [])
65
66 def test_clearcache(self):
67 cached = []
68 for entry in TESTS:
69 filename = os.path.join(TEST_PATH, entry) + '.py'
70 cached.append(filename)
71 linecache.getline(filename, 1)
72
73 # Are all files cached?
74 cached_empty = [fn for fn in cached if fn not in linecache.cache]
75 self.assertEquals(cached_empty, [])
76
77 # Can we clear the cache?
78 linecache.clearcache()
79 cached_empty = [fn for fn in cached if fn in linecache.cache]
80 self.assertEquals(cached_empty, [])
81
82 def test_checkcache(self):
83 getline = linecache.getline
Benjamin Peterson6722ac22010-05-21 21:16:12 +000084 # Create a source file and cache its contents
85 source_name = support.TESTFN + '.py'
Benjamin Petersonbd289da2010-05-21 21:17:22 +000086 self.addCleanup(support.unlink, source_name)
Benjamin Peterson6722ac22010-05-21 21:16:12 +000087 with open(source_name, 'w') as source:
88 source.write(SOURCE_1)
89 getline(source_name, 1)
Georg Brandl7c26d762009-05-05 08:28:49 +000090
Benjamin Peterson6722ac22010-05-21 21:16:12 +000091 # Keep a copy of the old contents
92 source_list = []
93 with open(source_name) as source:
94 for index, line in enumerate(source):
Antoine Pitrou9958c562010-04-18 19:14:38 +000095 self.assertEquals(line, getline(source_name, index + 1))
Benjamin Peterson6722ac22010-05-21 21:16:12 +000096 source_list.append(line)
Georg Brandl7c26d762009-05-05 08:28:49 +000097
Benjamin Peterson6722ac22010-05-21 21:16:12 +000098 with open(source_name, 'w') as source:
99 source.write(SOURCE_2)
Georg Brandl7c26d762009-05-05 08:28:49 +0000100
Benjamin Peterson6722ac22010-05-21 21:16:12 +0000101 # Try to update a bogus cache entry
102 linecache.checkcache('dummy')
103
104 # Check that the cache matches the old contents
105 for index, line in enumerate(source_list):
106 self.assertEquals(line, getline(source_name, index + 1))
107
108 # Update the cache and check whether it matches the new source file
109 linecache.checkcache(source_name)
110 with open(source_name) as source:
111 for index, line in enumerate(source):
112 self.assertEquals(line, getline(source_name, index + 1))
113 source_list.append(line)
Georg Brandl7c26d762009-05-05 08:28:49 +0000114
115def test_main():
116 support.run_unittest(LineCacheTests)
117
118if __name__ == "__main__":
119 test_main()