Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/specify_cli/command_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,26 @@ def init(
)
manifest.save()

# Extension and preset registration read init-options from disk.
# Persist this invocation's mode before re-registering either.
init_opts = {
**previous_init_options,
"ai": selected_ai,
"integration": resolved_integration.key,
"here": here,
"script": selected_script,
"feature_numbering": previous_init_options.get("feature_numbering", "sequential"),
"speckit_version": get_speckit_version(),
"allow_symlinks": allow_symlinks,
}
if resolved_integration.is_skills_mode(
integration_parsed_options or None, project_root=project_path
):
init_opts["ai_skills"] = True
else:
init_opts.pop("ai_skills", None)
save_init_options(project_path, init_opts)

if force:
from .integrations._helpers import (
_register_extensions_for_agent,
Expand Down Expand Up @@ -858,24 +878,6 @@ def init(
sanitized_wf = str(wf_err).replace("\n", " ").strip()
tracker.error("workflow", f"install failed: {sanitized_wf[:120]}")

init_opts = {
**previous_init_options,
"ai": selected_ai,
"integration": resolved_integration.key,
"here": here,
"script": selected_script,
"feature_numbering": previous_init_options.get("feature_numbering", "sequential"),
"speckit_version": get_speckit_version(),
"allow_symlinks": allow_symlinks,
}
if resolved_integration.is_skills_mode(
integration_parsed_options or None, project_root=project_path
):
init_opts["ai_skills"] = True
else:
init_opts.pop("ai_skills", None)
save_init_options(project_path, init_opts)

ensure_executable_scripts(project_path, tracker=tracker)

if preset:
Expand Down
5 changes: 5 additions & 0 deletions src/specify_cli/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,7 @@ def _register_extension_skills(
from ..integrations import get_integration
from ..integrations.base import IntegrationBase
from ..integrations._file_changes import changing_file, unlink, write_text
from ..shared_infra import _validate_safe_shared_directory

written: List[str] = []
opts = load_init_options(self.project_root)
Expand Down Expand Up @@ -1618,6 +1619,10 @@ def _replacement(match: re.Match[str]) -> str:

# Check if skill already exists before creating the directory
skill_subdir = skills_dir / skill_name
try:
_validate_safe_shared_directory(skills_dir, skill_subdir)
except (OSError, ValueError):
continue
skill_file = skill_subdir / "SKILL.md"
cache_root = extension_dir / ".specify-dev" / "extension-skills"
cache_file = cache_root / skill_name / "SKILL.md"
Expand Down
73 changes: 73 additions & 0 deletions tests/integrations/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,79 @@ def test_init_here_force_reapplies_installed_presets(self, tmp_path, monkeypatch
"_register_extensions_for_agent was not called during init --here --force"
)

@pytest.mark.parametrize("skills_mode", [True, False])
def test_init_here_force_registers_extensions_in_selected_mode(self, tmp_path, skills_mode):
"""A preinstalled extension follows this init's mode without saved options."""
from typer.testing import CliRunner

from specify_cli import _locate_bundled_extension, app
from specify_cli.extensions import ExtensionManager

project = tmp_path / "force-extension-mode"
project.mkdir()
extension = _locate_bundled_extension("git")
assert extension is not None
ExtensionManager(project).install_from_directory(
extension, "1.3.2", register_commands=False
)
assert not (project / ".specify" / "init-options.json").exists()

old_cwd = os.getcwd()
try:
os.chdir(project)
result = CliRunner().invoke(
app,
[
"init", "--here", "--force", "--non-interactive",
"--integration", "copilot", "--script", "sh",
"--ignore-agent-tools", "--integration-options",
"--skills" if skills_mode else "--commands",
],
catch_exceptions=False,
)
finally:
os.chdir(old_cwd)

assert result.exit_code == 0, result.output
skill = project / ".github" / "skills" / "speckit-git-feature" / "SKILL.md"
command = project / ".github" / "agents" / "speckit.git.feature.agent.md"
assert skill.exists() is skills_mode
assert command.exists() is not skills_mode

def test_init_force_preserves_external_extension_skill(self, tmp_path, monkeypatch):
from typer.testing import CliRunner
from specify_cli import _locate_bundled_extension, app
from specify_cli.extensions import ExtensionManager

project = tmp_path / "force-extension-symlink"
project.mkdir()
extension = _locate_bundled_extension("git")
assert extension is not None
ExtensionManager(project).install_from_directory(
extension, "1.3.2", register_commands=False,
)
external = tmp_path / "external"
external.mkdir()
external_file = external / "SKILL.md"
external_file.write_bytes(b"Keep external skill\r\n")
skill = project / ".github" / "skills" / "speckit-git-feature"
skill.parent.mkdir(parents=True)
try:
skill.symlink_to(external, target_is_directory=True)
except OSError:
pytest.skip("Current platform/user cannot create directory symlinks")
monkeypatch.chdir(project)

result = CliRunner().invoke(app, [
"init", "--here", "--force", "--non-interactive", "--offline",
"--integration", "copilot", "--script", "sh",
"--ignore-agent-tools", "--integration-options", "--skills",
])

assert result.exit_code == 0, result.output
assert skill.is_symlink()
assert external_file.read_bytes() == b"Keep external skill\r\n"

def test_init_here_without_force_does_not_reapply_presets(self, tmp_path):
"""Without --force (fresh project), _register_presets_for_agent should NOT be called."""
from unittest.mock import MagicMock, patch
Expand Down
26 changes: 26 additions & 0 deletions tests/test_extension_skills.py
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,32 @@ def test_force_false_skips_existing_skill(self, project_dir, temp_dir):
assert "speckit-test-ext-hello" not in written
assert skill_file.read_text(encoding="utf-8") == "core-template content only"

@pytest.mark.parametrize("force", [False, True])
def test_skill_directory_symlink_preserves_external_files(
self, project_dir, temp_dir, force,
):
_create_init_options(project_dir, ai="claude", ai_skills=True)
skills_dir = _create_skills_dir(project_dir, ai="claude")
ext_dir = _create_extension_dir(temp_dir)
external = temp_dir / "external-skills"
external.mkdir()
external_file = external / "SKILL.md"
external_file.write_bytes(b"External user content\r\n")
link = skills_dir / "speckit-test-ext-hello"
try:
link.symlink_to(external, target_is_directory=True)
except OSError:
pytest.skip("Current platform/user cannot create directory symlinks")

manager = ExtensionManager(project_dir)
manifest = ExtensionManifest(ext_dir / "extension.yml")
written = manager._register_extension_skills(manifest, ext_dir, force=force)

assert external_file.read_bytes() == b"External user content\r\n"
assert link.is_symlink()
assert "speckit-test-ext-hello" not in written
assert "speckit-test-ext-world" in written

def test_force_true_overwrites_existing_skill(self, project_dir, temp_dir):
"""With force=True the function must overwrite the existing SKILL.md.

Expand Down
Loading