summaryrefslogtreecommitdiff
path: root/test/test_files.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_files.py')
-rw-r--r--test/test_files.py89
1 files changed, 84 insertions, 5 deletions
diff --git a/test/test_files.py b/test/test_files.py
index 4ffc6ca2..5692699c 100644
--- a/test/test_files.py
+++ b/test/test_files.py
@@ -3,8 +3,9 @@
import os, sys
from coverage.files import FileLocator, TreeMatcher, FnmatchMatcher
-from coverage.files import find_python_files
-from coverage.backward import set # pylint: disable-msg=W0622
+from coverage.files import PathAliases, find_python_files, abs_file
+from coverage.backward import set # pylint: disable=W0622
+from coverage.misc import CoverageException
sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
from coveragetest import CoverageTest
@@ -36,6 +37,17 @@ class FileLocatorTest(CoverageTest):
self.assertEqual(fl.relative_filename(a1), "file1.py")
self.assertEqual(fl.relative_filename(a2), a2)
+ def test_filepath_contains_absolute_prefix_twice(self):
+ # https://bitbucket.org/ned/coveragepy/issue/194
+ # Build a path that has two pieces matching the absolute path prefix.
+ # Technically, this test doesn't do that on Windows, but drive
+ # letters make that impractical to acheive.
+ fl = FileLocator()
+ d = abs_file(os.curdir)
+ trick = os.path.splitdrive(d)[1].lstrip(os.path.sep)
+ rel = os.path.join('sub', trick, 'file1.py')
+ self.assertEqual(fl.relative_filename(abs_file(rel)), rel)
+
class MatcherTest(CoverageTest):
"""Tests of file matchers."""
@@ -72,20 +84,87 @@ class MatcherTest(CoverageTest):
self.assertFalse(fnm.match(fl.canonical_filename(file5)))
+class PathAliasesTest(CoverageTest):
+ """Tests for coverage/files.py:PathAliases"""
+
+ def test_noop(self):
+ aliases = PathAliases()
+ self.assertEqual(aliases.map('/ned/home/a.py'), '/ned/home/a.py')
+
+ def test_nomatch(self):
+ aliases = PathAliases()
+ aliases.add('/home/*/src', './mysrc')
+ self.assertEqual(aliases.map('/home/foo/a.py'), '/home/foo/a.py')
+
+ def test_wildcard(self):
+ aliases = PathAliases()
+ aliases.add('/ned/home/*/src', './mysrc')
+ self.assertEqual(aliases.map('/ned/home/foo/src/a.py'), './mysrc/a.py')
+ aliases = PathAliases()
+ aliases.add('/ned/home/*/src/', './mysrc')
+ self.assertEqual(aliases.map('/ned/home/foo/src/a.py'), './mysrc/a.py')
+
+ def test_no_accidental_match(self):
+ aliases = PathAliases()
+ aliases.add('/home/*/src', './mysrc')
+ self.assertEqual(aliases.map('/home/foo/srcetc'), '/home/foo/srcetc')
+
+ def test_multiple_patterns(self):
+ aliases = PathAliases()
+ aliases.add('/home/*/src', './mysrc')
+ aliases.add('/lib/*/libsrc', './mylib')
+ self.assertEqual(aliases.map('/home/foo/src/a.py'), './mysrc/a.py')
+ self.assertEqual(aliases.map('/lib/foo/libsrc/a.py'), './mylib/a.py')
+
+ def test_cant_have_wildcard_at_end(self):
+ aliases = PathAliases()
+ self.assertRaisesRegexp(
+ CoverageException, "Pattern must not end with wildcards.",
+ aliases.add, "/ned/home/*", "fooey"
+ )
+ self.assertRaisesRegexp(
+ CoverageException, "Pattern must not end with wildcards.",
+ aliases.add, "/ned/home/*/", "fooey"
+ )
+ self.assertRaisesRegexp(
+ CoverageException, "Pattern must not end with wildcards.",
+ aliases.add, "/ned/home/*/*/", "fooey"
+ )
+
+ def test_no_accidental_munging(self):
+ aliases = PathAliases()
+ aliases.add(r'c:\Zoo\boo', 'src/')
+ aliases.add('/home/ned$', 'src/')
+ self.assertEqual(aliases.map(r'c:\Zoo\boo\foo.py'), 'src/foo.py')
+ self.assertEqual(aliases.map(r'/home/ned$/foo.py'), 'src/foo.py')
+
+ def test_paths_are_os_corrected(self):
+ aliases = PathAliases()
+ aliases.add('/home/ned/*/src', './mysrc')
+ aliases.add(r'c:\ned\src', './mysrc')
+ mapped = aliases.map(r'C:\Ned\src\sub\a.py')
+ self.assertEqual(mapped, './mysrc/sub/a.py')
+
+ aliases = PathAliases()
+ aliases.add('/home/ned/*/src', r'.\mysrc')
+ aliases.add(r'c:\ned\src', r'.\mysrc')
+ mapped = aliases.map(r'/home/ned/foo/src/sub/a.py')
+ self.assertEqual(mapped, r'.\mysrc\sub\a.py')
+
+
class FindPythonFilesTest(CoverageTest):
"""Tests of `find_python_files`."""
def test_find_python_files(self):
self.make_file("sub/a.py")
self.make_file("sub/b.py")
- self.make_file("sub/__init__.py")
self.make_file("sub/x.c") # nope: not .py
self.make_file("sub/ssub/__init__.py")
self.make_file("sub/ssub/s.py")
+ self.make_file("sub/ssub/~s.py") # nope: editor effluvia
self.make_file("sub/lab/exp.py") # nope: no __init__.py
py_files = set(find_python_files("sub"))
self.assert_same_files(py_files, [
- "sub/__init__.py", "sub/a.py", "sub/b.py",
+ "sub/a.py", "sub/b.py",
"sub/ssub/__init__.py", "sub/ssub/s.py",
])
-