-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_test.go
More file actions
96 lines (81 loc) · 2.33 KB
/
app_test.go
File metadata and controls
96 lines (81 loc) · 2.33 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package env
import (
"os"
"testing"
)
func TestIsTestSuffixFromArguments(t *testing.T) {
original := os.Args
defer func() { os.Args = original }()
os.Args = []string{"cmd", "example.test"}
if !isTestSuffixFromArguments() {
t.Fatalf("expected true when args include .test")
}
os.Args = []string{"cmd", "run"}
if isTestSuffixFromArguments() {
t.Fatalf("expected false when no test args present")
}
}
func TestAppEnvHelpers(t *testing.T) {
t.Cleanup(func() { _ = os.Unsetenv("APP_ENV") })
_ = os.Setenv("APP_ENV", Staging)
if got := GetAppEnv(); got != Staging {
t.Fatalf("GetAppEnv mismatch: %s", got)
}
if !IsAppEnv(Staging, Production) {
t.Fatalf("IsAppEnv should match staging")
}
if IsAppEnvLocal() || IsAppEnvTestingOrLocal() {
t.Fatalf("unexpected local/testing match")
}
_ = os.Setenv("APP_ENV", Production)
if !IsAppEnvProduction() {
t.Fatalf("IsAppEnvProduction should be true")
}
_ = os.Setenv("APP_ENV", Local)
if !IsAppEnvLocalOrStaging() {
t.Fatalf("IsAppEnvLocalOrStaging should be true for local")
}
_ = os.Setenv("APP_ENV", Staging)
if !IsAppEnvStaging() {
t.Fatalf("IsAppEnvStaging should be true")
}
}
func TestSetAppEnv(t *testing.T) {
t.Cleanup(func() { _ = os.Unsetenv("APP_ENV") })
if err := SetAppEnv(Staging); err != nil {
t.Fatalf("SetAppEnv: %v", err)
}
if got := os.Getenv("APP_ENV"); got != Staging {
t.Fatalf("expected APP_ENV=%s, got %q", Staging, got)
}
_ = os.Setenv("APP_ENV", Local)
if err := SetAppEnv("invalid"); err == nil {
t.Fatalf("expected error for invalid APP_ENV")
}
if got := os.Getenv("APP_ENV"); got != Local {
t.Fatalf("expected APP_ENV to remain %q, got %q", Local, got)
}
}
func TestSetAppEnvHelpers(t *testing.T) {
t.Cleanup(func() { _ = os.Unsetenv("APP_ENV") })
cases := []struct {
name string
setter func() error
want string
}{
{name: "local", setter: SetAppEnvLocal, want: Local},
{name: "staging", setter: SetAppEnvStaging, want: Staging},
{name: "production", setter: SetAppEnvProduction, want: Production},
{name: "testing", setter: SetAppEnvTesting, want: Testing},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if err := tc.setter(); err != nil {
t.Fatalf("setter: %v", err)
}
if got := os.Getenv("APP_ENV"); got != tc.want {
t.Fatalf("expected APP_ENV=%s, got %q", tc.want, got)
}
})
}
}