diff --git a/cli/options.go b/cli/options.go index 989f9088..f1cfb115 100644 --- a/cli/options.go +++ b/cli/options.go @@ -391,16 +391,38 @@ func (o *ProjectOptions) GetWorkingDir() (string, error) { if o.WorkingDir != "" { return filepath.Abs(o.WorkingDir) } + + osWorkingDir, err := os.Getwd() + if err != nil { + return "", err + } + + workingDir := "" for _, path := range o.ConfigPaths { if path != "-" { absPath, err := filepath.Abs(path) if err != nil { return "", err } - return filepath.Dir(absPath), nil + + absDir := filepath.Dir(absPath) + + if absDir == osWorkingDir { + workingDir = osWorkingDir + continue + } + + if workingDir == "" { + workingDir = absDir + } } } - return os.Getwd() + + if workingDir == "" { + workingDir = osWorkingDir + } + + return workingDir, nil } func (o *ProjectOptions) GeConfigFiles() ([]types.ConfigFile, error) { diff --git a/cli/options_test.go b/cli/options_test.go index 31edf9b9..7bb5992d 100644 --- a/cli/options_test.go +++ b/cli/options_test.go @@ -353,3 +353,103 @@ func TestEnvVariablePrecedence(t *testing.T) { }) } } + +func TestGetWorkingDirWithWorkingDirFileAndSubDirFile(t *testing.T) { + wd, err := os.Getwd() + assert.NilError(t, err) + err = os.Chdir("testdata/simple") + assert.NilError(t, err) + defer os.Chdir(wd) //nolint:errcheck + + opts, err := NewProjectOptions([]string{ + "compose.yaml", + "subdirectory/compose.yaml", + }, WithName("my_project")) + assert.NilError(t, err) + currentDir, err := opts.GetWorkingDir() + assert.NilError(t, err) + assert.Equal(t, currentDir, filepath.Join(wd, "testdata", "simple")) +} + +func TestGetWorkingDirWithSubDirFileAndWorkingDirFile(t *testing.T) { + wd, err := os.Getwd() + assert.NilError(t, err) + err = os.Chdir("testdata/simple") + assert.NilError(t, err) + defer os.Chdir(wd) //nolint:errcheck + + opts, err := NewProjectOptions([]string{ + "subdirectory/compose.yaml", + "compose.yaml", + }, WithName("my_project")) + assert.NilError(t, err) + currentDir, err := opts.GetWorkingDir() + assert.NilError(t, err) + assert.Equal(t, currentDir, filepath.Join(wd, "testdata", "simple")) +} + +func TestGetWorkingDirWithOneSubDirFile(t *testing.T) { + wd, err := os.Getwd() + assert.NilError(t, err) + err = os.Chdir("testdata/simple") + assert.NilError(t, err) + defer os.Chdir(wd) //nolint:errcheck + + opts, err := NewProjectOptions([]string{ + "subdirectory/compose.yaml", + }, WithName("my_project")) + assert.NilError(t, err) + currentDir, err := opts.GetWorkingDir() + assert.NilError(t, err) + assert.Equal(t, currentDir, filepath.Join(wd, "testdata", "simple", "subdirectory")) +} + +func TestGetWorkingDirWithTwoSubDirFiles(t *testing.T) { + wd, err := os.Getwd() + assert.NilError(t, err) + err = os.Chdir("testdata/simple") + assert.NilError(t, err) + defer os.Chdir(wd) //nolint:errcheck + + opts, err := NewProjectOptions([]string{ + "subdirectory/compose.yaml", + "subdirectory/compose-other.yaml", + }, WithName("my_project")) + assert.NilError(t, err) + currentDir, err := opts.GetWorkingDir() + assert.NilError(t, err) + assert.Equal(t, currentDir, filepath.Join(wd, "testdata", "simple", "subdirectory")) +} + +func TestGetWorkingDirWithOneWorkingDirFile(t *testing.T) { + wd, err := os.Getwd() + assert.NilError(t, err) + err = os.Chdir("testdata/simple") + assert.NilError(t, err) + defer os.Chdir(wd) //nolint:errcheck + + opts, err := NewProjectOptions([]string{ + "compose.yaml", + }, WithName("my_project")) + assert.NilError(t, err) + currentDir, err := opts.GetWorkingDir() + assert.NilError(t, err) + assert.Equal(t, currentDir, filepath.Join(wd, "testdata", "simple")) +} + +func TestGetWorkingDirWithTwoWorkingDirFiles(t *testing.T) { + wd, err := os.Getwd() + assert.NilError(t, err) + err = os.Chdir("testdata/simple") + assert.NilError(t, err) + defer os.Chdir(wd) //nolint:errcheck + + opts, err := NewProjectOptions([]string{ + "compose.yaml", + "compose-other.yaml", + }, WithName("my_project")) + assert.NilError(t, err) + currentDir, err := opts.GetWorkingDir() + assert.NilError(t, err) + assert.Equal(t, currentDir, filepath.Join(wd, "testdata", "simple")) +} diff --git a/cli/testdata/simple/compose-other.yaml b/cli/testdata/simple/compose-other.yaml new file mode 100644 index 00000000..4d4a8cfd --- /dev/null +++ b/cli/testdata/simple/compose-other.yaml @@ -0,0 +1,3 @@ +services: + other: + image: nginx diff --git a/cli/testdata/simple/subdirectory/compose-other.yaml b/cli/testdata/simple/subdirectory/compose-other.yaml new file mode 100644 index 00000000..4d4a8cfd --- /dev/null +++ b/cli/testdata/simple/subdirectory/compose-other.yaml @@ -0,0 +1,3 @@ +services: + other: + image: nginx diff --git a/cli/testdata/simple/subdirectory/compose.yaml b/cli/testdata/simple/subdirectory/compose.yaml new file mode 100644 index 00000000..136dce20 --- /dev/null +++ b/cli/testdata/simple/subdirectory/compose.yaml @@ -0,0 +1,3 @@ +services: + subdirectory: + image: nginx