chore: enhance project initialization by adding post-task handling and improving file overwrite behavior
Some checks failed
CI / test (3.13) (push) Failing after 31s

This commit is contained in:
Alexander Kalinovsky
2025-08-27 16:37:37 +03:00
parent 90b0202f39
commit 62b12690c8
7 changed files with 308 additions and 158 deletions

View File

@@ -10,7 +10,8 @@ from pathlib import Path
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
# pytest is used by the test framework
import typer
from quickbot_cli.cli import _init_project
@@ -203,9 +204,33 @@ class TestCLIIntegration:
spec_content = {
"variables": {
"project_name": {"prompt": "Project name", "default": "simple_bot"},
"include_alembic": {"prompt": "Include Alembic?", "choices": ["yes", "no"], "default": "no"},
"include_i18n": {"prompt": "Include i18n?", "choices": ["yes", "no"], "default": "no"},
}
"include_alembic": {"prompt": "Include Alembic?", "choices": [True, False], "default": False},
"include_i18n": {"prompt": "Include i18n?", "choices": [True, False], "default": False},
},
"post_tasks": [
{
"when": "{{ not include_alembic }}",
"run": [
"rm",
"-rf",
"alembic",
"scripts/migrations_generate.sh",
"scripts/migrations_apply.sh",
],
},
{
"when": "{{ not include_i18n }}",
"run": [
"rm",
"-rf",
"locales",
"scripts/babel_init.sh",
"scripts/babel_extract.sh",
"scripts/babel_update.sh",
"scripts/babel_compile.sh",
],
},
],
}
spec_file = template_dir / "__template__.yaml"
@@ -283,8 +308,8 @@ class TestCLIIntegration:
assert "app = FastAPI(title='overwrite_test')" in (output_dir / "app" / "main.py").read_text()
assert "existing content" not in (output_dir / "app" / "main.py").read_text()
def test_project_generation_without_overwrite_fails(self) -> None:
"""Test that project generation fails without overwrite when files exist."""
def test_project_generation_without_overwrite_skips_existing(self) -> None:
"""Project generation should skip existing files when overwrite is False."""
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_path = Path(tmp_dir)
@@ -310,9 +335,12 @@ class TestCLIIntegration:
existing_file.parent.mkdir(parents=True, exist_ok=True)
existing_file.write_text("existing content")
# Should fail with FileExistsError when overwrite is False
with pytest.raises(FileExistsError):
# Should complete and keep the existing file content
with patch("quickbot_cli.cli.typer.secho") as mock_secho:
_init_project(output_dir, "basic", project_name="overwrite_test")
# Should show warning about skipping existing file
expected_warning = f"Warning: Skipping existing file: {output_dir / 'app' / 'main.py'}"
mock_secho.assert_any_call(expected_warning, fg=typer.colors.YELLOW)
# Check that file was not overwritten
assert "existing content" in (output_dir / "app" / "main.py").read_text()