package e2e import ( "fmt" "os" "strings" "testing" ) // GetBinaryPath resolves the path to the wg-wrap binary. // It prioritizes the WG_WRAP_BIN environment variable. func GetBinaryPath() (string, error) { path := os.Getenv("WG_WRAP_BIN") if path == "" { return "", fmt.Errorf("WG_WRAP_BIN environment variable not set") } if _, err := os.Stat(path); err != nil { return "", fmt.Errorf("binary not found at path %s: %w", path, err) } return path, nil } // EnsureBinary returns the path to the wg-wrap binary or skips the test if it's not available. func EnsureBinary(t *testing.T) string { t.Helper() bin, err := GetBinaryPath() if err != nil { t.Skipf("skipping test: %v", err) } return bin } // SetEnvOverrides returns a new slice of environment variables with the provided overrides applied. // It ensures that overriding an existing variable replaces it rather than appending it. func SetEnvOverrides(overrides map[string]string) []string { env := os.Environ() newEnv := make([]string, 0, len(env)+len(overrides)) for _, e := range env { matched := false for k := range overrides { if strings.HasPrefix(e, k+"=") { matched = true break } } if !matched { newEnv = append(newEnv, e) } } for k, v := range overrides { newEnv = append(newEnv, fmt.Sprintf("%s=%s", k, v)) } return newEnv }