-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
64 lines (51 loc) · 1.21 KB
/
main_test.go
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
package main
import (
"os"
"testing"
)
const (
envProd = "production_mode"
envDebug = "debug_mode"
envBuild = "__BUILD_MODE__"
)
func resetEnv() {
// Unset all environment variables
os.Unsetenv(envProd)
os.Unsetenv(envDebug)
os.Unsetenv(envBuild)
}
func TestMain(m *testing.M) {
// Run all tests, unset env variables, and exit
resetEnv()
val := m.Run()
resetEnv()
os.Exit(val)
}
func TestMainMethod(_ *testing.T) {
main()
}
func dirtyCheck(t *testing.T, val01, val02 bool) {
t.Helper()
if val01 != val02 {
t.Errorf("Values don't match: (%v, %v)", val01, val02)
}
}
func TestFirst(t *testing.T) {
resetEnv()
dirtyCheck(t, firstCheck(), false) // no variable should be detected
os.Setenv(envProd, "production")
dirtyCheck(t, firstCheck(), true) // detect `production` mode
resetEnv()
os.Setenv(envDebug, "debug")
dirtyCheck(t, firstCheck(), true) // detect `debug` mode
}
func TestSecond(t *testing.T) {
resetEnv()
dirtyCheck(t, secondCheck(), false) // no variable should be detected
resetEnv()
os.Setenv(envBuild, "production")
dirtyCheck(t, secondCheck(), true) // detect `production` mode!
resetEnv()
os.Setenv(envBuild, "debug")
dirtyCheck(t, secondCheck(), true) // detect `debug` mode!
}