Skip to content

Commit

Permalink
fix: some cases on windows (#170)
Browse files Browse the repository at this point in the history
Co-authored-by: Ori Shoshan <ori@wing.security>
  • Loading branch information
caarlos0 and Ori Shoshan authored May 15, 2021
1 parent d552093 commit 0397ad7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
9 changes: 0 additions & 9 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,6 @@ func configure(opts []Options) []Options {
return []Options{opt}
}

func toMap(env []string) map[string]string {
r := map[string]string{}
for _, e := range env {
p := strings.SplitN(e, "=", 2)
r[p[0]] = p[1]
}
return r
}

// getTagName returns the tag name.
func getTagName(opts []Options) string {
return opts[0].TagName
Expand Down
14 changes: 14 additions & 0 deletions env_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package env

import "strings"

func toMap(env []string) map[string]string {
r := map[string]string{}
for _, e := range env {
p := strings.SplitN(e, "=", 2)
r[p[0]] = p[1]
}
return r
}
25 changes: 25 additions & 0 deletions env_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package env

import "strings"

func toMap(env []string) map[string]string {
r := map[string]string{}
for _, e := range env {
p := strings.SplitN(e, "=", 2)

// On Windows, environment variables can start with '='. If so, Split at next character.
// See env_windows.go in the Go source: https://github.com/golang/go/blob/master/src/syscall/env_windows.go#L58
prefixEqualSign := false
if len(e) > 0 && e[0] == '=' {
e = e[1:]
prefixEqualSign = true
}
p = strings.SplitN(e, "=", 2)
if prefixEqualSign {
p[0] = "=" + p[0]
}

r[p[0]] = p[1]
}
return r
}
19 changes: 19 additions & 0 deletions env_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package env

import (
"testing"

"github.com/stretchr/testify/require"
)

// On Windows, environment variables can start with '='. This test verifies this behavior without relying on a Windows environment.
// See env_windows.go in the Go source: https://github.com/golang/go/blob/master/src/syscall/env_windows.go#L58
func TestToMapWindows(t *testing.T) {
envVars := []string{"=::=::\\", "=C:=C:\\test", "VAR=REGULARVAR"}
result := toMap(envVars)
require.Equal(t, map[string]string{
"=::": "::\\",
"=C:": "C:\\test",
"VAR": "REGULARVAR",
}, result)
}

0 comments on commit 0397ad7

Please sign in to comment.