diff --git a/owners.py b/owners.py index 563675a15..56311e40e 100644 --- a/owners.py +++ b/owners.py @@ -99,7 +99,7 @@ class Database(object): root: the path to the root of the Repository open: function callback to open a text file for reading os_path: module/object callback with fields for 'abspath', 'dirname', - 'exists', and 'join' + 'exists', 'join', and 'relpath' glob: function callback to list entries in a directory match a glob (i.e., glob.glob) """ @@ -292,7 +292,7 @@ class Database(object): include_path = path[2:] else: assert start.startswith(self.root) - start = self.os_path.dirname(start[len(self.root):]) + start = self.os_path.dirname(self.os_path.relpath(start, self.root)) include_path = self.os_path.join(start, path) owners_path = self.os_path.join(self.root, include_path) diff --git a/testing_support/filesystem_mock.py b/testing_support/filesystem_mock.py index b45ea8e37..7a44dabb3 100644 --- a/testing_support/filesystem_mock.py +++ b/testing_support/filesystem_mock.py @@ -86,9 +86,9 @@ class MockFileSystem(object): _RaiseNotFound(path) return self.files[path] - @staticmethod - def relpath(path, base): + def relpath(self, path, base): # This implementation is wrong in many ways; assert to check them for now. + if not base.endswith(self.sep): + base += self.sep assert path.startswith(base) - assert base.endswith('/') return path[len(base):] diff --git a/tests/owners_unittest.py b/tests/owners_unittest.py index 5cb177411..5cc5371e6 100755 --- a/tests/owners_unittest.py +++ b/tests/owners_unittest.py @@ -228,6 +228,15 @@ class OwnersDatabaseTest(_BaseTestCase): self.assert_files_not_covered_by(['content/garply/baz.cc'], [tom], ['content/garply/baz.cc']) + def test_file_include_relative_path_non_empty_root(self): + old_root = self.root + self.root = '/content' + self.assert_files_not_covered_by(['garply/foo.cc'], [peter], []) + self.assert_files_not_covered_by(['garply/bar.cc'], [darin], []) + self.assert_files_not_covered_by(['garply/baz.cc'], + [tom], ['garply/baz.cc']) + self.root = old_root + def test_file_include_per_file_absolute_path(self): self.files['/content/qux/OWNERS'] = owners_file(peter, lines=['per-file foo.*=file://content/baz/OWNERS'])