Skip to content

Commit

Permalink
Find runfiles in directories that are themselves runfiles (Python)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Feb 3, 2022
1 parent daabf87 commit 74f3bd2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
13 changes: 12 additions & 1 deletion tools/python/runfiles/runfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,18 @@ def __init__(self, path):
self._runfiles = _ManifestBased._LoadRunfiles(path)

def RlocationChecked(self, path):
return self._runfiles.get(path)
exact_match = self._runfiles.get(path)
if exact_match:
return exact_match
prefix_end = len(path)
while True:
prefix_end = path.rfind("/", 0, prefix_end - 1)
if prefix_end == -1:
return None
prefix_match = self._runfiles.get(path[0:prefix_end])
if prefix_match:
suffix_path_segments = path[prefix_end + 1:].split("/")
return os.path.join(prefix_match, *suffix_path_segments)

@staticmethod
def _LoadRunfiles(path):
Expand Down
13 changes: 11 additions & 2 deletions tools/python/runfiles/runfiles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ def testFailsToCreateAnyRunfilesBecauseEnvvarsAreNotDefined(self):

def testManifestBasedRlocation(self):
with _MockFile(contents=[
"Foo/runfile1", "Foo/runfile2 C:/Actual Path\\runfile2",
"Foo/Bar/runfile3 D:\\the path\\run file 3.txt"
"Foo/runfile1", "Foo/runfile2 C:/Actual Path\\runfile2",
"Foo/Bar/runfile3 D:\\the path\\run file 3.txt",
"Foo/Bar/Dir E:\\Actual Path\\Directory",
]) as mf:
r = runfiles.CreateManifestBased(mf.Path())
self.assertEqual(r.Rlocation("Foo/runfile1"), "Foo/runfile1")
Expand All @@ -150,9 +151,17 @@ def testManifestBasedRlocation(self):
r.Rlocation("Foo/Bar/runfile3"), "D:\\the path\\run file 3.txt")
self.assertIsNone(r.Rlocation("unknown"))
if RunfilesTest.IsWindows():
self.assertEqual(r.Rlocation("Foo/Bar/Dir/runfile4"),
"E:\\Actual Path\\Directory\\runfile4")
self.assertEqual(r.Rlocation("Foo/Bar/Dir/Deeply/Nested/runfile4"),
"E:\\Actual Path\\Directory\\Deeply\\Nested\\runfile4")
self.assertEqual(r.Rlocation("c:/foo"), "c:/foo")
self.assertEqual(r.Rlocation("c:\\foo"), "c:\\foo")
else:
self.assertEqual(r.Rlocation("Foo/Bar/Dir/runfile4"),
"E:\\Actual Path\\Directory/runfile4")
self.assertEqual(r.Rlocation("Foo/Bar/Dir/Deeply/Nested/runfile4"),
"E:\\Actual Path\\Directory/Deeply/Nested/runfile4")
self.assertEqual(r.Rlocation("/foo"), "/foo")

def testDirectoryBasedRlocation(self):
Expand Down

0 comments on commit 74f3bd2

Please sign in to comment.