Skip to content

bpo-25872: Add unit tests for linecache and threading #25913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 18, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor LineCacheInvalidationTests
  • Loading branch information
uniocto committed May 9, 2021
commit 8376d21502d7ea88c7b263bcdedd7f461a7ef12d
83 changes: 42 additions & 41 deletions Lib/test/test_linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,47 +238,48 @@ def raise_memoryerror(*args, **kwargs):
self.assertEqual(lines3, [])
self.assertEqual(linecache.getlines(FILENAME), lines)

def test_oserror(self):
def _oserror_helper():
linecache.clearcache()
be_deleted_file = os_helper.TESTFN + '.1'
be_modified_file = os_helper.TESTFN + '.2'
unchange_file = os_helper.TESTFN + '.3'
self.addCleanup(os_helper.unlink, be_deleted_file)
self.addCleanup(os_helper.unlink, be_modified_file)
self.addCleanup(os_helper.unlink, unchange_file)
with open(be_deleted_file, 'w', encoding='utf-8') as source:
source.write('print("will be deleted")')
with open(be_modified_file, 'w', encoding='utf-8') as source:
source.write('print("will be modified")')
with open(unchange_file, 'w', encoding='utf-8') as source:
source.write('print("unchange")')

_ = linecache.getlines(be_deleted_file)
_ = linecache.getlines(be_modified_file)
_ = linecache.getlines(unchange_file)
self.assertEqual(3, len(linecache.cache.keys()))

os.remove(be_deleted_file)
with open(be_modified_file, 'w', encoding='utf-8') as source:
source.write('print("was modified")')
return (be_deleted_file, be_modified_file, unchange_file)

deleted_file, modified_file, unchange_file = _oserror_helper()
_ = linecache.checkcache(deleted_file)
self.assertEqual(2, len(linecache.cache.keys()))
_ = linecache.checkcache(modified_file)
self.assertEqual(1, len(linecache.cache.keys()))
_ = linecache.checkcache(unchange_file)
self.assertEqual(1, len(linecache.cache.keys()))

deleted_file, modified_file, unchange_file = _oserror_helper()
_ = linecache.updatecache(deleted_file)
self.assertEqual(2, len(linecache.cache.keys()))
_ = linecache.updatecache(modified_file)
self.assertEqual(2, len(linecache.cache.keys()))
_ = linecache.updatecache(unchange_file)
self.assertEqual(2, len(linecache.cache.keys()))

class LineCacheInvalidationTests(unittest.TestCase):
def setUp(self):
super().setUp()
linecache.clearcache()
self.deleted_file = os_helper.TESTFN + '.1'
self.modified_file = os_helper.TESTFN + '.2'
self.unchange_file = os_helper.TESTFN + '.3'
self.addCleanup(os_helper.unlink, self.deleted_file)
self.addCleanup(os_helper.unlink, self.modified_file)
self.addCleanup(os_helper.unlink, self.unchange_file)
with open(self.deleted_file, 'w', encoding='utf-8') as source:
source.write('print("will be deleted")')
with open(self.modified_file, 'w', encoding='utf-8') as source:
source.write('print("will be modified")')
with open(self.unchange_file, 'w', encoding='utf-8') as source:
source.write('print("unchange")')

linecache.getlines(self.deleted_file)
linecache.getlines(self.modified_file)
linecache.getlines(self.unchange_file)

os.remove(self.deleted_file)
with open(self.modified_file, 'w', encoding='utf-8') as source:
source.write('print("was modified")')

def test_checkcache_with_oserror(self):
self.assertEqual(3, len(linecache.cache.keys()))
linecache.checkcache(self.deleted_file)
self.assertTrue(2 == len(linecache.cache.keys()) and
self.deleted_file not in linecache.cache.keys())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break this up into assertEqual and assertNotIn. See the assert methods here: https://docs.python.org/3/library/unittest.html

That way we get better error messages in the output when the assertion fails.


def test_checkcache_with_not_match_size_or_timestamp(self):
self.assertEqual(3, len(linecache.cache.keys()))
linecache.checkcache(self.modified_file)
self.assertTrue(2 == len(linecache.cache.keys()) and
self.modified_file not in linecache.cache.keys())

def test_checkcache_with_no_parameters(self):
self.assertEqual(3, len(linecache.cache.keys()))
linecache.checkcache()
self.assertTrue([self.unchange_file] == list(linecache.cache.keys()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertEqual



if __name__ == "__main__":
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy