Skip to content

Commit

Permalink
fix(tar): #2 Use normpath on names when testing for top level folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Cobertos committed Jul 18, 2019
1 parent 737fb36 commit 9902546
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Binary file added tests/testLeadingDots.unitypackage
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/test_testPackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ def test_packageExtract(self):
print(f"Extracting to {tmp}...")
extractPackage("./tests/test.unitypackage", outputPath=tmp)

#assert
self.assertTrue(os.path.isdir(tmp))
self.assertTrue(os.path.isdir(f"{tmp}/Assets"))
self.assertTrue(os.path.isfile(f"{tmp}/Assets/test.txt"))
self.assertEqual(open(f"{tmp}/Assets/test.txt").read(), "testing")

def test_packageExtractWithLeadingDots(self):
'''should be able to extract a unity package that contains ./ in every path in the tar'''
#arrange
with tempfile.TemporaryDirectory() as tmp:
#testLeadingDots.unitypackage - Same as test.unitypackage but archived with `tar -zrf archive.unitypackage .`
#to get the specific `./` before every path

#act
print(f"Extracting to {tmp}...")
extractPackage("./tests/testLeadingDots.unitypackage", outputPath=tmp)

#assert
self.assertTrue(os.path.isdir(tmp))
self.assertTrue(os.path.isdir(f"{tmp}/Assets"))
Expand Down
7 changes: 6 additions & 1 deletion unitypackage_extractor/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ def extractPackage(packagePath, outputPath=""):
"""
with tarfile.open(name=packagePath) as upkg:
for name in upkg.getnames():
if re.search(r"[/\\]", name): #Only the top level files of the tar
#.getnames() iterates over every file and folder, providing strings
# as directory entries

#Only top level files (use normpath to remove any leading ./ or other
#redundant path stuff
if re.search(r"[/\\]", os.path.normpath(name)):
continue

try:
Expand Down

0 comments on commit 9902546

Please sign in to comment.