blob: 945d7689cfb18b2897535008d341597b84dfe5a1 [file] [log] [blame]
Georg Brandle081eef2009-05-26 09:04:23 +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__)
15MODULES = "linecache unittest".split()
16MODULE_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 Petersona3401652010-05-21 21:45:16 +000034SOURCE_3 = '''
35def f():
36 return 3''' # No ending newline
37
38
Georg Brandle081eef2009-05-26 09:04:23 +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
45 self.assertEquals(getline(FILENAME, 2**15), EMPTY)
46 self.assertEquals(getline(FILENAME, -1), EMPTY)
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
52 self.assertEquals(getline(EMPTY, 1), EMPTY)
53 self.assertEquals(getline(INVALID_NAME, 1), EMPTY)
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'
58 for index, line in enumerate(open(filename)):
59 self.assertEquals(line, getline(filename, index + 1))
60
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)):
65 self.assertEquals(line, getline(filename, index + 1))
66
67 # Check that bogus data isn't returned (issue #1309567)
68 empty = linecache.getlines('a/b/c/__init__.py')
69 self.assertEquals(empty, [])
70
Benjamin Petersona3401652010-05-21 21:45:16 +000071 def test_no_ending_newline(self):
72 try:
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 finally:
78 support.unlink(support.TESTFN)
79
Georg Brandle081eef2009-05-26 09:04:23 +000080 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]
89 self.assertEquals(cached_empty, [])
90
91 # Can we clear the cache?
92 linecache.clearcache()
93 cached_empty = [fn for fn in cached if fn in linecache.cache]
94 self.assertEquals(cached_empty, [])
95
96 def test_checkcache(self):
97 getline = linecache.getline
98 try:
99 # Create a source file and cache its contents
R. David Murray1b479f22009-12-04 00:01:31 +0000100 source_name = support.TESTFN + '.py'
101 with open(source_name, 'w') as source:
102 source.write(SOURCE_1)
103 source.close()
104 getline(source_name, 1)
Georg Brandle081eef2009-05-26 09:04:23 +0000105
R. David Murray1b479f22009-12-04 00:01:31 +0000106 # Keep a copy of the old contents
107 source_list = []
108 source = open(source_name)
109 for index, line in enumerate(source):
110 self.assertEquals(line, getline(source_name, index + 1))
111 source_list.append(line)
112 source.close()
Georg Brandle081eef2009-05-26 09:04:23 +0000113
R. David Murray1b479f22009-12-04 00:01:31 +0000114 source = open(source_name, 'w')
115 source.write(SOURCE_2)
116 source.close()
Georg Brandle081eef2009-05-26 09:04:23 +0000117
R. David Murray1b479f22009-12-04 00:01:31 +0000118 # Try to update a bogus cache entry
119 linecache.checkcache('dummy')
Georg Brandle081eef2009-05-26 09:04:23 +0000120
R. David Murray1b479f22009-12-04 00:01:31 +0000121 # Check that the cache matches the old contents
122 for index, line in enumerate(source_list):
123 self.assertEquals(line, getline(source_name, index + 1))
Georg Brandle081eef2009-05-26 09:04:23 +0000124
R. David Murray1b479f22009-12-04 00:01:31 +0000125 # Update the cache and check whether it matches the new source file
126 linecache.checkcache(source_name)
127 source = open(source_name)
128 for index, line in enumerate(source):
129 self.assertEquals(line, getline(source_name, index + 1))
130 source_list.append(line)
Georg Brandle081eef2009-05-26 09:04:23 +0000131
132 finally:
R. David Murray1b479f22009-12-04 00:01:31 +0000133 support.unlink(source_name)
Georg Brandle081eef2009-05-26 09:04:23 +0000134
135def test_main():
136 support.run_unittest(LineCacheTests)
137
138if __name__ == "__main__":
139 test_main()