From 6619f72b0736292c6a8735683f64b42b92987015 Mon Sep 17 00:00:00 2001 From: Aditya Sirish <8928778+adityasaky@users.noreply.github.com> Date: Thu, 10 Oct 2024 04:08:17 -0400 Subject: [PATCH] Fix gitsign env test (#568) 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 --- pkg/version/version.go | 21 ++++++++++++++------- pkg/version/version_test.go | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/pkg/version/version.go b/pkg/version/version.go index 386e0cb4..ef710703 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -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 { @@ -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 } } } diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go index ac0fd972..71d4c9aa 100644 --- a/pkg/version/version_test.go +++ b/pkg/version/version_test.go @@ -17,6 +17,7 @@ package version import ( "os" + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -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") @@ -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) + } }