Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UnityPy/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def __init__(self, *args: FileSourceType, fs: Optional[AbstractFileSystem] = Non
elif self.fs.isdir(arg):
self.path = arg
self.load_folder(arg)
else:
# a path that is neither an existing file nor a directory should fail
# loudly; silently skipping it hides typos behind an empty Environment
if self.load_file(file=arg) is None:
raise FileNotFoundError(f"{arg}: file does not exist")
else:
self.load_file(file=arg)

Expand Down
26 changes: 26 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

import UnityPy


def test_load_missing_file_raises():
# silently returning an empty Environment hides typos and wrong paths
with pytest.raises(FileNotFoundError):
UnityPy.load("definitely_missing_file.unity3d")


def test_load_missing_dependency_stays_silent():
# missing dependencies (e.g. unshipped externals) are normal and must be skipped
env = UnityPy.Environment()
assert env.load_file("no_such_dependency.assets", is_dependency=True) is None


def test_load_folder_still_works(tmp_path):
(tmp_path / "dummy.asset").write_bytes(b"not a real unity file, just needs to exist")
env = UnityPy.load(str(tmp_path))
assert len(env.files) == 1


def test_load_bytes_still_works():
env = UnityPy.load(b"UnityFS\x00")
assert len(env.files) == 1