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
18 changes: 18 additions & 0 deletions src/fastapi_new/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ def new(
if not current_dir and config.path.exists():
_exit_with_error(toolkit, f"Directory '{config.name}' already exists.")

# In current directory mode there is no new directory to protect
# existing work, so check the files the template writes. Without
# this, running the command in the wrong directory silently
# destroys an existing main.py and README.md.
if current_dir:
existing = [
name
for name in ("main.py", "README.md")
if (config.path / name).exists()
]
if existing:
_exit_with_error(
toolkit,
"Refusing to overwrite existing file(s): "
+ ", ".join(existing)
+ ". Move them aside or run in an empty directory.",
)

if shutil.which("uv") is None:
_exit_with_error(
toolkit,
Expand Down
12 changes: 12 additions & 0 deletions tests/test_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ def test_rejects_existing_directory(temp_project_dir: Path) -> None:
assert "Directory 'existing_project' already exists." in result.output


def test_refuses_to_overwrite_existing_files_in_current_dir(
temp_project_dir: Path,
) -> None:
(temp_project_dir / "main.py").write_text("precious code")
(temp_project_dir / "README.md").write_text("precious readme")
result = runner.invoke(app, [])
assert result.exit_code == 1
assert "Refusing to overwrite" in result.output
assert (temp_project_dir / "main.py").read_text() == "precious code"
assert (temp_project_dir / "README.md").read_text() == "precious readme"


def test_rejects_python_below_3_10(temp_project_dir: Path) -> None:
result = runner.invoke(app, ["test_project", "--python", "3.9"])
assert result.exit_code == 1
Expand Down
Loading