Skip to content
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
32 changes: 32 additions & 0 deletions pkg/configtest/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package configtest

import (
"testing"
)

// snykAPIKey is the AutomaticEnv name for configuration.API_URL (viper key "snyk_api").
// Keep aligned with pkg/configuration/constants.go.
const snykAPIKey = "SNYK_API"

// KnownLeakEnvironmentKeys is cleared when [IsolateEnvironmentForTest] is called with no arguments.
var KnownLeakEnvironmentKeys = []string{
snykAPIKey,
}
Comment thread
robertolopezlopez marked this conversation as resolved.

// IsolateEnvironmentForTest clears environment variables for a test using t.Setenv(k, "").
// If no keys are provided, it clears the variables listed in [KnownLeakEnvironmentKeys].
// If explicit keys are provided, they OVERRIDE the default behavior: only the specified keys are cleared,
// and they are NOT merged with [KnownLeakEnvironmentKeys]. Empty keys are skipped.
func IsolateEnvironmentForTest(t *testing.T, keys ...string) {
t.Helper()
toClear := keys
if len(toClear) == 0 {
toClear = KnownLeakEnvironmentKeys
}
Comment on lines +20 to +25
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we ALWAYS consider ignoring the KnownLeakEnvironmentKeys values?
If so, we should concat always.
In the current way, if parameters for keys are passed, the KnownLeakEnvironmentKeys are not being considered

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this behavior has been documented in my last commit

for _, k := range toClear {
if k == "" {
continue
}
t.Setenv(k, "")
}
Comment thread
robertolopezlopez marked this conversation as resolved.
}
3 changes: 3 additions & 0 deletions pkg/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/patrickmn/go-cache"
"github.com/snyk/go-application-framework/pkg/configtest"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -269,6 +270,8 @@ func Test_ConfigurationGet_Url(t *testing.T) {
}

func Test_ConfigurationGet_StringSlice(t *testing.T) {
configtest.IsolateEnvironmentForTest(t)

config := New()

expectedDefault := []string{}
Expand Down
2 changes: 2 additions & 0 deletions pkg/local_workflows/report_analytics_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/snyk/go-application-framework/pkg/analytics"
"github.com/snyk/go-application-framework/pkg/configtest"
"github.com/snyk/go-application-framework/pkg/configuration"
testutils "github.com/snyk/go-application-framework/pkg/local_workflows/test_utils"
"github.com/snyk/go-application-framework/pkg/mocks"
Expand Down Expand Up @@ -320,6 +321,7 @@ func testInitReportAnalyticsWorkflow(ctrl *gomock.Controller) error {

func testGetMockHTTPClient(t *testing.T, orgId string, requestPayload string) *http.Client {
t.Helper()
configtest.IsolateEnvironmentForTest(t)
mockClient := testutils.NewTestClient(func(req *http.Request) *http.Response {
// Test request parameters
require.Equal(t, "/hidden/orgs/"+orgId+"/analytics?version=2024-03-07~experimental", req.URL.String())
Expand Down
3 changes: 3 additions & 0 deletions pkg/local_workflows/whoami_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"

"github.com/snyk/go-application-framework/pkg/configtest"
"github.com/snyk/go-application-framework/pkg/configuration"
testutils "github.com/snyk/go-application-framework/pkg/local_workflows/test_utils"
"github.com/snyk/go-application-framework/pkg/mocks"
Expand Down Expand Up @@ -78,6 +79,8 @@ func setupMockContext(t *testing.T, payload string, json bool, statusCode int) *
// This method is a helper
t.Helper()

configtest.IsolateEnvironmentForTest(t)

// setup
logger := zerolog.Logger{}
config := configuration.New()
Expand Down
3 changes: 3 additions & 0 deletions pkg/networking/networking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/snyk/error-catalog-golang-public/cli"
"github.com/snyk/error-catalog-golang-public/snyk"
"github.com/snyk/error-catalog-golang-public/snyk_errors"
"github.com/snyk/go-application-framework/pkg/configtest"
"github.com/snyk/go-httpauth/pkg/httpauth"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -449,6 +450,8 @@ func Test_UserAgentInfo_Complete(t *testing.T) {
}

func TestNetworkImpl_Clone(t *testing.T) {
configtest.IsolateEnvironmentForTest(t)

config := configuration.NewWithOpts(configuration.WithAutomaticEnv())
network := NewNetworkAccess(config)

Expand Down