Skip to content

Commit 688ca47

Browse files
authored
Isolate unit tests from os.Environ (#455)
Platform resolution unit tests were affected by GOARCH/GOOS. Move the os.Environ() call out of the function under test to avoid this.
1 parent 91077c8 commit 688ca47

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

pkg/build/gobuild.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
361361
cmd := exec.CommandContext(ctx, "go", args...)
362362
cmd.Dir = dir
363363

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

403-
env := append(defaultEnv, os.Environ()...)
404+
env = append(env, userEnv...)
404405
env = append(env, configEnv...)
405406
return env, nil
406407
}

pkg/build/gobuild_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ func TestBuildEnv(t *testing.T) {
236236
tests := []struct {
237237
description string
238238
platform v1.Platform
239+
userEnv []string
239240
configEnv []string
240241
expectedEnvs map[string]string
241242
}{
@@ -260,6 +261,7 @@ func TestBuildEnv(t *testing.T) {
260261
},
261262
{
262263
description: "override an envvar and add an envvar",
264+
userEnv: []string{"CGO_ENABLED=0"},
263265
configEnv: []string{"CGO_ENABLED=1", "GOPRIVATE=git.internal.example.com,source.developers.google.com"},
264266
expectedEnvs: map[string]string{
265267
"CGO_ENABLED": "1",
@@ -291,7 +293,7 @@ func TestBuildEnv(t *testing.T) {
291293
}
292294
for _, test := range tests {
293295
t.Run(test.description, func(t *testing.T) {
294-
env, err := buildEnv(test.platform, test.configEnv)
296+
env, err := buildEnv(test.platform, test.userEnv, test.configEnv)
295297
if err != nil {
296298
t.Fatalf("unexpected error running buildEnv(): %v", err)
297299
}

0 commit comments

Comments
 (0)