forked from sonirico/go-hyperliquid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestutils.go
More file actions
74 lines (67 loc) · 1.9 KB
/
testutils.go
File metadata and controls
74 lines (67 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package hyperliquid
import (
"os"
"strings"
"github.com/joho/godotenv"
)
// loadEnvClean loads environment variables from the specified .env file(s)
// after first clearing all HL_* prefixed environment variables.
//
// This prevents issues where previously set HL_* variables (from shell or
// previous test runs) persist even when commented out or removed from .env files.
//
// In CI environments (detected by CI=true), skips clearing to preserve CI-injected credentials.
//
// Usage:
//
// loadEnvClean(".env.testnet") // Load single file
// loadEnvClean(".env.testnet", ".env.local") // Load multiple files
// loadEnvClean() // Load default .env
func loadEnvClean(filenames ...string) error {
// Skip clearing in CI - credentials come from CI environment, not .env files
// Check multiple CI indicators: GitHub Actions, GitLab CI, CircleCI, Travis, Jenkins, etc.
if !isCI() {
clearHyperliquidEnv()
}
// Load new environment from files (will be no-op in CI if files don't exist)
return godotenv.Overload(filenames...)
}
// isCI detects if running in a CI environment
func isCI() bool {
// GitHub Actions
if os.Getenv("GITHUB_ACTIONS") == "true" || os.Getenv("CI") == "true" {
return true
}
// GitLab CI
if os.Getenv("GITLAB_CI") == "true" {
return true
}
// CircleCI
if os.Getenv("CIRCLECI") == "true" {
return true
}
// Travis CI
if os.Getenv("TRAVIS") == "true" {
return true
}
// Jenkins
if os.Getenv("JENKINS_URL") != "" {
return true
}
return false
}
// clearHyperliquidEnv removes all HL_* prefixed environment variables.
// This ensures a clean slate before loading new environment configuration.
func clearHyperliquidEnv() {
for _, env := range os.Environ() {
// env format is "KEY=VALUE"
pair := strings.SplitN(env, "=", 2)
if len(pair) != 2 {
continue
}
key := pair[0]
if strings.HasPrefix(key, "HL_") {
os.Unsetenv(key)
}
}
}