»Core Development>Code coverage>Lib/test/test_linecache.py

Python code coverage for Lib/test/test_linecache.py

#countcontent
1n/a""" Tests for the linecache module """
2n/a
3n/aimport linecache
4n/aimport unittest
5n/aimport os.path
6n/aimport tempfile
7n/aimport tokenize
8n/afrom test import support
9n/a
10n/a
11n/aFILENAME = linecache.__file__
12n/aNONEXISTENT_FILENAME = FILENAME + '.missing'
13n/aINVALID_NAME = '!@$)(!@#_1'
14n/aEMPTY = ''
15n/aTEST_PATH = os.path.dirname(__file__)
16n/aMODULES = "linecache abc".split()
17n/aMODULE_PATH = os.path.dirname(FILENAME)
18n/a
19n/aSOURCE_1 = '''
20n/a" Docstring "
21n/a
22n/adef function():
23n/a return result
24n/a
25n/a'''
26n/a
27n/aSOURCE_2 = '''
28n/adef f():
29n/a return 1 + 1
30n/a
31n/aa = f()
32n/a
33n/a'''
34n/a
35n/aSOURCE_3 = '''
36n/adef f():
37n/a return 3''' # No ending newline
38n/a
39n/a
40n/aclass TempFile:
41n/a
42n/a def setUp(self):
43n/a super().setUp()
44n/a with tempfile.NamedTemporaryFile(delete=False) as fp:
45n/a self.file_name = fp.name
46n/a fp.write(self.file_byte_string)
47n/a self.addCleanup(support.unlink, self.file_name)
48n/a
49n/a
50n/aclass GetLineTestsGoodData(TempFile):
51n/a # file_list = ['list\n', 'of\n', 'good\n', 'strings\n']
52n/a
53n/a def setUp(self):
54n/a self.file_byte_string = ''.join(self.file_list).encode('utf-8')
55n/a super().setUp()
56n/a
57n/a def test_getline(self):
58n/a with tokenize.open(self.file_name) as fp:
59n/a for index, line in enumerate(fp):
60n/a if not line.endswith('\n'):
61n/a line += '\n'
62n/a
63n/a cached_line = linecache.getline(self.file_name, index + 1)
64n/a self.assertEqual(line, cached_line)
65n/a
66n/a def test_getlines(self):
67n/a lines = linecache.getlines(self.file_name)
68n/a self.assertEqual(lines, self.file_list)
69n/a
70n/a
71n/aclass GetLineTestsBadData(TempFile):
72n/a # file_byte_string = b'Bad data goes here'
73n/a
74n/a def test_getline(self):
75n/a self.assertRaises((SyntaxError, UnicodeDecodeError),
76n/a linecache.getline, self.file_name, 1)
77n/a
78n/a def test_getlines(self):
79n/a self.assertRaises((SyntaxError, UnicodeDecodeError),
80n/a linecache.getlines, self.file_name)
81n/a
82n/a
83n/aclass EmptyFile(GetLineTestsGoodData, unittest.TestCase):
84n/a file_list = []
85n/a
86n/a
87n/aclass SingleEmptyLine(GetLineTestsGoodData, unittest.TestCase):
88n/a file_list = ['\n']
89n/a
90n/a
91n/aclass GoodUnicode(GetLineTestsGoodData, unittest.TestCase):
92n/a file_list = ['á\n', 'b\n', 'abcdef\n', 'ááááá\n']
93n/a
94n/a
95n/aclass BadUnicode(GetLineTestsBadData, unittest.TestCase):
96n/a file_byte_string = b'\x80abc'
97n/a
98n/a
99n/aclass LineCacheTests(unittest.TestCase):
100n/a
101n/a def test_getline(self):
102n/a getline = linecache.getline
103n/a
104n/a # Bad values for line number should return an empty string
105n/a self.assertEqual(getline(FILENAME, 2**15), EMPTY)
106n/a self.assertEqual(getline(FILENAME, -1), EMPTY)
107n/a
108n/a # Float values currently raise TypeError, should it?
109n/a self.assertRaises(TypeError, getline, FILENAME, 1.1)
110n/a
111n/a # Bad filenames should return an empty string
112n/a self.assertEqual(getline(EMPTY, 1), EMPTY)
113n/a self.assertEqual(getline(INVALID_NAME, 1), EMPTY)
114n/a
115n/a # Check module loading
116n/a for entry in MODULES:
117n/a filename = os.path.join(MODULE_PATH, entry) + '.py'
118n/a with open(filename) as file:
119n/a for index, line in enumerate(file):
120n/a self.assertEqual(line, getline(filename, index + 1))
121n/a
122n/a # Check that bogus data isn't returned (issue #1309567)
123n/a empty = linecache.getlines('a/b/c/__init__.py')
124n/a self.assertEqual(empty, [])
125n/a
126n/a def test_no_ending_newline(self):
127n/a self.addCleanup(support.unlink, support.TESTFN)
128n/a with open(support.TESTFN, "w") as fp:
129n/a fp.write(SOURCE_3)
130n/a lines = linecache.getlines(support.TESTFN)
131n/a self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"])
132n/a
133n/a def test_clearcache(self):
134n/a cached = []
135n/a for entry in MODULES:
136n/a filename = os.path.join(MODULE_PATH, entry) + '.py'
137n/a cached.append(filename)
138n/a linecache.getline(filename, 1)
139n/a
140n/a # Are all files cached?
141n/a self.assertNotEqual(cached, [])
142n/a cached_empty = [fn for fn in cached if fn not in linecache.cache]
143n/a self.assertEqual(cached_empty, [])
144n/a
145n/a # Can we clear the cache?
146n/a linecache.clearcache()
147n/a cached_empty = [fn for fn in cached if fn in linecache.cache]
148n/a self.assertEqual(cached_empty, [])
149n/a
150n/a def test_checkcache(self):
151n/a getline = linecache.getline
152n/a # Create a source file and cache its contents
153n/a source_name = support.TESTFN + '.py'
154n/a self.addCleanup(support.unlink, source_name)
155n/a with open(source_name, 'w') as source:
156n/a source.write(SOURCE_1)
157n/a getline(source_name, 1)
158n/a
159n/a # Keep a copy of the old contents
160n/a source_list = []
161n/a with open(source_name) as source:
162n/a for index, line in enumerate(source):
163n/a self.assertEqual(line, getline(source_name, index + 1))
164n/a source_list.append(line)
165n/a
166n/a with open(source_name, 'w') as source:
167n/a source.write(SOURCE_2)
168n/a
169n/a # Try to update a bogus cache entry
170n/a linecache.checkcache('dummy')
171n/a
172n/a # Check that the cache matches the old contents
173n/a for index, line in enumerate(source_list):
174n/a self.assertEqual(line, getline(source_name, index + 1))
175n/a
176n/a # Update the cache and check whether it matches the new source file
177n/a linecache.checkcache(source_name)
178n/a with open(source_name) as source:
179n/a for index, line in enumerate(source):
180n/a self.assertEqual(line, getline(source_name, index + 1))
181n/a source_list.append(line)
182n/a
183n/a def test_lazycache_no_globals(self):
184n/a lines = linecache.getlines(FILENAME)
185n/a linecache.clearcache()
186n/a self.assertEqual(False, linecache.lazycache(FILENAME, None))
187n/a self.assertEqual(lines, linecache.getlines(FILENAME))
188n/a
189n/a def test_lazycache_smoke(self):
190n/a lines = linecache.getlines(NONEXISTENT_FILENAME, globals())
191n/a linecache.clearcache()
192n/a self.assertEqual(
193n/a True, linecache.lazycache(NONEXISTENT_FILENAME, globals()))
194n/a self.assertEqual(1, len(linecache.cache[NONEXISTENT_FILENAME]))
195n/a # Note here that we're looking up a nonexistent filename with no
196n/a # globals: this would error if the lazy value wasn't resolved.
197n/a self.assertEqual(lines, linecache.getlines(NONEXISTENT_FILENAME))
198n/a
199n/a def test_lazycache_provide_after_failed_lookup(self):
200n/a linecache.clearcache()
201n/a lines = linecache.getlines(NONEXISTENT_FILENAME, globals())
202n/a linecache.clearcache()
203n/a linecache.getlines(NONEXISTENT_FILENAME)
204n/a linecache.lazycache(NONEXISTENT_FILENAME, globals())
205n/a self.assertEqual(lines, linecache.updatecache(NONEXISTENT_FILENAME))
206n/a
207n/a def test_lazycache_check(self):
208n/a linecache.clearcache()
209n/a linecache.lazycache(NONEXISTENT_FILENAME, globals())
210n/a linecache.checkcache()
211n/a
212n/a def test_lazycache_bad_filename(self):
213n/a linecache.clearcache()
214n/a self.assertEqual(False, linecache.lazycache('', globals()))
215n/a self.assertEqual(False, linecache.lazycache('<foo>', globals()))
216n/a
217n/a def test_lazycache_already_cached(self):
218n/a linecache.clearcache()
219n/a lines = linecache.getlines(NONEXISTENT_FILENAME, globals())
220n/a self.assertEqual(
221n/a False,
222n/a linecache.lazycache(NONEXISTENT_FILENAME, globals()))
223n/a self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME]))
224n/a
225n/a def test_memoryerror(self):
226n/a lines = linecache.getlines(FILENAME)
227n/a self.assertTrue(lines)
228n/a def raise_memoryerror(*args, **kwargs):
229n/a raise MemoryError
230n/a with support.swap_attr(linecache, 'updatecache', raise_memoryerror):
231n/a lines2 = linecache.getlines(FILENAME)
232n/a self.assertEqual(lines2, lines)
233n/a
234n/a linecache.clearcache()
235n/a with support.swap_attr(linecache, 'updatecache', raise_memoryerror):
236n/a lines3 = linecache.getlines(FILENAME)
237n/a self.assertEqual(lines3, [])
238n/a self.assertEqual(linecache.getlines(FILENAME), lines)
239n/a
240n/a
241n/aif __name__ == "__main__":
242n/a unittest.main()