From 2ce511a8ea3dc166e1823d4523764c13252d1d04 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Sun, 14 Sep 2025 21:00:26 -0700 Subject: [PATCH] Resolve directories passed to `flow init` --- internal/super/init.go | 34 ++++++++++++++++++++---------- internal/super/init_test.go | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 internal/super/init_test.go diff --git a/internal/super/init.go b/internal/super/init.go index 59a8cabf1..ff86ce57b 100644 --- a/internal/super/init.go +++ b/internal/super/init.go @@ -133,24 +133,36 @@ func validateCurrentDirectoryForInit() error { } // resolveTargetDirectory determines the target directory for the Flow project -// based on user input. Empty input means current directory. +// based on user input by resolving a candidate path and checking if it equals +// the current working directory. func resolveTargetDirectory(userInput string) (string, error) { - if strings.TrimSpace(userInput) == "" { - // Validate current directory for Flow project conflicts + pwd, err := os.Getwd() + if err != nil { + return "", fmt.Errorf("failed to get current working directory: %w", err) + } + + trimmed := strings.TrimSpace(userInput) + + // Build a candidate absolute path for comparison + var candidate string + if trimmed == "" { + candidate = pwd + } else if filepath.IsAbs(trimmed) { + candidate = filepath.Clean(trimmed) + } else { + candidate = filepath.Clean(filepath.Join(pwd, trimmed)) + } + + // If candidate resolves to current directory, validate and use it + if candidate == filepath.Clean(pwd) { if err := validateCurrentDirectoryForInit(); err != nil { return "", err } - - // Use current directory - pwd, err := os.Getwd() - if err != nil { - return "", fmt.Errorf("failed to get current working directory: %w", err) - } return pwd, nil } - // Use provided name to create new directory - return getTargetDirectory(userInput) + // Otherwise, use provided name/path to create or validate new directory + return getTargetDirectory(trimmed) } func updateGitignore(targetDir string, readerWriter flowkit.ReaderWriter) error { diff --git a/internal/super/init_test.go b/internal/super/init_test.go new file mode 100644 index 000000000..2f72c7ed8 --- /dev/null +++ b/internal/super/init_test.go @@ -0,0 +1,41 @@ +/* + * Flow CLI + * + * Copyright Flow Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package super + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_ResolveTargetDirectory_CurrentDirCases(t *testing.T) { + wd, err := filepath.Abs(".") + assert.NoError(t, err) + + // Case 1: Empty input should resolve to current directory + cur, err := resolveTargetDirectory("") + assert.NoError(t, err) + assert.Equal(t, filepath.Clean(wd), filepath.Clean(cur)) + + // Case 2: '.' should resolve to current directory + dot, err := resolveTargetDirectory(".") + assert.NoError(t, err) + assert.Equal(t, filepath.Clean(wd), filepath.Clean(dot)) +}