diff --git a/UnityPy/environment.py b/UnityPy/environment.py index fa06ab91..9d30bd9f 100644 --- a/UnityPy/environment.py +++ b/UnityPy/environment.py @@ -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) diff --git a/tests/test_environment.py b/tests/test_environment.py new file mode 100644 index 00000000..cf99cb4a --- /dev/null +++ b/tests/test_environment.py @@ -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