Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/credential/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func ParseTemplate(path string) ([]Entry, error) {

// BuildEnv converts resolved credentials into environment variable assignments.
func BuildEnv(resolved []Resolved, base []string) []string {
env := make([]string, len(base))
copy(env, base)
env := make([]string, 0, len(base)+len(resolved))
env = append(env, base...)
for _, r := range resolved {
env = append(env, fmt.Sprintf("%s=%s", r.EnvVar, r.Value))
}
Expand Down
33 changes: 33 additions & 0 deletions internal/credential/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"
)

Expand Down Expand Up @@ -87,3 +88,35 @@ func TestNoopProviderReportsUnavailable(t *testing.T) {
t.Fatalf("err = %v, want ErrNoopProvider", err)
}
}

func TestBuildEnvAppendsResolvedValuesWithoutMutatingBase(t *testing.T) {
t.Parallel()

base := []string{"PATH=/usr/bin", "HOME=/tmp/test"}
got := BuildEnv([]Resolved{
{
Entry: Entry{EnvVar: "GITHUB_TOKEN"},
Value: "token-1",
},
{
Entry: Entry{EnvVar: "LINEAR_API_KEY"},
Value: "token-2",
},
}, base)

want := []string{
"PATH=/usr/bin",
"HOME=/tmp/test",
"GITHUB_TOKEN=token-1",
"LINEAR_API_KEY=token-2",
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("BuildEnv() = %#v, want %#v", got, want)
}
if !reflect.DeepEqual(base, []string{"PATH=/usr/bin", "HOME=/tmp/test"}) {
t.Fatalf("BuildEnv() mutated base = %#v", base)
}
if len(got) > 0 && len(base) > 0 && &got[0] == &base[0] {
t.Fatal("BuildEnv() reused base backing array")
}
}
Loading