|
| 1 | +""" |
| 2 | +Conftest for ComfyUI-vLLM-Omni tests. |
| 3 | +
|
| 4 | +This module sets up the test environment by: |
| 5 | +1. Adding the ComfyUI plugin to Python path |
| 6 | +2. Mocking comfy_api.input module (AudioInput, VideoInput) since comfyui is not installed |
| 7 | +3. Mocking comfy_extras.nodes_audio module |
| 8 | +""" |
| 9 | + |
1 | 10 | import os |
2 | 11 | import sys |
| 12 | +from typing import BinaryIO, TypedDict |
| 13 | +from unittest.mock import MagicMock |
| 14 | + |
| 15 | + |
| 16 | +def pytest_configure(config): |
| 17 | + """ |
| 18 | + Called after command line options have been parsed and before test collection. |
| 19 | + This is the right place to set up sys.path and mock modules. |
| 20 | + """ |
| 21 | + _setup_comfyui_test_environment() |
| 22 | + |
| 23 | + |
| 24 | +def _setup_comfyui_test_environment(): |
| 25 | + """Set up the test environment for ComfyUI plugin testing.""" |
| 26 | + # Add ComfyUI plugin path to allow importing comfyui_vllm_omni |
| 27 | + _COMFYUI_PLUGIN_PATH = os.path.abspath( |
| 28 | + os.path.join(os.path.dirname(__file__), "..", "..", "apps", "ComfyUI-vLLM-Omni") |
| 29 | + ) |
| 30 | + if _COMFYUI_PLUGIN_PATH not in sys.path: |
| 31 | + sys.path.insert(0, _COMFYUI_PLUGIN_PATH) |
| 32 | + |
| 33 | + # Now we can import torch (which is safe as it doesn't conflict) |
| 34 | + import torch |
| 35 | + |
| 36 | + # === Mock comfy_api.input module === |
| 37 | + class AudioInput(TypedDict): |
| 38 | + """Mock AudioInput TypedDict from comfy_api.input""" |
| 39 | + waveform: torch.Tensor # Shape: (B, C, T) |
| 40 | + sample_rate: int |
| 41 | + |
| 42 | + class VideoInput: |
| 43 | + """Mock VideoInput class from comfy_api.input""" |
| 44 | + def __init__(self, data: bytes = b"mock_video_data"): |
| 45 | + self._data = data |
| 46 | + |
| 47 | + def save_to(self, file: str | BinaryIO): |
| 48 | + """Save video data to file or file-like object.""" |
| 49 | + if isinstance(file, str): |
| 50 | + print("Called VideoInput.save_to with file path. Saving to a path is no-op in tests.") |
| 51 | + else: |
| 52 | + file.write(self._data) |
| 53 | + |
| 54 | + # Create mock comfy_api module hierarchy |
| 55 | + mock_comfy_api = MagicMock() |
| 56 | + mock_comfy_api_input = MagicMock() |
| 57 | + mock_comfy_api_input.AudioInput = AudioInput |
| 58 | + mock_comfy_api_input.VideoInput = VideoInput |
| 59 | + mock_comfy_api.input = mock_comfy_api_input |
| 60 | + |
| 61 | + # Create mock comfy_extras module |
| 62 | + mock_comfy_extras = MagicMock() |
| 63 | + mock_nodes_audio = MagicMock() |
| 64 | + |
| 65 | + def mock_load(_: str | BinaryIO): |
| 66 | + """Mock nodes_audio.load that returns a waveform tensor and sample rate.""" |
| 67 | + # Return mock audio data: (channels, samples) tensor and sample rate |
| 68 | + waveform = torch.zeros((1, 24000), dtype=torch.float32) |
| 69 | + sample_rate = 24000 |
| 70 | + return waveform, sample_rate |
| 71 | + |
| 72 | + mock_nodes_audio.load = mock_load |
| 73 | + mock_comfy_extras.nodes_audio = mock_nodes_audio |
| 74 | + |
| 75 | + # Install mock modules BEFORE importing any comfyui_vllm_omni code |
| 76 | + sys.modules["comfy_api"] = mock_comfy_api |
| 77 | + sys.modules["comfy_api.input"] = mock_comfy_api_input |
| 78 | + sys.modules["comfy_extras"] = mock_comfy_extras |
| 79 | + sys.modules["comfy_extras.nodes_audio"] = mock_nodes_audio |
| 80 | + |
3 | 81 |
|
4 | | -# Add the project root directory to Python path |
5 | | -# This allows the tests to import the project |
6 | | -sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "apps", "ComfyUI-vLLM-Omni"))) |
| 82 | +# Run setup immediately when conftest is imported (before pytest_configure for early loading) |
| 83 | +_setup_comfyui_test_environment() |
0 commit comments