Skip to content
Merged
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
21 changes: 19 additions & 2 deletions src/specify_cli/command_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ def init(
invoke_prefix_for_integration as _invoke_prefix_for_integration,
with_integration_setting as _with_integration_setting,
)
from .integration_state import (
installed_integration_keys,
integration_settings as _stored_integration_settings,
try_read_integration_json,
)
from .integrations._commands import (
_parse_integration_options,
_write_integration_json,
Expand Down Expand Up @@ -709,6 +714,18 @@ def init(
from .integrations.manifest import IntegrationManifest

tracker.start("integration")
# Re-init keeps the other installed integrations and their
# settings; the selected one stores only the options passed here.
current, _state_error = try_read_integration_json(project_path)
current = current or {}
installed_keys = installed_integration_keys(current)
other_settings = {
key: setting
for key, setting in _stored_integration_settings(current).items()
if key != resolved_integration.key
}
if resolved_integration.key not in installed_keys:
installed_keys.append(resolved_integration.key)
manifest = IntegrationManifest(
resolved_integration.key,
project_path,
Expand Down Expand Up @@ -763,7 +780,7 @@ def init(
)
manifest.save()
integration_settings = _with_integration_setting(
{},
{"integration_settings": other_settings},
resolved_integration.key,
resolved_integration,
script_type=selected_script,
Expand All @@ -774,7 +791,7 @@ def init(
_write_integration_json(
project_path,
resolved_integration.key,
[resolved_integration.key],
installed_keys,
integration_settings,
)

Expand Down
61 changes: 61 additions & 0 deletions tests/specify_cli/integrations/test_installed_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,67 @@ def test_new_project_init_from_catalog_environment(tmp_path, server, monkeypatch
assert KEY in read_records(project)


def adapter_files(project):
package = f".specify/integrations/packages/{KEY}/"
return {
path: content for path, content in snapshot(project).items()
if path.startswith((".sample-agent/", package))
or path in {f".specify/integrations/{KEY}.manifest.json", ".specify/integrations/packages.json"}
}


def reinit_default_beside_adapter(tmp_path, server):
"""Install Claude (default) and the external adapter, then re-init Claude."""
publish(server)
project = catalog_project(tmp_path, server)
assert run(project, ["integration", "install", "claude"]).exit_code == 0
install(project)
before = adapter_files(project)
setting = json.loads((project / ".specify/integration.json").read_text())["integration_settings"][KEY]
reinit = run(project, [
"init", "--here", "--force", "--non-interactive", "--ignore-agent-tools",
"--integration", "claude", "--script", "py",
])
assert reinit.exit_code == 0, reinit.output
return project, before, setting


def test_reinit_keeps_installed_external_adapter(tmp_path, server):
project, before, setting = reinit_default_beside_adapter(tmp_path, server)
state = json.loads((project / ".specify/integration.json").read_text())
assert state["installed_integrations"] == ["claude", KEY]
assert state["default_integration"] == "claude"
assert state["integration_settings"][KEY] == setting
assert KEY in read_records(project)
assert adapter_files(project) == before


def test_reinit_keeps_external_adapter_upgradeable_and_uninstallable(tmp_path, server):
project, _, _ = reinit_default_beside_adapter(tmp_path, server)
publish(server, version="2.0.0")
upgraded = run(project, ["integration", "upgrade", KEY, "--trust-integration"])
assert upgraded.exit_code == 0, upgraded.output
assert read_records(project)[KEY]["version"] == "2.0.0"
removed = run(project, ["integration", "uninstall", KEY])
assert removed.exit_code == 0, removed.output
assert adapter_files(project) == {}
state = json.loads((project / ".specify/integration.json").read_text())
assert state["installed_integrations"] == ["claude"]
assert KEY not in INTEGRATION_REGISTRY


def test_uninstalling_reinitialized_default_falls_back_to_external_adapter(tmp_path, server):
project, _, _ = reinit_default_beside_adapter(tmp_path, server)
removed = run(project, ["integration", "uninstall", "claude"])
assert removed.exit_code == 0, removed.output
state = json.loads((project / ".specify/integration.json").read_text())
assert state["installed_integrations"] == [KEY]
assert state["integration"] == state["default_integration"] == KEY
assert not (project / ".claude/skills/speckit-plan/SKILL.md").exists()
assert (project / ".sample-agent/skills/speckit-plan/SKILL.md").is_file()
assert KEY in read_records(project) and KEY in INTEGRATION_REGISTRY


@pytest.mark.parametrize("damaged_uninstall", [False, True])
@pytest.mark.parametrize("failed_commit", [False, True])
def test_extension_and_preset_contributions_follow_active_external_adapter(
Expand Down
68 changes: 68 additions & 0 deletions tests/specify_cli/test_command_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,74 @@ def test_reinit_preserves_feature_numbering(tmp_path: Path, monkeypatch, numberi
assert updated["here"] is True


@pytest.mark.parametrize("selected", ["claude", "codex", "gemini"])
def test_reinit_keeps_other_installed_integrations(tmp_path, monkeypatch, selected):
"""The upgrade-guide fallback must keep the other installed integrations."""
monkeypatch.chdir(tmp_path)
runner = CliRunner()
args = ["--script", "sh", "--ignore-agent-tools", "--non-interactive"]
result = runner.invoke(app, ["init", "project", "--integration", "claude", *args])
assert result.exit_code == 0, result.output
project = tmp_path / "project"
monkeypatch.chdir(project)
result = runner.invoke(app, ["integration", "install", "codex", "--script", "ps"])
assert result.exit_code == 0, result.output
state_path = project / ".specify/integration.json"
before_state = json.loads(state_path.read_text(encoding="utf-8"))
retained = "claude" if selected == "codex" else "codex"
manifest_path = project / f".specify/integrations/{retained}.manifest.json"
before = {manifest_path: manifest_path.read_bytes()}
for relative in json.loads(before[manifest_path])["files"]:
path = project / relative
before[path] = path.read_bytes()

result = runner.invoke(app, ["init", "--here", "--force", "--integration", selected, *args])

assert result.exit_code == 0, result.output
state = json.loads(state_path.read_text(encoding="utf-8"))
expected = ["claude", "codex"] + (["gemini"] if selected == "gemini" else [])
assert state["installed_integrations"] == expected
assert state["default_integration"] == selected
assert state["integration_settings"][retained] == before_state["integration_settings"][retained]
assert all(path.read_bytes() == content for path, content in before.items())
result = runner.invoke(app, ["integration", "status", "--json"])
assert result.exit_code == 0, result.output
assert json.loads(result.output)["installed_integrations"] == expected
result = runner.invoke(app, ["integration", "upgrade", retained])
assert result.exit_code == 0, result.output


def test_reinit_stores_only_the_options_passed_to_it(tmp_path, monkeypatch):
"""Re-init replaces the selected integration's options and keeps the others'."""
monkeypatch.chdir(tmp_path)
runner = CliRunner()
args = ["--script", "sh", "--ignore-agent-tools", "--non-interactive"]
result = runner.invoke(app, ["init", "fresh", "--integration", "claude", *args])
assert result.exit_code == 0, result.output
fresh = json.loads((tmp_path / "fresh/.specify/integration.json").read_text(encoding="utf-8"))
result = runner.invoke(app, [
"init", "project", "--integration", "claude",
"--integration-options=--no-model-invocation", *args,
])
assert result.exit_code == 0, result.output
project = tmp_path / "project"
monkeypatch.chdir(project)
result = runner.invoke(app, [
"integration", "install", "codex", "--script", "ps", "--integration-options=--skills",
])
assert result.exit_code == 0, result.output
state_path = project / ".specify/integration.json"
before = json.loads(state_path.read_text(encoding="utf-8"))
assert before["integration_settings"]["claude"]["parsed_options"] == {"no_model_invocation": True}

result = runner.invoke(app, ["init", "--here", "--force", "--integration", "claude", *args])

assert result.exit_code == 0, result.output
state = json.loads(state_path.read_text(encoding="utf-8"))
assert state["integration_settings"]["claude"] == fresh["integration_settings"]["claude"]
assert state["integration_settings"]["codex"] == before["integration_settings"]["codex"]


@pytest.mark.parametrize("name", ["proj [v2]", "my[bold]app"])
def test_next_steps_cd_shows_the_real_project_name(tmp_path: Path, name: str):
"""The `cd` line must name the directory that was actually created."""
Expand Down
Loading