|
7 | 7 |
|
8 | 8 | import json |
9 | 9 | import shutil |
| 10 | +import zipfile |
10 | 11 | from pathlib import Path |
11 | 12 | from unittest.mock import patch |
12 | 13 |
|
@@ -696,6 +697,174 @@ def test_update_skips_catalog_entry_without_version(self, tmp_path): |
696 | 697 | assert "test-ext: Missing catalog version (skipping)" in flat |
697 | 698 | assert ExtensionManager(project_dir).registry.get("test-ext")["version"] == "1.0.0" |
698 | 699 |
|
| 700 | + def test_update_preserves_skills_mode_registration(self, tmp_path, monkeypatch): |
| 701 | + """Updating an extension must keep Claude skills in skills mode.""" |
| 702 | + fake_home = tmp_path / "home" |
| 703 | + fake_home.mkdir() |
| 704 | + monkeypatch.setattr(Path, "home", lambda: fake_home) |
| 705 | + |
| 706 | + project_dir = tmp_path / "project" |
| 707 | + (project_dir / ".specify").mkdir(parents=True) |
| 708 | + (project_dir / ".claude" / "skills").mkdir(parents=True) |
| 709 | + (project_dir / ".specify" / "init-options.json").write_text( |
| 710 | + json.dumps({"ai": "claude", "ai_skills": True, "script": "sh"}), |
| 711 | + encoding="utf-8", |
| 712 | + ) |
| 713 | + |
| 714 | + manager = ExtensionManager(project_dir) |
| 715 | + v1_dir = self._create_extension_source(tmp_path, "1.0.0") |
| 716 | + manifest_path = v1_dir / "extension.yml" |
| 717 | + manifest = yaml.safe_load(manifest_path.read_text(encoding="utf-8")) |
| 718 | + manifest["provides"]["commands"][0]["aliases"] = [ |
| 719 | + "speckit.test-ext.alternate" |
| 720 | + ] |
| 721 | + manifest_path.write_text(yaml.safe_dump(manifest), encoding="utf-8") |
| 722 | + manager.install_from_directory(v1_dir, "0.1.0") |
| 723 | + |
| 724 | + skill_names = ["speckit-test-ext-hello", "speckit-test-ext-alternate"] |
| 725 | + command_names = [ |
| 726 | + "speckit.test-ext.hello", |
| 727 | + "speckit.test-ext.alternate", |
| 728 | + ] |
| 729 | + skill_files = [ |
| 730 | + project_dir / ".claude" / "skills" / name / "SKILL.md" |
| 731 | + for name in skill_names |
| 732 | + ] |
| 733 | + assert all(path.is_file() for path in skill_files) |
| 734 | + old_entry = manager.registry.get("test-ext") |
| 735 | + assert old_entry["registered_skills"] == [] |
| 736 | + assert old_entry["registered_commands"] == {"claude": command_names} |
| 737 | + |
| 738 | + v2_dir = self._create_extension_source(tmp_path, "2.0.0") |
| 739 | + manifest_path = v2_dir / "extension.yml" |
| 740 | + manifest = yaml.safe_load(manifest_path.read_text(encoding="utf-8")) |
| 741 | + manifest["provides"]["commands"][0]["aliases"] = [ |
| 742 | + "speckit.test-ext.alternate" |
| 743 | + ] |
| 744 | + manifest_path.write_text(yaml.safe_dump(manifest), encoding="utf-8") |
| 745 | + command_path = v2_dir / "commands" / "hello.md" |
| 746 | + command_path.write_text( |
| 747 | + command_path.read_text(encoding="utf-8") + "\nupdated command\n", |
| 748 | + encoding="utf-8", |
| 749 | + ) |
| 750 | + zip_path = tmp_path / "test-ext-2.0.0.zip" |
| 751 | + with zipfile.ZipFile(zip_path, "w") as archive: |
| 752 | + for path in v2_dir.rglob("*"): |
| 753 | + if path.is_file(): |
| 754 | + archive.write(path, path.relative_to(v2_dir)) |
| 755 | + |
| 756 | + runner = CliRunner() |
| 757 | + with ( |
| 758 | + patch.object(Path, "cwd", return_value=project_dir), |
| 759 | + patch.object( |
| 760 | + ExtensionCatalog, |
| 761 | + "get_extension_info", |
| 762 | + return_value={ |
| 763 | + "id": "test-ext", |
| 764 | + "name": "Test Extension", |
| 765 | + "version": "2.0.0", |
| 766 | + "_install_allowed": True, |
| 767 | + }, |
| 768 | + ), |
| 769 | + patch.object( |
| 770 | + ExtensionCatalog, "download_extension", return_value=zip_path |
| 771 | + ), |
| 772 | + ): |
| 773 | + result = runner.invoke( |
| 774 | + app, |
| 775 | + ["extension", "update", "test-ext"], |
| 776 | + input="y\n", |
| 777 | + catch_exceptions=True, |
| 778 | + ) |
| 779 | + |
| 780 | + assert result.exit_code == 0, result.output |
| 781 | + updated = ExtensionManager(project_dir).registry.get("test-ext") |
| 782 | + assert updated["registered_skills"] == [] |
| 783 | + assert updated["registered_commands"] == {"claude": command_names} |
| 784 | + fresh_project = tmp_path / "fresh-project" |
| 785 | + (fresh_project / ".specify").mkdir(parents=True) |
| 786 | + (fresh_project / ".claude" / "skills").mkdir(parents=True) |
| 787 | + (fresh_project / ".specify" / "init-options.json").write_text( |
| 788 | + json.dumps({"ai": "claude", "ai_skills": True, "script": "sh"}), |
| 789 | + encoding="utf-8", |
| 790 | + ) |
| 791 | + ExtensionManager(fresh_project).install_from_directory(v2_dir, "0.1.0") |
| 792 | + for name, updated_path in zip(skill_names, skill_files, strict=True): |
| 793 | + fresh_path = fresh_project / ".claude" / "skills" / name / "SKILL.md" |
| 794 | + assert "updated command" in updated_path.read_text(encoding="utf-8") |
| 795 | + assert updated_path.read_text(encoding="utf-8") == fresh_path.read_text( |
| 796 | + encoding="utf-8" |
| 797 | + ) |
| 798 | + |
| 799 | + def test_consecutive_hermes_updates_refresh_global_skill(self, tmp_path, monkeypatch): |
| 800 | + """Each update replaces the marker-owned skill in Hermes' home directory.""" |
| 801 | + fake_home = tmp_path / "home" |
| 802 | + fake_home.mkdir() |
| 803 | + monkeypatch.setattr(Path, "home", lambda: fake_home) |
| 804 | + |
| 805 | + project_dir = tmp_path / "hermes-project" |
| 806 | + (project_dir / ".specify").mkdir(parents=True) |
| 807 | + (project_dir / ".specify" / "init-options.json").write_text( |
| 808 | + json.dumps({"ai": "hermes", "ai_skills": True, "script": "sh"}), |
| 809 | + encoding="utf-8", |
| 810 | + ) |
| 811 | + skill_file = ( |
| 812 | + fake_home / ".hermes" / "skills" / "speckit-test-ext-hello" / "SKILL.md" |
| 813 | + ) |
| 814 | + |
| 815 | + def prepare_source(version): |
| 816 | + source = self._create_extension_source(tmp_path, version) |
| 817 | + command = source / "commands" / "hello.md" |
| 818 | + command.write_text( |
| 819 | + command.read_text(encoding="utf-8") + f"\nrelease {version}\n", |
| 820 | + encoding="utf-8", |
| 821 | + ) |
| 822 | + return source |
| 823 | + |
| 824 | + manager = ExtensionManager(project_dir) |
| 825 | + v1 = prepare_source("1.0.0") |
| 826 | + manager.install_from_directory(v1, "0.1.0") |
| 827 | + assert "release 1.0.0" in skill_file.read_text(encoding="utf-8") |
| 828 | + assert manager.registry.get("test-ext")["registered_commands"] == { |
| 829 | + "hermes": ["speckit.test-ext.hello"] |
| 830 | + } |
| 831 | + |
| 832 | + runner = CliRunner() |
| 833 | + for version in ("2.0.0", "3.0.0"): |
| 834 | + source = prepare_source(version) |
| 835 | + archive_path = tmp_path / f"test-ext-{version}.zip" |
| 836 | + with zipfile.ZipFile(archive_path, "w") as archive: |
| 837 | + for path in source.rglob("*"): |
| 838 | + if path.is_file(): |
| 839 | + archive.write(path, path.relative_to(source)) |
| 840 | + with ( |
| 841 | + patch.object(Path, "cwd", return_value=project_dir), |
| 842 | + patch.object( |
| 843 | + ExtensionCatalog, |
| 844 | + "get_extension_info", |
| 845 | + return_value={ |
| 846 | + "id": "test-ext", |
| 847 | + "name": "Test Extension", |
| 848 | + "version": version, |
| 849 | + "_install_allowed": True, |
| 850 | + }, |
| 851 | + ), |
| 852 | + patch.object( |
| 853 | + ExtensionCatalog, |
| 854 | + "download_extension", |
| 855 | + return_value=archive_path, |
| 856 | + ), |
| 857 | + ): |
| 858 | + result = runner.invoke( |
| 859 | + app, |
| 860 | + ["extension", "update", "test-ext"], |
| 861 | + input="y\n", |
| 862 | + catch_exceptions=True, |
| 863 | + ) |
| 864 | + assert result.exit_code == 0, result.output |
| 865 | + assert f"release {version}" in skill_file.read_text(encoding="utf-8") |
| 866 | + assert ExtensionManager(project_dir).registry.get("test-ext")["version"] == version |
| 867 | + |
699 | 868 | def test_update_failure_rolls_back_registry_hooks_and_commands(self, tmp_path, monkeypatch): |
700 | 869 | """Failed update should restore original registry, hooks, and command files.""" |
701 | 870 | from specify_cli import app |
|
0 commit comments