mirror of
https://github.com/ko-build/ko.git
synced 2025-03-17 20:47:51 +02:00
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.
This commit is contained in:
parent
91077c8c5e
commit
688ca47675
@ -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)
|
||||
}
|
||||
@ -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,
|
||||
@ -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
|
||||
}
|
||||
|
@ -236,6 +236,7 @@ func TestBuildEnv(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
platform v1.Platform
|
||||
userEnv []string
|
||||
configEnv []string
|
||||
expectedEnvs map[string]string
|
||||
}{
|
||||
@ -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",
|
||||
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user