Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 4678a3e

Browse files
committed
mobycli: add test for comDockerCli
Signed-off-by: Nick Sieger <[email protected]>
1 parent 22d6860 commit 4678a3e

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

cli/mobycli/exec_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
package mobycli
1818

1919
import (
20+
"os"
21+
"path/filepath"
2022
"testing"
2123

2224
"gotest.tools/v3/assert"
2325

2426
"github.com/docker/compose-cli/api/context/store"
27+
"github.com/stretchr/testify/require"
2528
)
2629

2730
func TestDelegateContextTypeToMoby(t *testing.T) {
@@ -44,3 +47,44 @@ func TestDelegateContextTypeToMoby(t *testing.T) {
4447
assert.Assert(t, !mustDelegateToMoby(ctx))
4548
}
4649
}
50+
51+
func TestFindComDockerCli(t *testing.T) {
52+
tmp := t.TempDir()
53+
bin := filepath.Join(tmp, "bin")
54+
55+
// com.docker.cli on path
56+
pathFile := filepath.Join(bin, ComDockerCli)
57+
require.NoError(t, os.MkdirAll(bin, os.ModePerm))
58+
require.NoError(t, os.WriteFile(pathFile, []byte(""), os.ModePerm))
59+
60+
exe, err := os.Executable()
61+
require.NoError(t, err)
62+
exe, err = filepath.EvalSymlinks(exe)
63+
require.NoError(t, err)
64+
65+
// com.docker.cli in same directory as current executable
66+
currFile := filepath.Join(filepath.Dir(exe), ComDockerCli)
67+
require.NoError(t, os.WriteFile(currFile, []byte(""), os.ModePerm))
68+
69+
savePath := os.Getenv("PATH")
70+
t.Cleanup(func() { _ = os.Setenv("PATH", savePath) })
71+
72+
_ = os.Setenv("PATH", bin)
73+
74+
t.Run("from $DOCKER_COM_DOCKER_CLI", func(t *testing.T) {
75+
fromenv := filepath.Join(tmp, "fromenv")
76+
_ = os.Setenv("DOCKER_COM_DOCKER_CLI", fromenv)
77+
t.Cleanup(func() { _ = os.Unsetenv("DOCKER_COM_DOCKER_CLI") })
78+
79+
assert.Equal(t, fromenv, comDockerCli())
80+
})
81+
82+
t.Run("from binary next to current executable", func(t *testing.T) {
83+
assert.Equal(t, currFile, comDockerCli())
84+
})
85+
86+
t.Run("from binary on path", func(t *testing.T) {
87+
_ = os.Remove(currFile)
88+
assert.Equal(t, pathFile, comDockerCli())
89+
})
90+
}

0 commit comments

Comments
 (0)