use addCleanup

This commit is contained in:
Benjamin Peterson 2010-05-21 21:16:12 +00:00
parent 233eb54051
commit 6722ac2f00
1 changed files with 26 additions and 29 deletions

View File

@ -81,39 +81,36 @@ class LineCacheTests(unittest.TestCase):
def test_checkcache(self): def test_checkcache(self):
getline = linecache.getline getline = linecache.getline
try: # Create a source file and cache its contents
# Create a source file and cache its contents source_name = support.TESTFN + '.py'
source_name = support.TESTFN + '.py' self.addCleanup(test_support.unlink, source_name)
with open(source_name, 'w') as source: with open(source_name, 'w') as source:
source.write(SOURCE_1) source.write(SOURCE_1)
getline(source_name, 1) getline(source_name, 1)
# Keep a copy of the old contents # Keep a copy of the old contents
source_list = [] source_list = []
with open(source_name) as source: with open(source_name) as source:
for index, line in enumerate(source): for index, line in enumerate(source):
self.assertEquals(line, getline(source_name, index + 1))
source_list.append(line)
with open(source_name, 'w') as source:
source.write(SOURCE_2)
# Try to update a bogus cache entry
linecache.checkcache('dummy')
# Check that the cache matches the old contents
for index, line in enumerate(source_list):
self.assertEquals(line, getline(source_name, index + 1)) self.assertEquals(line, getline(source_name, index + 1))
source_list.append(line)
# Update the cache and check whether it matches the new source file with open(source_name, 'w') as source:
linecache.checkcache(source_name) source.write(SOURCE_2)
with open(source_name) as source:
for index, line in enumerate(source):
self.assertEquals(line, getline(source_name, index + 1))
source_list.append(line)
finally: # Try to update a bogus cache entry
support.unlink(source_name) linecache.checkcache('dummy')
# Check that the cache matches the old contents
for index, line in enumerate(source_list):
self.assertEquals(line, getline(source_name, index + 1))
# Update the cache and check whether it matches the new source file
linecache.checkcache(source_name)
with open(source_name) as source:
for index, line in enumerate(source):
self.assertEquals(line, getline(source_name, index + 1))
source_list.append(line)
def test_main(): def test_main():
support.run_unittest(LineCacheTests) support.run_unittest(LineCacheTests)