summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephan Richter <stephan.richter@gmail.com>2019-01-27 14:37:40 -0500
committerNed Batchelder <ned@nedbatchelder.com>2019-01-28 21:37:24 -0500
commit1655796d48cb67d7247b41a09fe5c2463b32d47a (patch)
tree44f1ffc1d491ed4161af7af0dcfdb2e4501150ec
parent21d3b37e290796312750f207442b33afbaab14de (diff)
downloadpython-coveragepy-git-1655796d48cb67d7247b41a09fe5c2463b32d47a.tar.gz
Make sure that the cache is properly filled. (25x speedup on our system that has a large sys.path.)
It is always a bad idea to reassign the cachekey during the computation.
-rw-r--r--coverage/files.py5
-rw-r--r--tests/test_files.py13
2 files changed, 16 insertions, 2 deletions
diff --git a/coverage/files.py b/coverage/files.py
index b328f653..d9495912 100644
--- a/coverage/files.py
+++ b/coverage/files.py
@@ -59,6 +59,7 @@ def canonical_filename(filename):
"""
if filename not in CANONICAL_FILENAME_CACHE:
+ cf = filename
if not os.path.isabs(filename):
for path in [os.curdir] + sys.path:
if path is None:
@@ -69,9 +70,9 @@ def canonical_filename(filename):
except UnicodeError:
exists = False
if exists:
- filename = f
+ cf = f
break
- cf = abs_file(filename)
+ cf = abs_file(cf)
CANONICAL_FILENAME_CACHE[filename] = cf
return CANONICAL_FILENAME_CACHE[filename]
diff --git a/tests/test_files.py b/tests/test_files.py
index b4490ea6..87b85431 100644
--- a/tests/test_files.py
+++ b/tests/test_files.py
@@ -57,6 +57,19 @@ class FilesTest(CoverageTest):
rel = os.path.join('sub', trick, 'file1.py')
self.assertEqual(files.relative_filename(abs_file(rel)), rel)
+ def test_canonical_filename_ensure_cache_hit(self):
+ self.make_file("sub/proj1/file1.py")
+ d = os.path.normpath("sub/proj1")
+ self.chdir(d)
+ files.set_relative_directory()
+ canonical_path = files.canonical_filename('sub/proj1/file1.py')
+ self.assertEqual(canonical_path, self.abs_path('file1.py'))
+ # After the filename has been converted, it should be in the cache.
+ self.assertIn('sub/proj1/file1.py', files.CANONICAL_FILENAME_CACHE)
+ self.assertEqual(
+ files.canonical_filename('sub/proj1/file1.py'),
+ self.abs_path('file1.py'))
+
@pytest.mark.parametrize("original, flat", [
(u"a/b/c.py", u"a_b_c_py"),