|
8 | 8 | import yaml |
9 | 9 |
|
10 | 10 | from specify_cli import save_init_options |
11 | | -from specify_cli.extensions import HookExecutor |
| 11 | +from specify_cli.extensions import ExtensionManifest, HookExecutor |
12 | 12 | from specify_cli.extensions._hook_projection import project_hooks |
13 | 13 |
|
14 | 14 |
|
@@ -352,6 +352,107 @@ def test_shared_infrastructure_refresh_projects_existing_config(tmp_path, monkey |
352 | 352 | _assert_snapshot(tmp_path) |
353 | 353 |
|
354 | 354 |
|
| 355 | +@pytest.mark.parametrize("opt_in", [False, True]) |
| 356 | +def test_shared_infrastructure_refresh_retires_projection_without_config( |
| 357 | + tmp_path, monkeypatch, opt_in |
| 358 | +): |
| 359 | + import specify_cli |
| 360 | + |
| 361 | + executor = _enabled_executor(tmp_path) |
| 362 | + executor.save_project_config(_config()) |
| 363 | + executor.config_file.unlink() |
| 364 | + save_init_options(tmp_path, {"native_extension_hooks": opt_in}) |
| 365 | + monkeypatch.setattr(specify_cli, "_install_shared_infra_impl", lambda *args, **kwargs: True) |
| 366 | + |
| 367 | + assert specify_cli._install_shared_infra(tmp_path, "sh", refresh_managed=True) |
| 368 | + assert not (tmp_path / ".specify/hook-dispatch").exists() |
| 369 | + assert not executor.config_file.exists() |
| 370 | + |
| 371 | + |
| 372 | +def test_shared_infrastructure_refresh_rejects_symlinked_projection_without_config( |
| 373 | + tmp_path, monkeypatch |
| 374 | +): |
| 375 | + import specify_cli |
| 376 | + |
| 377 | + root = tmp_path / ".specify" |
| 378 | + root.mkdir() |
| 379 | + target = tmp_path / "external" |
| 380 | + target.mkdir() |
| 381 | + marker = target / "before_plan.json" |
| 382 | + marker.write_bytes(b"untouched") |
| 383 | + cache = root / "hook-dispatch" |
| 384 | + cache.symlink_to(target, target_is_directory=True) |
| 385 | + monkeypatch.setattr(specify_cli, "_install_shared_infra_impl", lambda *args, **kwargs: True) |
| 386 | + |
| 387 | + with pytest.raises(ValueError, match="symlinked hook projection"): |
| 388 | + specify_cli._install_shared_infra(tmp_path, "sh", refresh_managed=True) |
| 389 | + assert cache.is_symlink() |
| 390 | + assert marker.read_bytes() == b"untouched" |
| 391 | + assert not (root / "extensions.yml").exists() |
| 392 | + |
| 393 | + |
| 394 | +@pytest.mark.parametrize("cache_state", ["missing", "stale"]) |
| 395 | +@pytest.mark.parametrize("has_hooks", [False, True]) |
| 396 | +def test_unchanged_native_registration_refreshes_projection_without_rewriting_yaml( |
| 397 | + tmp_path, extension_dir, cache_state, has_hooks |
| 398 | +): |
| 399 | + if not has_hooks: |
| 400 | + manifest_path = extension_dir / "extension.yml" |
| 401 | + data = yaml.safe_load(manifest_path.read_text(encoding="utf-8")) |
| 402 | + data["hooks"] = {} |
| 403 | + manifest_path.write_text(yaml.safe_dump(data), encoding="utf-8") |
| 404 | + manifest = ExtensionManifest(extension_dir / "extension.yml") |
| 405 | + executor = _enabled_executor(tmp_path) |
| 406 | + executor.register_hooks(manifest) |
| 407 | + original = b"# Preserve this comment\r\n" + executor.config_file.read_bytes().replace( |
| 408 | + b"\n", b"\r\n" |
| 409 | + ) |
| 410 | + executor.config_file.write_bytes(original) |
| 411 | + cache = tmp_path / ".specify/hook-dispatch" |
| 412 | + if cache_state == "missing": |
| 413 | + for child in cache.iterdir(): |
| 414 | + child.unlink() |
| 415 | + cache.rmdir() |
| 416 | + else: |
| 417 | + (cache / "events.txt").write_bytes(b"before_plan\n") |
| 418 | + (cache / "before_plan.json").write_bytes(b"stale") |
| 419 | + |
| 420 | + executor.register_hooks(manifest) |
| 421 | + |
| 422 | + assert executor.config_file.read_bytes() == original |
| 423 | + assert (cache / "source.yml").read_bytes() == original |
| 424 | + expected = project_hooks(yaml.safe_load(original)) |
| 425 | + index = "".join(f"{event}\n" for event in expected).encode("utf-8") |
| 426 | + assert (cache / "events.txt").read_bytes() == index |
| 427 | + assert (cache / "events.txt.sha256").read_text().strip() == hashlib.sha256(index).hexdigest() |
| 428 | + for event, payload in expected.items(): |
| 429 | + assert (cache / f"{event}.json").read_bytes() == payload |
| 430 | + assert (cache / f"{event}.json.sha256").read_text().strip() == hashlib.sha256(payload).hexdigest() |
| 431 | + assert not (cache / "before_plan.json").exists() |
| 432 | + |
| 433 | + |
| 434 | +def test_unchanged_native_registration_reports_projection_failure( |
| 435 | + tmp_path, extension_dir, monkeypatch |
| 436 | +): |
| 437 | + import specify_cli.extensions._hook_projection as projection |
| 438 | + |
| 439 | + manifest = ExtensionManifest(extension_dir / "extension.yml") |
| 440 | + executor = _enabled_executor(tmp_path) |
| 441 | + executor.register_hooks(manifest) |
| 442 | + original = executor.config_file.read_bytes() |
| 443 | + real_write = projection.write_bytes |
| 444 | + |
| 445 | + def fail_index(path, content): |
| 446 | + if path.name == "events.txt": |
| 447 | + raise OSError("disk full") |
| 448 | + return real_write(path, content) |
| 449 | + |
| 450 | + monkeypatch.setattr(projection, "write_bytes", fail_index) |
| 451 | + with pytest.raises(OSError, match="disk full.*refresh-shared-infra"): |
| 452 | + executor.register_hooks(manifest) |
| 453 | + assert executor.config_file.read_bytes() == original |
| 454 | + |
| 455 | + |
355 | 456 | def test_project_hooks_is_pure(): |
356 | 457 | config = _config() |
357 | 458 | original = yaml.safe_dump(config) |
|
0 commit comments