Skip to content
Merged
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
29 changes: 29 additions & 0 deletions pkg/backends/jmeter/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package jmeter

import (
"context"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -238,6 +240,7 @@ func TestSetDefaults(t *testing.T) {
})

t.Run("No default", func(t *testing.T) {
ClearEnv(t)
cfg := Config{}
envconfig.MustProcess("", &cfg)
jmeter := &Backend{
Expand All @@ -251,3 +254,29 @@ func TestSetDefaults(t *testing.T) {
assert.Equal(t, jmeter.workerConfig.Tag, defaultWorkerImageTag)
})
}

// ClearEnv clears all environment variables for the duration of the test
// and restores them to their original state automatically when the test finishes.
func ClearEnv(t *testing.T) {
// 1. Snapshot the current environment
originalEnv := os.Environ()

// 2. Clear the environment immediately
os.Clearenv()

// 3. Register a cleanup function to restore state
t.Cleanup(func() {
// Clear again to ensure variables added during the test are removed
os.Clearenv()

// Restore the original variables
for _, pair := range originalEnv {
// Environment variables are formatted as "KEY=VALUE"
// We split by the first "=" only
parts := strings.SplitN(pair, "=", 2)
if len(parts) == 2 {
os.Setenv(parts[0], parts[1])
}
}
})
}
Loading