From 5ca1b05f9c61f5b2a481695b7507b4912c263eb0 Mon Sep 17 00:00:00 2001 From: Andreas Salhus Bakseter <141913422+baksetercx@users.noreply.github.com> Date: Thu, 2 Jan 2025 12:54:51 +0100 Subject: [PATCH] Add e2e test for create Python --- pkg/create/create.go | 55 +++++++++++++++++++++++++++++++++++++++ tests/create/run_tests.sh | 48 +++++++++++++++++++++++++++++++--- 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/pkg/create/create.go b/pkg/create/create.go index 37c24d6..f130e2a 100644 --- a/pkg/create/create.go +++ b/pkg/create/create.go @@ -179,6 +179,36 @@ func Create(ctx context.Context, c *cli.Command) error { } if template == PythonAPI { + checkUvInstalledOutput := checkUvInstalledCommand(nil) + if command.IsError(checkUvInstalledOutput) { + yes, err := utils.PromptYesNo("uv is not installed. Do you want to install it using pipx?", nonInteractive) + if err != nil { + return cli.Exit(err, 1) + } + + checkPipxInstalledOutput := checkPipxInstalledCommand(nil) + if command.IsError(checkPipxInstalledOutput) { + log.Fatal( + "pipx is not installed, cannot automatically install uv." + + " Please install uv yourself (https://docs.astral.sh/uv/getting-started/installation)," + + " or install pipx and try again.", + ) + } + + if yes { + style.PrintInfo("Installing uv...") + + installUvOutput := installUvCommand(nil) + if command.IsError(installUvOutput) { + return cli.Exit("Failed to install uv.", 1) + } + + style.PrintSuccess("uv installed!") + } else { + return cli.Exit("uv is required for creating a new project. Please install it first.", 1) + } + } + uvSyncOutput := uvSyncCommand(projectDirectory, nil) if command.IsError(uvSyncOutput) { return cli.Exit("Failed to generate uv.lock file.", 1) @@ -329,6 +359,31 @@ func installCookiecutterCommand( ) } +func checkUvInstalledCommand( + options *command.RunOptions, +) command.Output { + return command.Run( + *exec.Command( + "uv", + "--version", + ), + options, + ) +} + +func installUvCommand( + options *command.RunOptions, +) command.Output { + return command.Run( + *exec.Command( + "pipx", + "install", + "uv", + ), + options, + ) +} + func uvSyncCommand( projectDirectory string, options *command.RunOptions, diff --git a/tests/create/run_tests.sh b/tests/create/run_tests.sh index 72d7f21..64c16aa 100755 --- a/tests/create/run_tests.sh +++ b/tests/create/run_tests.sh @@ -2,6 +2,8 @@ set -e +system_name='core' + test_create_dotnet8() { app_name='demo-api' project_dir='DemoApi' # pascal case for .NET @@ -10,7 +12,7 @@ test_create_dotnet8() { output_dir="$(mktemp -d)" if ! 3lv create \ - -s core \ + -s "$system_name" \ -a "$app_name" \ -t "$dotnet8_template_type" \ --non-interactive \ @@ -29,16 +31,56 @@ test_create_dotnet8() { exit 1 fi - # TODO: use 3lv build - if ! dotnet build "$output_dir/$project_dir"; then + if ! 3lv build \ + -s "$system_name" \ + -f "$output_dir/$project_dir/$project_dir.csproj" \ + "$app_name"; then echo "Failed to build project for template $dotnet8_template_type." exit 1 fi done } +test_create_python() { + app_name='demo-api-python' + project_dir="$app_name" + + for python_template_type in python-api; do + output_dir="$(mktemp -d)" + + if ! 3lv create \ + -s "$system_name" \ + -a "$app_name" \ + -t "$python_template_type" \ + --non-interactive \ + "$output_dir"; then + echo "Failed to create project with template $python_template_type." + exit 1 + fi + + if [[ ! -d "$output_dir/$project_dir" ]]; then + echo "Project directory does not exist for template $python_template_type." + exit 1 + fi + + if [[ ! -f "$output_dir/$project_dir/.github/workflows/build-deploy-$app_name.yml" ]]; then + echo "Workflow file does not exist for template $python_template_type." + exit 1 + fi + + if ! 3lv build \ + -s "$system_name" \ + -f "$output_dir/$project_dir/pyproject.toml" \ + "$app_name"; then + echo "Failed to build project for template $python_template_type." + exit 1 + fi + done +} + main() { test_create_dotnet8 + test_create_python echo 'All tests passed!' }