ComfyUI-Manager/tests/common/pip_util/test_platform_conditions.py
Dr.Lt.Data 2866193baf ● feat: Draft pip package policy management system (not yet integrated)
Add comprehensive pip dependency conflict resolution framework as draft implementation. This is self-contained and does not affect existing
ComfyUI Manager functionality.

Key components:
- pip_util.py with PipBatch class for policy-driven package management
- Lazy-loaded policy system supporting base + user overrides
- Multi-stage policy execution (uninstall → apply_first_match → apply_all_matches → restore)
- Conditional policies based on platform, installed packages, and ComfyUI version
- Comprehensive test suite covering edge cases, workflows, and platform scenarios
- Design and implementation documentation

Policy capabilities (draft):
- Package replacement (e.g., PIL → Pillow, opencv-python → opencv-contrib-python)
- Version pinning to prevent dependency conflicts
- Dependency protection during installations
- Platform-specific handling (Linux/Windows, GPU detection)
- Pre-removal and post-restoration workflows

Testing infrastructure:
- Pytest-based test suite with isolated environments
- Dependency analysis tools for conflict detection
- Coverage for policy priority, edge cases, and environment recovery

Status: Draft implementation complete, integration with manager workflows pending.
2025-10-04 08:55:59 +09:00

140 lines
3.8 KiB
Python

"""
Test platform-specific conditions (Priority 2)
Tests OS and GPU detection for conditional policies
"""
import json
import subprocess
from pathlib import Path
import pytest
@pytest.fixture
def platform_policy(temp_policy_dir):
"""Create policy with platform conditions"""
policy_content = {
"onnxruntime": {
"apply_first_match": [
{
"condition": {
"type": "platform",
"os": "linux",
"has_gpu": True
},
"type": "replace",
"replacement": "onnxruntime-gpu"
}
]
}
}
policy_file = temp_policy_dir / "pip-policy.json"
policy_file.write_text(json.dumps(policy_content, indent=2))
return policy_file
@pytest.fixture
def mock_platform_subprocess(monkeypatch):
"""Mock subprocess for platform test"""
call_sequence = []
installed_packages = {}
def mock_run(cmd, **kwargs):
call_sequence.append(cmd)
# pip freeze
if "freeze" in cmd:
output = "\n".join([f"{pkg}=={ver}" for pkg, ver in installed_packages.items()])
return subprocess.CompletedProcess(cmd, 0, output, "")
# pip install
if "install" in cmd:
if "onnxruntime-gpu" in cmd:
installed_packages["onnxruntime-gpu"] = "1.0.0"
elif "onnxruntime" in cmd:
installed_packages["onnxruntime"] = "1.0.0"
return subprocess.CompletedProcess(cmd, 0, "", "")
return subprocess.CompletedProcess(cmd, 0, "", "")
monkeypatch.setattr("subprocess.run", mock_run)
return call_sequence, installed_packages
@pytest.mark.integration
def test_linux_gpu_uses_gpu_package(
platform_policy,
mock_manager_util,
mock_context,
mock_platform_subprocess,
mock_platform_linux,
mock_torch_cuda_available
):
"""
Test GPU-specific package on Linux + GPU
Priority: 2 (Important)
Purpose:
Verify that platform-conditional policies correctly detect
Linux + GPU and install the appropriate package variant.
"""
import sys
# Path setup handled by conftest.py
from comfyui_manager.common.pip_util import PipBatch
call_sequence, installed_packages = mock_platform_subprocess
with PipBatch() as batch:
result = batch.install("onnxruntime")
# Verify installation succeeded
assert result is True
# Verify GPU version was installed
install_calls = [cmd for cmd in call_sequence if "install" in cmd]
assert any("onnxruntime-gpu" in str(cmd) for cmd in install_calls)
assert "onnxruntime-gpu" in installed_packages
@pytest.mark.integration
def test_windows_no_gpu_uses_cpu_package(
platform_policy,
mock_manager_util,
mock_context,
mock_platform_subprocess,
mock_platform_windows,
mock_torch_cuda_unavailable
):
"""
Test CPU package on Windows + No GPU
Priority: 2 (Important)
Purpose:
Verify that when platform conditions are not met,
the original package is installed without replacement.
"""
import sys
# Path setup handled by conftest.py
from comfyui_manager.common.pip_util import PipBatch
call_sequence, installed_packages = mock_platform_subprocess
with PipBatch() as batch:
result = batch.install("onnxruntime")
# Verify installation succeeded
assert result is True
# Verify CPU version was installed (no GPU replacement)
install_calls = [cmd for cmd in call_sequence if "install" in cmd]
assert any("onnxruntime" in str(cmd) for cmd in install_calls)
assert "onnxruntime-gpu" not in str(call_sequence)
assert "onnxruntime" in installed_packages
assert "onnxruntime-gpu" not in installed_packages