From 17ba039f287bd07a3c5d88dc7644f029ce6d4030 Mon Sep 17 00:00:00 2001 From: boring877 Date: Tue, 6 Oct 2026 16:03:15 +0000 Subject: [PATCH] fix(environment): raise FileNotFoundError instead of silently loading nothing UnityPy.load('missing.unity3d') currently returns an empty Environment with no error, hiding typos and wrong paths behind env.objects == []. There is even a commented-out raise for this exact case in load_file. This restores loud failures for top-level paths while keeping load_file's silent-skip for missing dependencies (unshipped externals are normal and are resolved through the same code path). --- UnityPy/environment.py | 5 +++++ tests/test_environment.py | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/test_environment.py 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