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
26 changes: 24 additions & 2 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
100 changes: 100 additions & 0 deletions cli/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
3 changes: 3 additions & 0 deletions cli/testdata/simple/compose-other.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
other:
image: nginx
3 changes: 3 additions & 0 deletions cli/testdata/simple/subdirectory/compose-other.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
other:
image: nginx
3 changes: 3 additions & 0 deletions cli/testdata/simple/subdirectory/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
subdirectory:
image: nginx