|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
5 | | -from comfy_cli.registry.config_parser import extract_node_configuration |
| 5 | +from comfy_cli.registry.config_parser import ( |
| 6 | + extract_node_configuration, |
| 7 | + validate_and_extract_accelerator_classifiers, |
| 8 | + validate_and_extract_os_classifiers, |
| 9 | + validate_version, |
| 10 | +) |
6 | 11 | from comfy_cli.registry.types import ( |
7 | 12 | License, |
8 | 13 | Model, |
@@ -127,3 +132,198 @@ def test_extract_license_incorrect_format(): |
127 | 132 | assert result is not None, "Expected PyProjectConfig, got None" |
128 | 133 | assert isinstance(result, PyProjectConfig) |
129 | 134 | assert result.project.license == License(text="MIT") |
| 135 | + |
| 136 | + |
| 137 | +def test_extract_node_configuration_with_os_classifiers(): |
| 138 | + mock_data = { |
| 139 | + "project": { |
| 140 | + "classifiers": [ |
| 141 | + "Operating System :: OS Independent", |
| 142 | + "Operating System :: Microsoft :: Windows", |
| 143 | + "Programming Language :: Python :: 3", |
| 144 | + "Topic :: Software Development", |
| 145 | + ] |
| 146 | + } |
| 147 | + } |
| 148 | + with ( |
| 149 | + patch("os.path.isfile", return_value=True), |
| 150 | + patch("builtins.open", mock_open()), |
| 151 | + patch("tomlkit.load", return_value=mock_data), |
| 152 | + ): |
| 153 | + result = extract_node_configuration("fake_path.toml") |
| 154 | + |
| 155 | + assert result is not None |
| 156 | + assert len(result.project.supported_os) == 2 |
| 157 | + assert "OS Independent" in result.project.supported_os |
| 158 | + assert "Microsoft :: Windows" in result.project.supported_os |
| 159 | + |
| 160 | + |
| 161 | +def test_extract_node_configuration_with_accelerator_classifiers(): |
| 162 | + mock_data = { |
| 163 | + "project": { |
| 164 | + "classifiers": [ |
| 165 | + "Environment :: GPU :: NVIDIA CUDA", |
| 166 | + "Environment :: GPU :: AMD ROCm", |
| 167 | + "Environment :: GPU :: Intel Arc", |
| 168 | + "Environment :: NPU :: Huawei Ascend", |
| 169 | + "Environment :: GPU :: Apple Metal", |
| 170 | + "Programming Language :: Python :: 3", |
| 171 | + "Topic :: Software Development", |
| 172 | + ] |
| 173 | + } |
| 174 | + } |
| 175 | + with ( |
| 176 | + patch("os.path.isfile", return_value=True), |
| 177 | + patch("builtins.open", mock_open()), |
| 178 | + patch("tomlkit.load", return_value=mock_data), |
| 179 | + ): |
| 180 | + result = extract_node_configuration("fake_path.toml") |
| 181 | + |
| 182 | + assert result is not None |
| 183 | + assert len(result.project.supported_accelerators) == 5 |
| 184 | + assert "GPU :: NVIDIA CUDA" in result.project.supported_accelerators |
| 185 | + assert "GPU :: AMD ROCm" in result.project.supported_accelerators |
| 186 | + assert "GPU :: Intel Arc" in result.project.supported_accelerators |
| 187 | + assert "NPU :: Huawei Ascend" in result.project.supported_accelerators |
| 188 | + assert "GPU :: Apple Metal" in result.project.supported_accelerators |
| 189 | + |
| 190 | + |
| 191 | +def test_extract_node_configuration_with_comfyui_version(): |
| 192 | + mock_data = {"project": {"dependencies": ["packge1>=2.0.0", "comfyui-frontend-package>=1.2.3", "package2>=1.0.0"]}} |
| 193 | + with ( |
| 194 | + patch("os.path.isfile", return_value=True), |
| 195 | + patch("builtins.open", mock_open()), |
| 196 | + patch("tomlkit.load", return_value=mock_data), |
| 197 | + ): |
| 198 | + result = extract_node_configuration("fake_path.toml") |
| 199 | + |
| 200 | + assert result is not None |
| 201 | + assert result.project.supported_comfyui_frontend_version == ">=1.2.3" |
| 202 | + assert len(result.project.dependencies) == 2 |
| 203 | + assert "comfyui-frontend-package>=1.2.3" not in result.project.dependencies |
| 204 | + assert "packge1>=2.0.0" in result.project.dependencies |
| 205 | + assert "package2>=1.0.0" in result.project.dependencies |
| 206 | + |
| 207 | + |
| 208 | +def test_extract_node_configuration_with_requires_comfyui(): |
| 209 | + mock_data = {"project": {}, "tool": {"comfy": {"requires-comfyui": "2.0.0"}}} |
| 210 | + with ( |
| 211 | + patch("os.path.isfile", return_value=True), |
| 212 | + patch("builtins.open", mock_open()), |
| 213 | + patch("tomlkit.load", return_value=mock_data), |
| 214 | + ): |
| 215 | + result = extract_node_configuration("fake_path.toml") |
| 216 | + |
| 217 | + assert result is not None |
| 218 | + assert result.project.supported_comfyui_version == "2.0.0" |
| 219 | + |
| 220 | + |
| 221 | +def test_validate_and_extract_os_classifiers_valid(): |
| 222 | + """Test OS validation with valid classifiers.""" |
| 223 | + classifiers = [ |
| 224 | + "Operating System :: Microsoft :: Windows", |
| 225 | + "Operating System :: POSIX :: Linux", |
| 226 | + "Operating System :: MacOS", |
| 227 | + "Operating System :: OS Independent", |
| 228 | + "Programming Language :: Python :: 3", |
| 229 | + ] |
| 230 | + result = validate_and_extract_os_classifiers(classifiers) |
| 231 | + expected = ["Microsoft :: Windows", "POSIX :: Linux", "MacOS", "OS Independent"] |
| 232 | + assert result == expected |
| 233 | + |
| 234 | + |
| 235 | +@patch("typer.echo") |
| 236 | +def test_validate_and_extract_os_classifiers_invalid(mock_echo): |
| 237 | + """Test OS validation with invalid classifiers.""" |
| 238 | + classifiers = [ |
| 239 | + "Operating System :: Microsoft :: Windows", |
| 240 | + "Operating System :: Linux", # Invalid - should be "POSIX :: Linux" |
| 241 | + "Programming Language :: Python :: 3", |
| 242 | + ] |
| 243 | + result = validate_and_extract_os_classifiers(classifiers) |
| 244 | + assert result == [] |
| 245 | + mock_echo.assert_called_once() |
| 246 | + assert "Invalid Operating System classifier found" in mock_echo.call_args[0][0] |
| 247 | + |
| 248 | + |
| 249 | +def test_validate_and_extract_accelerator_classifiers_valid(): |
| 250 | + """Test accelerator validation with valid classifiers.""" |
| 251 | + classifiers = [ |
| 252 | + "Environment :: GPU :: NVIDIA CUDA", |
| 253 | + "Environment :: GPU :: AMD ROCm", |
| 254 | + "Environment :: GPU :: Intel Arc", |
| 255 | + "Environment :: NPU :: Huawei Ascend", |
| 256 | + "Environment :: GPU :: Apple Metal", |
| 257 | + "Programming Language :: Python :: 3", |
| 258 | + ] |
| 259 | + result = validate_and_extract_accelerator_classifiers(classifiers) |
| 260 | + expected = [ |
| 261 | + "GPU :: NVIDIA CUDA", |
| 262 | + "GPU :: AMD ROCm", |
| 263 | + "GPU :: Intel Arc", |
| 264 | + "NPU :: Huawei Ascend", |
| 265 | + "GPU :: Apple Metal", |
| 266 | + ] |
| 267 | + assert result == expected |
| 268 | + |
| 269 | + |
| 270 | +@patch("typer.echo") |
| 271 | +def test_validate_and_extract_accelerator_classifiers_invalid(mock_echo): |
| 272 | + """Test accelerator validation with invalid classifiers.""" |
| 273 | + classifiers = [ |
| 274 | + "Environment :: GPU :: NVIDIA CUDA", |
| 275 | + "Environment :: GPU :: Invalid GPU", # Invalid |
| 276 | + "Programming Language :: Python :: 3", |
| 277 | + ] |
| 278 | + result = validate_and_extract_accelerator_classifiers(classifiers) |
| 279 | + assert result == [] |
| 280 | + mock_echo.assert_called_once() |
| 281 | + assert "Invalid Environment classifier found" in mock_echo.call_args[0][0] |
| 282 | + |
| 283 | + |
| 284 | +def test_validate_version_valid(): |
| 285 | + """Test version validation with valid versions.""" |
| 286 | + valid_versions = [ |
| 287 | + "1.1.1", |
| 288 | + ">=1.0.0", |
| 289 | + "==2.1.0-beta", |
| 290 | + "1.5.2", |
| 291 | + "~=3.0.0", |
| 292 | + "!=1.2.3", |
| 293 | + ">2.0.0", |
| 294 | + "<3.0.0", |
| 295 | + "<=4.0.0", |
| 296 | + "<>1.0.0", |
| 297 | + "=1.0.0", |
| 298 | + "1.0.0-alpha1", |
| 299 | + ">=1.0.0,<2.0.0", |
| 300 | + "==1.2.3,!=1.2.4", |
| 301 | + ">=1.0.0,<=2.0.0,!=1.5.0", |
| 302 | + "1.0.0,2.0.0", |
| 303 | + ">1.0.0,<2.0.0,!=1.5.0-beta", |
| 304 | + ] |
| 305 | + |
| 306 | + for version in valid_versions: |
| 307 | + result = validate_version(version, "test_field") |
| 308 | + assert result == version, f"Version {version} should be valid" |
| 309 | + |
| 310 | + |
| 311 | +@patch("typer.echo") |
| 312 | +def test_validate_version_invalid(mock_echo): |
| 313 | + """Test version validation with invalid versions.""" |
| 314 | + invalid_versions = [ |
| 315 | + "1.0", # Missing patch version |
| 316 | + ">=abc", # Invalid version format |
| 317 | + "invalid-version", # Completely invalid |
| 318 | + "1.0.0.0", # Too many version parts |
| 319 | + ">>1.0.0", # Invalid operator |
| 320 | + ">=1.0.0,invalid", |
| 321 | + "1.0,2.0.0", |
| 322 | + ">=1.0.0,>=abc", |
| 323 | + ] |
| 324 | + |
| 325 | + for version in invalid_versions: |
| 326 | + result = validate_version(version, "test_field") |
| 327 | + assert result == "", f"Version {version} should be invalid" |
| 328 | + |
| 329 | + assert mock_echo.call_count == len(invalid_versions) |
0 commit comments