Skip to content

Commit 2c4d9ea

Browse files
committed
test: make credential helper tests cross-platform for windows
1 parent ac1ac95 commit 2c4d9ea

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

pkg/environment/credential_helper_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package environment
22

33
import (
4+
"runtime"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -17,6 +18,19 @@ func TestNewCredentialHelperProvider(t *testing.T) {
1718
func TestCredentialHelperProvider_Get(t *testing.T) {
1819
t.Parallel()
1920

21+
echoCmd := "echo"
22+
echoArgs := func(v string) []string { return []string{v} }
23+
falseCmd := "false"
24+
25+
if runtime.GOOS == "windows" {
26+
echoCmd = "powershell"
27+
echoArgs = func(v string) []string {
28+
return []string{"-NoProfile", "-Command", "Write-Output '" + v + "'"}
29+
}
30+
falseCmd = "powershell"
31+
// simulate 'false' by exiting with 1
32+
}
33+
2034
tests := []struct {
2135
name string
2236
command string
@@ -25,11 +39,11 @@ func TestCredentialHelperProvider_Get(t *testing.T) {
2539
wantValue string
2640
wantFound bool
2741
}{
28-
{"ignores non-DOCKER_TOKEN vars", "echo", []string{"test-token"}, "OTHER_VAR", "", false},
29-
{"success", "echo", []string{"my-secret-token"}, DockerDesktopTokenEnv, "my-secret-token", true},
30-
{"trims whitespace", "echo", []string{" token-with-spaces "}, DockerDesktopTokenEnv, "token-with-spaces", true},
31-
{"empty output", "echo", []string{""}, DockerDesktopTokenEnv, "", false},
32-
{"command fails", "false", nil, DockerDesktopTokenEnv, "", false},
42+
{"ignores non-DOCKER_TOKEN vars", echoCmd, echoArgs("test-token"), "OTHER_VAR", "", false},
43+
{"success", echoCmd, echoArgs("my-secret-token"), DockerDesktopTokenEnv, "my-secret-token", true},
44+
{"trims whitespace", echoCmd, echoArgs(" token-with-spaces "), DockerDesktopTokenEnv, "token-with-spaces", true},
45+
{"empty output", echoCmd, echoArgs(""), DockerDesktopTokenEnv, "", false},
46+
{"command fails", falseCmd, []string{"-NoProfile", "-Command", "exit 1"}, DockerDesktopTokenEnv, "", false},
3347
{"command not found", "nonexistent-command-12345", nil, DockerDesktopTokenEnv, "", false},
3448
}
3549

0 commit comments

Comments
 (0)