Skip to content

Commit

Permalink
Fix gitsign env test (#568)
Browse files Browse the repository at this point in the history
The env test fails if the testing environment has other sigstore / gitsign
related env vars set. This commit unsets them for the duration of the test.

Signed-off-by: Aditya Sirish A Yelgundhalli <[email protected]>
  • Loading branch information
adityasaky authored Oct 10, 2024
1 parent 512c386 commit 6619f72
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
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)
}
}

0 comments on commit 6619f72

Please sign in to comment.