Skip to content

Commit

Permalink
path_utilities: bugfix: GetContainingPackage support non-existent files
Browse files Browse the repository at this point in the history
This fixes a bug that was introduced in commit
85b899a that resulted in
GetContainingPackage no longer supporting non-existent files. This
commit fixes that bug and adds a unit test to ensure that it does not
happen again.
  • Loading branch information
Javagedes committed Aug 4, 2023
1 parent 3925690 commit 199c0a2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion edk2toollib/uefi/edk2/path_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def GetContainingPackage(self, InputPath: str) -> str:
else:
dirpath = InputPath.parent

while not path_root.samefile(dirpath):
while path_root != dirpath:
if dirpath.exists():
for f in dirpath.iterdir():
if f.suffix.lower() =='.dec':
Expand Down
44 changes: 44 additions & 0 deletions tests.unit/test_path_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,50 @@ def test_get_containing_package_ws_abs_different_case(self):
p = os.path.join(folder_pp1_abs, "testfile.c")
self.assertIsNone(pathobj.GetContainingPackage(p), folder_pp_rel)

def test_get_containing_package_with_nonexistent_path(self):
'''Test basic usage of GetContainingPackage when the file path does not exist.
File layout:
root/ <-- current working directory (self.tmp)
folder_ws/ <-- workspace root
folder_pp/ <-- packages path
PPTestPkg/ <-- A edk2 package
PPTestPkg.DEC
module1/
module1.INF
module2/
module2.INF
X64/
TestFile.c
WSTestPkg/ <-- A edk2 package
WSTestPkg.dec
module1/
module1.inf
module2/
module2.inf
X64/
TestFile.c
'''
ws_rel = "folder_ws"
ws_abs = os.path.join(self.tmp, ws_rel)
wsi_abs = os.path.join(self.tmp, ws_rel)
os.mkdir(ws_abs)
folder_pp_rel = "pp1"
folder_pp1_abs = os.path.join(ws_abs, folder_pp_rel)
os.mkdir(folder_pp1_abs)
ws_p_name = "WSTestPkg"
ws_pkg_abs = self._make_edk2_package_helper(ws_abs, ws_p_name)
pp_p_name = "PPTestPkg"
pp_pkg_abs = self._make_edk2_package_helper(folder_pp1_abs, pp_p_name, extension_case_lower=False)
pathobj = Edk2Path(wsi_abs, [folder_pp1_abs])

p = os.path.join(pp_pkg_abs, "subfolder", "testfile2.c")
self.assertEqual(pathobj.GetContainingPackage(p), pp_p_name)

p = os.path.join(ws_pkg_abs, "subfolder", "testfile2.c")
self.assertEqual(pathobj.GetContainingPackage(p), ws_p_name)

def test_get_containing_modules_with_relative_path(self):
"""Test that a relative path raises an exception.
Expand Down

0 comments on commit 199c0a2

Please sign in to comment.