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
13 changes: 12 additions & 1 deletion src/specify_cli/command_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,18 @@ def init(
ensure_constitution_from_template(project_path, tracker=tracker)

tracker.complete("final", "project ready")
except (typer.Exit, SystemExit):
except (typer.Exit, SystemExit) as exc:
# A failing step that already reported its own error exits
# here (e.g. a shared-infrastructure OSError becomes
# typer.Exit(1)); remove a directory this run created, as the
# generic failure path below does.
exit_code = getattr(exc, "exit_code", getattr(exc, "code", 1))
if (
exit_code not in (0, None)
and not here and project_path.exists() and not dir_existed_before
and initial_directory_state(project_path) is None
):
shutil.rmtree(project_path, ignore_errors=True)
raise
except Exception as e:
tracker.error("final", str(e))
Expand Down
66 changes: 66 additions & 0 deletions tests/specify_cli/test_command_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,72 @@ def test_invalid_integration_value_is_rendered_literally(tmp_path: Path):
assert "nope[/red]" in _strip(result.stdout)


def _fail_shared_infra(monkeypatch):
"""Make the shared-infrastructure step fail part-way, as a full disk would.

``_install_shared_infra_or_exit`` turns the ``OSError`` into
``typer.Exit(1)``, which is how init sees a real shared-infra failure.
"""
import specify_cli

def partial_install(project_path, *args, **kwargs):
scripts = Path(project_path) / ".specify" / "scripts"
scripts.mkdir(parents=True, exist_ok=True)
(scripts / "partial.sh").write_text("#!/usr/bin/env bash\n", encoding="utf-8")
raise OSError(28, "No space left on device")

monkeypatch.setattr(specify_cli, "_install_shared_infra", partial_install)


def test_shared_infra_failure_removes_new_project_dir(tmp_path: Path, monkeypatch):
"""A failed init into a new directory removes it, so a retry can succeed.

The shared-infrastructure failure arrives as ``typer.Exit(1)``. It used to
be re-raised before the cleanup ran, leaving a half-initialized project
that made the retry fail with "Directory already exists".
"""
_fail_shared_infra(monkeypatch)

result = _init(tmp_path, "proj")

assert result.exit_code == 1, _strip(result.stdout)
assert "Failed to install shared infrastructure" in _strip(result.stdout)
assert not (tmp_path / "proj").exists()

monkeypatch.undo()
Comment on lines +216 to +224
retry = _init(tmp_path, "proj")
assert retry.exit_code == 0, _strip(retry.stdout)
assert (tmp_path / "proj" / ".specify").is_dir()


def test_shared_infra_failure_keeps_existing_project_dir(tmp_path: Path, monkeypatch):
"""A failed init never deletes a directory that existed before it ran."""
existing = tmp_path / "proj"
existing.mkdir()
keep = existing / "notes.txt"
keep.write_text("user data\n", encoding="utf-8")
_fail_shared_infra(monkeypatch)

previous = os.getcwd()
os.chdir(tmp_path)
try:
result = CliRunner().invoke(
app,
[
"init", "proj", "--force",
"--integration", "generic",
"--integration-options", "--commands-dir .agent/commands",
"--ignore-agent-tools", "--offline",
],
catch_exceptions=True,
)
finally:
os.chdir(previous)

assert result.exit_code == 1, _strip(result.stdout)
assert keep.read_text(encoding="utf-8") == "user data\n"


def _cd_argument(stdout: str) -> str:
"""Return the argument of the printed `cd` command, verbatim.

Expand Down
Loading