|
1 | 1 | import importlib.util |
| 2 | +import json |
| 3 | +import os |
2 | 4 | import subprocess |
3 | 5 | import sys |
4 | 6 | import tempfile |
5 | 7 | import unittest |
6 | 8 | from pathlib import Path |
| 9 | +from unittest import mock |
7 | 10 |
|
8 | 11 |
|
9 | 12 | ROOT = Path(__file__).resolve().parents[1] |
@@ -195,6 +198,154 @@ def test_build_process_output_details_keeps_partial_timeout_logs(self): |
195 | 198 |
|
196 | 199 | self.assertEqual(details, {"stdout": "line one\nline two", "stderr": "warning"}) |
197 | 200 |
|
| 201 | + def test_parse_simple_yaml_handles_comments_quotes_and_whitespace(self): |
| 202 | + module = load_validator_module() |
| 203 | + |
| 204 | + with tempfile.NamedTemporaryFile("w", suffix=".yml", delete=False) as handle: |
| 205 | + handle.write( |
| 206 | + """ |
| 207 | + # leading comment |
| 208 | +
|
| 209 | + key1: value1 # trailing comment |
| 210 | + key2: \" spaced value \" |
| 211 | + key3: 'another value' |
| 212 | + key4: value-with-#-hash |
| 213 | + """ |
| 214 | + ) |
| 215 | + metadata_path = Path(handle.name) |
| 216 | + |
| 217 | + try: |
| 218 | + parsed = module._parse_simple_yaml(metadata_path) |
| 219 | + finally: |
| 220 | + os.remove(metadata_path) |
| 221 | + |
| 222 | + self.assertEqual(parsed["key1"], "value1") |
| 223 | + self.assertEqual(parsed["key2"], " spaced value ") |
| 224 | + self.assertEqual(parsed["key3"], "another value") |
| 225 | + self.assertEqual(parsed["key4"], "value-with-#-hash") |
| 226 | + |
| 227 | + def test_load_metadata_uses_yaml_safe_load_when_available(self): |
| 228 | + module = load_validator_module() |
| 229 | + |
| 230 | + with tempfile.NamedTemporaryFile("w", suffix=".yml", delete=False) as handle: |
| 231 | + handle.write("name: should-be-overridden\n") |
| 232 | + metadata_path = Path(handle.name) |
| 233 | + |
| 234 | + fake_yaml = mock.Mock() |
| 235 | + fake_yaml.safe_load.return_value = {"name": "from-yaml", "version": "1.0.0"} |
| 236 | + |
| 237 | + try: |
| 238 | + with mock.patch.object(module, "yaml", fake_yaml): |
| 239 | + metadata = module.load_metadata(metadata_path) |
| 240 | + finally: |
| 241 | + os.remove(metadata_path) |
| 242 | + |
| 243 | + self.assertEqual(metadata, {"name": "from-yaml", "version": "1.0.0"}) |
| 244 | + fake_yaml.safe_load.assert_called_once() |
| 245 | + |
| 246 | + def test_load_metadata_uses_simple_parser_when_yaml_unavailable(self): |
| 247 | + module = load_validator_module() |
| 248 | + |
| 249 | + with tempfile.NamedTemporaryFile("w", suffix=".yml", delete=False) as handle: |
| 250 | + handle.write( |
| 251 | + """ |
| 252 | + name: demo-plugin |
| 253 | + version: \"0.2.3\" |
| 254 | + """ |
| 255 | + ) |
| 256 | + metadata_path = Path(handle.name) |
| 257 | + |
| 258 | + yaml_backup = getattr(module, "yaml", None) |
| 259 | + try: |
| 260 | + module.yaml = None |
| 261 | + metadata = module.load_metadata(metadata_path) |
| 262 | + finally: |
| 263 | + module.yaml = yaml_backup |
| 264 | + os.remove(metadata_path) |
| 265 | + |
| 266 | + self.assertEqual(metadata.get("name"), "demo-plugin") |
| 267 | + self.assertEqual(metadata.get("version"), "0.2.3") |
| 268 | + |
| 269 | + def test_load_plugins_index_accepts_valid_object(self): |
| 270 | + module = load_validator_module() |
| 271 | + |
| 272 | + index_obj = { |
| 273 | + "good-plugin": {"name": "Good Plugin", "repo": "https://github.com/example/good"}, |
| 274 | + "another-plugin": {"name": "Another Plugin"}, |
| 275 | + } |
| 276 | + |
| 277 | + with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as handle: |
| 278 | + json.dump(index_obj, handle) |
| 279 | + index_path = Path(handle.name) |
| 280 | + |
| 281 | + try: |
| 282 | + plugins = module.load_plugins_index(index_path) |
| 283 | + finally: |
| 284 | + os.remove(index_path) |
| 285 | + |
| 286 | + self.assertEqual(plugins, index_obj) |
| 287 | + |
| 288 | + def test_load_plugins_index_rejects_json_array(self): |
| 289 | + module = load_validator_module() |
| 290 | + |
| 291 | + with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as handle: |
| 292 | + json.dump([{"name": "array-entry"}], handle) |
| 293 | + index_path = Path(handle.name) |
| 294 | + |
| 295 | + try: |
| 296 | + with self.assertRaisesRegex(ValueError, "plugins.json must contain a JSON object"): |
| 297 | + module.load_plugins_index(index_path) |
| 298 | + finally: |
| 299 | + os.remove(index_path) |
| 300 | + |
| 301 | + def test_load_plugins_index_rejects_non_dict_values(self): |
| 302 | + module = load_validator_module() |
| 303 | + |
| 304 | + index_obj = { |
| 305 | + "valid-plugin": {"name": "Valid Plugin", "repo": "https://github.com/example/valid"}, |
| 306 | + "not-a-dict": "just-a-string", |
| 307 | + } |
| 308 | + |
| 309 | + with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as handle: |
| 310 | + json.dump(index_obj, handle) |
| 311 | + index_path = Path(handle.name) |
| 312 | + |
| 313 | + try: |
| 314 | + with self.assertRaisesRegex(ValueError, "plugins.json values must be objects/dicts"): |
| 315 | + module.load_plugins_index(index_path) |
| 316 | + finally: |
| 317 | + os.remove(index_path) |
| 318 | + |
| 319 | + |
| 320 | +class ValidationProgressTests(unittest.TestCase): |
| 321 | + def test_validate_selected_plugins_emits_progress_and_result_lines(self): |
| 322 | + module = load_validator_module() |
| 323 | + selected = [ |
| 324 | + ("plugin-a", {"repo": "https://github.com/example/plugin-a"}), |
| 325 | + ("plugin-b", {"repo": "https://github.com/example/plugin-b"}), |
| 326 | + ] |
| 327 | + fake_results = [ |
| 328 | + {"plugin": "plugin-a", "ok": True, "stage": "load", "message": "ok"}, |
| 329 | + {"plugin": "plugin-b", "ok": False, "stage": "metadata", "message": "invalid metadata.yaml"}, |
| 330 | + ] |
| 331 | + |
| 332 | + with mock.patch.object(module, "validate_plugin", side_effect=fake_results) as validate_mock: |
| 333 | + with mock.patch("builtins.print") as print_mock: |
| 334 | + results = module.validate_selected_plugins( |
| 335 | + selected=selected, |
| 336 | + astrbot_path=Path("/tmp/AstrBot"), |
| 337 | + script_path=Path("/tmp/run.py"), |
| 338 | + work_dir=Path("/tmp/work"), |
| 339 | + clone_timeout=60, |
| 340 | + load_timeout=300, |
| 341 | + ) |
| 342 | + |
| 343 | + self.assertEqual(results, fake_results) |
| 344 | + self.assertEqual(validate_mock.call_count, 2) |
| 345 | + print_mock.assert_any_call("[1/2] Validating plugin-a", flush=True) |
| 346 | + print_mock.assert_any_call("[1/2] PASS plugin-a [load] ok", flush=True) |
| 347 | + print_mock.assert_any_call("[2/2] FAIL plugin-b [metadata] invalid metadata.yaml", flush=True) |
| 348 | + |
198 | 349 |
|
199 | 350 | class MetadataValidationTests(unittest.TestCase): |
200 | 351 | def test_reports_missing_required_metadata_fields(self): |
|
0 commit comments