forked from onsi/gomega
/
env_test.go
81 lines (64 loc) · 1.71 KB
/
env_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package defaults_test
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
d "github.com/onsi/gomega/internal/defaults"
)
var _ = Describe("Durations", func() {
var (
duration *time.Duration
envVarGot string
envVarToReturn string
getDurationFromEnv = func(name string) string {
envVarGot = name
return envVarToReturn
}
setDuration = func(t time.Duration) {
duration = &t
}
setDurationCalled = func() bool {
return duration != nil
}
resetDuration = func() {
duration = nil
}
)
BeforeEach(func() {
resetDuration()
})
Context("When the environment has a duration", func() {
Context("When the duration is valid", func() {
BeforeEach(func() {
envVarToReturn = "10m"
d.SetDurationFromEnv(getDurationFromEnv, setDuration, "MY_ENV_VAR")
})
It("sets the duration", func() {
Expect(envVarGot).To(Equal("MY_ENV_VAR"))
Expect(setDurationCalled()).To(Equal(true))
Expect(*duration).To(Equal(10 * time.Minute))
})
})
Context("When the duration is not valid", func() {
BeforeEach(func() {
envVarToReturn = "10"
})
It("panics with a helpful error message", func() {
Expect(func() {
d.SetDurationFromEnv(getDurationFromEnv, setDuration, "MY_ENV_VAR")
}).To(PanicWith(MatchRegexp("Expected a duration when using MY_ENV_VAR")))
})
})
})
Context("When the environment does not have a duration", func() {
BeforeEach(func() {
envVarToReturn = ""
d.SetDurationFromEnv(getDurationFromEnv, setDuration, "MY_ENV_VAR")
})
It("does not set the duration", func() {
Expect(envVarGot).To(Equal("MY_ENV_VAR"))
Expect(setDurationCalled()).To(Equal(false))
Expect(duration).To(BeNil())
})
})
})