Skip to content

Commit

Permalink
Merge pull request #193 from fyne-io/features/propagate-goflags-env
Browse files Browse the repository at this point in the history
Features/propagate goflags env
  • Loading branch information
Bluebugs authored Jan 28, 2024
2 parents 049e534 + fb831a1 commit 5448401
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
5 changes: 5 additions & 0 deletions internal/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
Expand Down Expand Up @@ -174,6 +175,10 @@ func makeDefaultContext(flags *CommonFlags, args []string) (Context, error) {
return ctx, err
}

if env := os.Getenv("GOFLAGS"); env != "" {
ctx.Env["GOFLAGS"] = env
}

if len(flags.Ldflags) > 0 {
goflags := ""
for _, ldflags := range strings.Fields(flags.Ldflags) {
Expand Down
4 changes: 2 additions & 2 deletions internal/command/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func AppendEnv(args []string, environs map[string]string, quoteNeeded bool) []st
for k, v := range environs {
env := k + "=" + v
if quoteNeeded && strings.Contains(v, "=") {
// engine requires to double quote the env var when value contains
// engine requires to double quote the value when it contains
// the `=` char
env = fmt.Sprintf("%q", env)
env = fmt.Sprintf("%s=%q", k, v)
}
args = append(args, "-e", env)
}
Expand Down
107 changes: 107 additions & 0 deletions internal/command/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,113 @@ func TestCmdEnginePodman(t *testing.T) {
}
}

func TestAppendEnv(t *testing.T) {
type args struct {
args []string
env map[string]string
quoteNeeded bool
}
tests := []struct {
name string
args args
wantStart []string
wantEnd [][2]string
}{
{
name: "empty",
args: args{
args: []string{},
env: map[string]string{},
quoteNeeded: true,
},
wantStart: []string{},
wantEnd: [][2]string{},
},
{
name: "quote needed",
args: args{
args: []string{},
env: map[string]string{"VAR": "value"},
quoteNeeded: true,
},
wantStart: []string{},
wantEnd: [][2]string{{"-e", "VAR=value"}},
},
{
name: "quote not needed",
args: args{
args: []string{},
env: map[string]string{"VAR": "value"},
quoteNeeded: false,
},
wantStart: []string{},
wantEnd: [][2]string{{"-e", "VAR=value"}},
},
{
name: "multiple",
args: args{
args: []string{},
env: map[string]string{"VAR": "value", "VAR2": "value2"},
quoteNeeded: true,
},
wantStart: []string{},
wantEnd: [][2]string{{"-e", "VAR=value"}, {"-e", "VAR2=value2"}},
},
{
name: "multiple with args",
args: args{
args: []string{"arg1", "arg2"},
env: map[string]string{"VAR": "value", "VAR2": "value2"},
quoteNeeded: true,
},
wantStart: []string{"arg1", "arg2"},
wantEnd: [][2]string{{"-e", "VAR=value"}, {"-e", "VAR2=value2"}},
},
{
name: "multiple with args and equal sign require quoting values",
args: args{
args: []string{"arg1", "arg2"},
env: map[string]string{"VAR": "value", "VAR2": "value2=2"},
quoteNeeded: true,
},
wantStart: []string{"arg1", "arg2"},
wantEnd: [][2]string{{"-e", "VAR=value"}, {"-e", "VAR2=\"value2=2\""}},
},
{
name: "multiple with args and equal sign do not require quoting values",
args: args{
args: []string{"arg1", "arg2"},
env: map[string]string{"VAR": "value", "VAR2": "value2=2"},
quoteNeeded: false,
},
wantStart: []string{"arg1", "arg2"},
wantEnd: [][2]string{{"-e", "VAR=value"}, {"-e", "VAR2=value2=2"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := AppendEnv(tt.args.args, tt.args.env, tt.args.quoteNeeded)
var i int
for _, v := range tt.wantStart {
assert.Equal(t, v, got[i])
i++
}
for ; i < len(got); i += 2 {
found := false
for k, v := range tt.wantEnd {
if v[0] == got[i] && v[1] == got[i+1] {
tt.wantEnd = append(tt.wantEnd[:k], tt.wantEnd[k+1:]...)
found = true
break
}
}
assert.Equal(t, true, found)
}
assert.Equal(t, 0, len(tt.wantEnd))
})
}
}

func TestMain(m *testing.M) {
os.Unsetenv("SSH_AUTH_SOCK")
os.Exit(m.Run())
Expand Down

0 comments on commit 5448401

Please sign in to comment.