ComfyUI/tests/unit/fsspec_tests/test_package_filesystem.py
doctorpangloss 67f9d3e693 Improved custom nodes compatibility
- Fixed and verifies compatibility with the following nodes:
   ComfyUI-Manager==3.0.1
   ComfyUI_LayerStyle==1.0.90
   ComfyUI-Easy-Use==1.3.4 (required fix)
   ComfyUI-KJNodes==1.1.7 (required mitigation)
   ComfyUI_Custom_Nodes_AlekPet==1.0.88
   LanPaint==1.4.1
   Comfyui-Simple-Json-Node==1.1.0
 - Add support to referencing files in packages.
2025-10-23 16:28:10 -07:00

121 lines
4.4 KiB
Python

import pytest
import fsspec
from comfy.component_model import package_filesystem
import os
# Ensure the filesystem is registered once for all tests
@pytest.fixture(scope="module", autouse=True)
def setup_package_filesystem():
if "pkg" not in fsspec.available_protocols():
fsspec.register_implementation(
package_filesystem.PkgResourcesFileSystem.protocol,
package_filesystem.PkgResourcesFileSystem,
)
# Yield to allow tests to run, then teardown if necessary (though not needed here)
yield
@pytest.fixture
def pkg_fs():
return fsspec.filesystem("pkg")
def test_open_file_in_package(pkg_fs):
"""Test opening a file directly within a package."""
with pkg_fs.open("pkg://tests.unit.fsspec_tests.files/b.txt", "rb") as f:
content = f.read()
assert content == b"OK"
def test_open_file_in_text_mode(pkg_fs):
"""Test opening a file in text mode."""
with pkg_fs.open("pkg://tests.unit.fsspec_tests.files/b.txt", "r") as f:
content = f.read()
assert content == "OK"
def test_open_file_in_subdir(pkg_fs):
"""Test opening a file in a subdirectory of a package."""
with pkg_fs.open("pkg://tests.unit.fsspec_tests.files/subdir/a.txt", "rb") as f:
content = f.read()
assert content == b"OK"
def test_file_not_found(pkg_fs):
"""Test that opening a non-existent file raises FileNotFoundError."""
with pytest.raises(FileNotFoundError):
pkg_fs.open("pkg://tests.unit.fsspec_tests.files/nonexistent.txt")
def test_package_not_found(pkg_fs):
"""Test that using a non-existent package raises FileNotFoundError."""
with pytest.raises(FileNotFoundError):
pkg_fs.open("pkg://non.existent.package/resource.txt")
def test_ls_package_root(pkg_fs):
"""Test listing the contents of a package."""
contents = pkg_fs.ls("pkg://tests.unit.fsspec_tests.files", detail=False)
expected_items = {
"pkg://tests.unit.fsspec_tests.files/b.txt",
"pkg://tests.unit.fsspec_tests.files/subdir",
"pkg://tests.unit.fsspec_tests.files/__init__.py",
}
# Use a subset assertion to be resilient to __pycache__
normalized_contents = {os.path.normpath(p.split('@')[0]) for p in contents}
normalized_expected = {os.path.normpath(p) for p in expected_items}
assert normalized_expected.issubset(normalized_contents)
def test_ls_subdir(pkg_fs):
"""Test listing the contents of a subdirectory."""
contents = pkg_fs.ls("pkg://tests.unit.fsspec_tests.files/subdir", detail=False)
normalized_contents = [os.path.normpath(p.split('@')[0]) for p in contents]
assert os.path.normpath("pkg://tests.unit.fsspec_tests.files/subdir/a.txt") in normalized_contents
def test_info_file(pkg_fs):
"""Test getting info for a file."""
info = pkg_fs.info("pkg://tests.unit.fsspec_tests.files/b.txt")
assert info["type"] == "file"
assert info["name"] == "pkg://tests.unit.fsspec_tests.files/b.txt"
assert info["size"] == 2
def test_info_directory(pkg_fs):
"""Test getting info for a directory."""
info = pkg_fs.info("pkg://tests.unit.fsspec_tests.files/subdir")
assert info["type"] == "directory"
assert info["name"] == "pkg://tests.unit.fsspec_tests.files/subdir"
# Directories typically don't have a size in this context, or it might be 0
assert "size" in info # Ensure size key exists
assert info["size"] is None or info["size"] == 0
def test_load_font_with_upath(pkg_fs):
"""Test that a font can be loaded from the pkg filesystem using UPath."""
from upath import UPath
from PIL import ImageFont, features
# This test requires Pillow with FreeType support
if not features.check("freetype2"):
pytest.skip("Pillow FreeType support not available")
# UPath will use the registered fsspec filesystem for "pkg"
font_path = UPath("pkg://comfy.fonts/Tiny5-Regular.ttf")
# Try to load the font by passing the UPath object directly.
# This is not expected to work for non-local paths unless the consuming
# library (Pillow) has specific support for fsspec/upath.
try:
font = ImageFont.truetype(font_path, 10)
except (TypeError, AttributeError):
# If passing the path directly fails, fall back to opening the file
# and passing the file-like object, which is the standard way.
with font_path.open("rb") as f:
font = ImageFont.truetype(f, 10)
assert font is not None
assert isinstance(font, ImageFont.FreeTypeFont)