blob: 7d14534d35bdc5755e371241e8127ecc63cca6f4 [file] [log] [blame]
Georg Brandl991f9202009-05-05 08:31:54 +00001""" Tests for the linecache module """
2
3import linecache
4import unittest
5import os.path
6from test import support
7
8
9FILENAME = linecache.__file__
10INVALID_NAME = '!@$)(!@#_1'
11EMPTY = ''
Victor Stinnere98f1772011-05-16 17:18:51 +020012TESTS = 'inspect_fodder inspect_fodder2 mapping_tests'
Georg Brandl991f9202009-05-05 08:31:54 +000013TESTS = TESTS.split()
14TEST_PATH = os.path.dirname(support.__file__)
Benjamin Petersonbed7d042009-07-19 21:01:52 +000015MODULES = "linecache abc".split()
Georg Brandl991f9202009-05-05 08:31:54 +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 Petersonaada7b82010-05-21 21:45:06 +000034SOURCE_3 = '''
35def f():
36 return 3''' # No ending newline
37
38
Georg Brandl991f9202009-05-05 08:31:54 +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 Melottib3aedd42010-11-20 19:04:17 +000045 self.assertEqual(getline(FILENAME, 2**15), EMPTY)
46 self.assertEqual(getline(FILENAME, -1), EMPTY)
Georg Brandl991f9202009-05-05 08:31:54 +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 Melottib3aedd42010-11-20 19:04:17 +000052 self.assertEqual(getline(EMPTY, 1), EMPTY)
53 self.assertEqual(getline(INVALID_NAME, 1), EMPTY)
Georg Brandl991f9202009-05-05 08:31:54 +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'
Brett Cannon01743682010-10-29 23:55:51 +000058 with open(filename) as file:
59 for index, line in enumerate(file):
Ezio Melottib3aedd42010-11-20 19:04:17 +000060 self.assertEqual(line, getline(filename, index + 1))
Georg Brandl991f9202009-05-05 08:31:54 +000061
62 # Check module loading
63 for entry in MODULES:
64 filename = os.path.join(MODULE_PATH, entry) + '.py'
Brett Cannon01743682010-10-29 23:55:51 +000065 with open(filename) as file:
66 for index, line in enumerate(file):
Ezio Melottib3aedd42010-11-20 19:04:17 +000067 self.assertEqual(line, getline(filename, index + 1))
Georg Brandl991f9202009-05-05 08:31:54 +000068
69 # Check that bogus data isn't returned (issue #1309567)
70 empty = linecache.getlines('a/b/c/__init__.py')
Ezio Melottib3aedd42010-11-20 19:04:17 +000071 self.assertEqual(empty, [])
Georg Brandl991f9202009-05-05 08:31:54 +000072
Benjamin Petersonaada7b82010-05-21 21:45:06 +000073 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 Brandl991f9202009-05-05 08:31:54 +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]
Ezio Melottib3aedd42010-11-20 19:04:17 +000089 self.assertEqual(cached_empty, [])
Georg Brandl991f9202009-05-05 08:31:54 +000090
91 # Can we clear the cache?
92 linecache.clearcache()
93 cached_empty = [fn for fn in cached if fn in linecache.cache]
Ezio Melottib3aedd42010-11-20 19:04:17 +000094 self.assertEqual(cached_empty, [])
Georg Brandl991f9202009-05-05 08:31:54 +000095
96 def test_checkcache(self):
97 getline = linecache.getline
Benjamin Petersonaada7b82010-05-21 21:45:06 +000098 # 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 Brandl991f9202009-05-05 08:31:54 +0000104
Benjamin Petersonaada7b82010-05-21 21:45:06 +0000105 # 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 Melottib3aedd42010-11-20 19:04:17 +0000109 self.assertEqual(line, getline(source_name, index + 1))
Benjamin Petersonaada7b82010-05-21 21:45:06 +0000110 source_list.append(line)
Georg Brandl991f9202009-05-05 08:31:54 +0000111
Benjamin Petersonaada7b82010-05-21 21:45:06 +0000112 with open(source_name, 'w') as source:
113 source.write(SOURCE_2)
Georg Brandl991f9202009-05-05 08:31:54 +0000114
Benjamin Petersonaada7b82010-05-21 21:45:06 +0000115 # 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 Melottib3aedd42010-11-20 19:04:17 +0000120 self.assertEqual(line, getline(source_name, index + 1))
Benjamin Petersonaada7b82010-05-21 21:45:06 +0000121
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 Melottib3aedd42010-11-20 19:04:17 +0000126 self.assertEqual(line, getline(source_name, index + 1))
Benjamin Petersonaada7b82010-05-21 21:45:06 +0000127 source_list.append(line)
Georg Brandl991f9202009-05-05 08:31:54 +0000128
129def test_main():
130 support.run_unittest(LineCacheTests)
131
132if __name__ == "__main__":
133 test_main()