diff options
-rw-r--r-- | coverage/files.py | 12 | ||||
-rw-r--r-- | test/test_files.py | 34 |
2 files changed, 40 insertions, 6 deletions
diff --git a/coverage/files.py b/coverage/files.py index 28331a1..5690679 100644 --- a/coverage/files.py +++ b/coverage/files.py @@ -6,7 +6,8 @@ class FileLocator(object): """Understand how filenames work.""" def __init__(self): - self.relative_dir = self.abs_file(os.curdir) + # The absolute path to our current directory. + self.relative_dir = self.abs_file(os.curdir) + os.sep # Cache of results of calling the canonical_filename() method, to # avoid duplicating work. @@ -20,13 +21,12 @@ class FileLocator(object): """Return the relative form of `filename`. The filename will be relative to the current directory when the - FileLocator was constructed. + `FileLocator` was constructed. """ - common_prefix = os.path.commonprefix( - [filename, self.relative_dir + os.sep] - ) - return filename[len(common_prefix):] + if filename.startswith(self.relative_dir): + filename = filename.replace(self.relative_dir, "") + return filename def canonical_filename(self, filename): """Return a canonical filename for `filename`. diff --git a/test/test_files.py b/test/test_files.py new file mode 100644 index 0000000..4852d69 --- /dev/null +++ b/test/test_files.py @@ -0,0 +1,34 @@ +"""Tests for files.py""" + +import os, sys + +from coverage.files import FileLocator + +sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k +from coveragetest import CoverageTest + + +class FileLocatorTest(CoverageTest): + """Tests of `FileLocator`.""" + + def abs_path(self, p): + return os.path.join(os.getcwd(), os.path.normpath(p)) + + def test_simple(self): + self.make_file("hello.py", "#hello") + fl = FileLocator() + self.assertEqual(fl.relative_filename("hello.py"), "hello.py") + abs = self.abs_path("hello.py") + self.assertNotEqual(abs, "hello.py") + self.assertEqual(fl.relative_filename(abs), "hello.py") + + def test_peer_directories(self): + self.make_file("sub/proj1/file1.py", "file1") + self.make_file("sub/proj2/file2.py", "file2") + abs1 = self.abs_path("sub/proj1/file1.py") + abs2 = self.abs_path("sub/proj2/file2.py") + d = os.path.normpath("sub/proj1") + os.chdir(d) + fl = FileLocator() + self.assertEqual(fl.relative_filename(abs1), "file1.py") + self.assertEqual(fl.relative_filename(abs2), abs2) |