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
10 changes: 8 additions & 2 deletions agentbridge/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ def scaffold_adapter_plugin(
),
}

collisions = [path for path in files if path.exists()]
if collisions and not force:
formatted = ", ".join(str(path) for path in collisions)
raise FileExistsError(
f"Scaffold target already exists at generated path(s): {formatted}; "
"pass force=True to overwrite"
)

for path, content in files.items():
if path.exists() and not force:
raise FileExistsError(f"{path} already exists; pass force=True to overwrite")
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8")
created.append(ScaffoldedFile(path=path, created=True))
Expand Down
21 changes: 21 additions & 0 deletions tests/test_scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,24 @@ def test_scaffold_adapter_plugin_does_not_overwrite_without_force(tmp_path) -> N
assert "already exists" in str(exc)
else: # pragma: no cover - defensive assertion style
raise AssertionError("Expected existing files to fail without force")


def test_scaffold_adapter_plugin_preflights_collisions_before_writing(tmp_path) -> None:
target = tmp_path / "plugin"
package_dir = target / "agentbridge_custom"
package_dir.mkdir(parents=True)
(package_dir / "adapter.py").write_text("# existing adapter\n", encoding="utf-8")

try:
scaffold_adapter_plugin(target, backend_name="custom")
except FileExistsError as exc:
message = str(exc)
assert "adapter.py" in message
assert "force=True" in message
else: # pragma: no cover - defensive assertion style
raise AssertionError("Expected existing adapter.py to fail without force")

assert not (target / "pyproject.toml").exists()
assert not (target / "README.md").exists()
assert not (package_dir / "__init__.py").exists()
assert not (target / "tests" / "test_adapter.py").exists()
Loading