-
Notifications
You must be signed in to change notification settings - Fork 42
/
cleanup_test.go
172 lines (143 loc) · 4.6 KB
/
cleanup_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2013, 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package testing_test
import (
"os"
gc "gopkg.in/check.v1"
"github.com/juju/testing"
)
type cleanupSuite struct {
testing.CleanupSuite
}
var _ = gc.Suite(&cleanupSuite{})
func (s *cleanupSuite) TestTearDownSuiteEmpty(c *gc.C) {
// The suite stack is empty initially, check we can tear that down.
s.TearDownSuite(c)
s.SetUpSuite(c)
}
func (s *cleanupSuite) TestTearDownTestEmpty(c *gc.C) {
// The test stack is empty initially, check we can tear that down.
s.TearDownTest(c)
s.SetUpTest(c)
}
func (s *cleanupSuite) TestAddCleanup(c *gc.C) {
order := []string{}
s.AddCleanup(func(*gc.C) {
order = append(order, "first")
})
s.AddCleanup(func(*gc.C) {
order = append(order, "second")
})
s.TearDownTest(c)
c.Assert(order, gc.DeepEquals, []string{"second", "first"})
// SetUpTest resets the cleanup stack, this stops the cleanup functions
// being called again.
s.SetUpTest(c)
}
func (s *cleanupSuite) TestPatchEnvironment(c *gc.C) {
const envName = "TESTING_PATCH_ENVIRONMENT"
// remember the old value, and set it to something we can check
oldValue := os.Getenv(envName)
os.Setenv(envName, "initial")
s.PatchEnvironment(envName, "new value")
// Using check to make sure the environment gets set back properly in the test.
c.Check(os.Getenv(envName), gc.Equals, "new value")
s.TearDownTest(c)
c.Check(os.Getenv(envName), gc.Equals, "initial")
// SetUpTest resets the cleanup stack, this stops the cleanup functions
// being called again.
s.SetUpTest(c)
// explicitly return the envName to the old value
os.Setenv(envName, oldValue)
}
func (s *cleanupSuite) TestPatchValueInt(c *gc.C) {
i := 42
s.PatchValue(&i, 0)
c.Assert(i, gc.Equals, 0)
s.TearDownTest(c)
c.Assert(i, gc.Equals, 42)
// SetUpTest resets the cleanup stack, this stops the cleanup functions
// being called again.
s.SetUpTest(c)
}
func (s *cleanupSuite) TestPatchValueFunction(c *gc.C) {
function := func() string {
return "original"
}
s.PatchValue(&function, func() string {
return "patched"
})
c.Assert(function(), gc.Equals, "patched")
s.TearDownTest(c)
c.Assert(function(), gc.Equals, "original")
// SetUpTest resets the cleanup stack, this stops the cleanup functions
// being called again.
s.SetUpTest(c)
}
// noopCleanup is a simple function that does nothing that can be passed to
// AddCleanup
func noopCleanup(*gc.C) {
}
func (s cleanupSuite) TestAddCleanupPanicIfUnsafe(c *gc.C) {
// It is unsafe to call AddCleanup when the test itself is not a
// pointer receiver, because AddCleanup modifies the s.testStack
// attribute, but in a non-pointer receiver, that object is lost when
// the Test function returns.
// This Test must, itself, be a non pointer receiver to trigger this
c.Assert(func() { s.AddCleanup(noopCleanup) },
gc.PanicMatches,
"unsafe to call AddCleanup from non pointer receiver test")
}
type cleanupSuiteAndTestLifetimes struct {
}
var _ = gc.Suite(&cleanupSuiteAndTestLifetimes{})
func (s *cleanupSuiteAndTestLifetimes) TestAddCleanupBeforeSetUpSuite(c *gc.C) {
suite := &testing.CleanupSuite{}
c.Assert(func() { suite.AddCleanup(noopCleanup) },
gc.PanicMatches,
"unsafe to call AddCleanup before SetUpSuite")
suite.SetUpSuite(c)
suite.SetUpTest(c)
suite.TearDownTest(c)
suite.TearDownSuite(c)
}
func (s *cleanupSuiteAndTestLifetimes) TestAddCleanupAfterTearDownSuite(c *gc.C) {
suite := &testing.CleanupSuite{}
suite.SetUpSuite(c)
suite.SetUpTest(c)
suite.TearDownTest(c)
suite.TearDownSuite(c)
c.Assert(func() { suite.AddCleanup(noopCleanup) },
gc.PanicMatches,
"unsafe to call AddCleanup after TearDownSuite")
}
func (s *cleanupSuiteAndTestLifetimes) TestAddCleanupMixedSuiteAndTest(c *gc.C) {
calls := []string{}
suite := &testing.CleanupSuite{}
suite.SetUpSuite(c)
suite.AddCleanup(func(*gc.C) { calls = append(calls, "before SetUpTest") })
suite.SetUpTest(c)
suite.AddCleanup(func(*gc.C) { calls = append(calls, "during Test1") })
suite.TearDownTest(c)
c.Check(calls, gc.DeepEquals, []string{
"during Test1",
})
c.Assert(func() { suite.AddCleanup(noopCleanup) },
gc.PanicMatches,
"unsafe to call AddCleanup after a test has been torn down"+
" before a new test has been set up"+
" \\(Suite level changes only make sense before first test is run\\)")
suite.SetUpTest(c)
suite.AddCleanup(func(*gc.C) { calls = append(calls, "during Test2") })
suite.TearDownTest(c)
c.Check(calls, gc.DeepEquals, []string{
"during Test1",
"during Test2",
})
suite.TearDownSuite(c)
c.Check(calls, gc.DeepEquals, []string{
"during Test1",
"during Test2",
"before SetUpTest",
})
}