Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gitsign env test #568

Merged
merged 1 commit into from
Oct 10, 2024
Merged
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
21 changes: 14 additions & 7 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ var (
// Output of "git describe". The prerequisite is that the
// branch should be tagged using the correct versioning strategy.
gitVersion = "devel"

envVarPrefixes = []string{
"GITSIGN_",
// Can modify Sigstore/TUF client behavior - https://github.com/sigstore/sigstore/blob/35d6a82c15183f7fe7a07eca45e17e378aa32126/pkg/tuf/client.go#L52
"SIGSTORE_",
"TUF_",
}
)

type Info struct {
Expand Down Expand Up @@ -62,15 +69,15 @@ func getGitsignEnv() []string {
for _, e := range os.Environ() {
// Prefixes to look for. err on the side of showing too much rather
// than too little. We'll only output things that have values set.
for _, prefix := range []string{
"GITSIGN_",
// Can modify Sigstore/TUF client behavior - https://github.com/sigstore/sigstore/blob/35d6a82c15183f7fe7a07eca45e17e378aa32126/pkg/tuf/client.go#L52
"SIGSTORE_",
"TUF_",
} {
for _, prefix := range envVarPrefixes {
if strings.HasPrefix(e, prefix) {
eComponents := strings.Split(strings.TrimSpace(e), "=")
if len(eComponents) == 1 || len(eComponents[1]) == 0 {
// The variable is set to nothing
// eg: SIGSTORE_ROOT_FILE=
continue
}
out = append(out, e)
continue
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package version

import (
"os"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -30,6 +31,15 @@ func TestVersionText(t *testing.T) {
}

func TestEnv(t *testing.T) {
for _, envVar := range os.Environ() {
for _, prefix := range envVarPrefixes {
if strings.HasPrefix(envVar, prefix) {
t.Setenv(strings.Split(envVar, "=")[0], "") // t.Setenv restores value during cleanup
break
}
}
}

os.Setenv("GITSIGN_CONNECTOR_ID", "foobar")
os.Setenv("GITSIGN_TEST", "foo")
os.Setenv("TUF_ROOT", "bar")
Expand All @@ -43,4 +53,12 @@ func TestEnv(t *testing.T) {
if diff := cmp.Diff(got.Env, want); diff != "" {
t.Error(diff)
}

// want doesn't change because the variable is set to nothing and must be
// ignored
os.Setenv("SIGSTORE_ROOT_FILE", "")
got = GetVersionInfo()
if diff := cmp.Diff(got.Env, want); diff != "" {
t.Error(diff)
}
}
Loading