Skip to content

Commit

Permalink
Isolate unit tests from os.Environ (#455)
Browse files Browse the repository at this point in the history
Platform resolution unit tests were affected by GOARCH/GOOS. Move the
os.Environ() call out of the function under test to avoid this.
  • Loading branch information
jonjohnsonjr authored Sep 29, 2021
1 parent 91077c8 commit 688ca47
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 6 additions & 5 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
cmd := exec.CommandContext(ctx, "go", args...)
cmd.Dir = dir

env, err := buildEnv(platform, config.Env)
env, err := buildEnv(platform, os.Environ(), config.Env)
if err != nil {
return "", fmt.Errorf("could not create env for %s: %v", ip, err)
}
Expand All @@ -383,8 +383,9 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
// buildEnv creates the environment variables used by the `go build` command.
// From `os/exec.Cmd`: If Env contains duplicate environment keys, only the last
// value in the slice for each duplicate key is used.
func buildEnv(platform v1.Platform, configEnv []string) ([]string, error) {
defaultEnv := []string{
func buildEnv(platform v1.Platform, userEnv, configEnv []string) ([]string, error) {
// Default env
env := []string{
"CGO_ENABLED=0",
"GOOS=" + platform.OS,
"GOARCH=" + platform.Architecture,
Expand All @@ -396,11 +397,11 @@ func buildEnv(platform v1.Platform, configEnv []string) ([]string, error) {
return nil, fmt.Errorf("goarm failure: %v", err)
}
if goarm != "" {
defaultEnv = append(defaultEnv, "GOARM="+goarm)
env = append(env, "GOARM="+goarm)
}
}

env := append(defaultEnv, os.Environ()...)
env = append(env, userEnv...)
env = append(env, configEnv...)
return env, nil
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func TestBuildEnv(t *testing.T) {
tests := []struct {
description string
platform v1.Platform
userEnv []string
configEnv []string
expectedEnvs map[string]string
}{
Expand All @@ -260,6 +261,7 @@ func TestBuildEnv(t *testing.T) {
},
{
description: "override an envvar and add an envvar",
userEnv: []string{"CGO_ENABLED=0"},
configEnv: []string{"CGO_ENABLED=1", "GOPRIVATE=git.internal.example.com,source.developers.google.com"},
expectedEnvs: map[string]string{
"CGO_ENABLED": "1",
Expand Down Expand Up @@ -291,7 +293,7 @@ func TestBuildEnv(t *testing.T) {
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
env, err := buildEnv(test.platform, test.configEnv)
env, err := buildEnv(test.platform, test.userEnv, test.configEnv)
if err != nil {
t.Fatalf("unexpected error running buildEnv(): %v", err)
}
Expand Down

0 comments on commit 688ca47

Please sign in to comment.