-
Notifications
You must be signed in to change notification settings - Fork 42
/
patch_test.go
126 lines (103 loc) · 3.26 KB
/
patch_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
// Copyright 2013, 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package testing_test
import (
"errors"
"os"
gc "gopkg.in/check.v1"
"github.com/juju/testing"
)
type PatchValueSuite struct{}
var _ = gc.Suite(&PatchValueSuite{})
func (*PatchValueSuite) TestSetInt(c *gc.C) {
i := 99
restore := testing.PatchValue(&i, 88)
c.Assert(i, gc.Equals, 88)
restore()
c.Assert(i, gc.Equals, 99)
}
func (*PatchValueSuite) TestSetError(c *gc.C) {
oldErr := errors.New("foo")
newErr := errors.New("bar")
err := oldErr
restore := testing.PatchValue(&err, newErr)
c.Assert(err, gc.Equals, newErr)
restore()
c.Assert(err, gc.Equals, oldErr)
}
func (*PatchValueSuite) TestSetErrorToNil(c *gc.C) {
oldErr := errors.New("foo")
err := oldErr
restore := testing.PatchValue(&err, nil)
c.Assert(err, gc.Equals, nil)
restore()
c.Assert(err, gc.Equals, oldErr)
}
func (*PatchValueSuite) TestSetMapToNil(c *gc.C) {
oldMap := map[string]int{"foo": 1234}
m := oldMap
restore := testing.PatchValue(&m, nil)
c.Assert(m, gc.IsNil)
restore()
c.Assert(m, gc.DeepEquals, oldMap)
}
func (*PatchValueSuite) TestSetPanicsWhenNotAssignable(c *gc.C) {
i := 99
type otherInt int
c.Assert(func() { testing.PatchValue(&i, otherInt(88)) }, gc.PanicMatches, `reflect\.Set: value of type testing_test\.otherInt is not assignable to type int`)
}
type PatchEnvironmentSuite struct{}
var _ = gc.Suite(&PatchEnvironmentSuite{})
func (*PatchEnvironmentSuite) TestPatchEnvironment(c *gc.C) {
const envName = "TESTING_PATCH_ENVIRONMENT"
// remember the old value, and set it to something we can check
oldValue, oldValueSet := os.LookupEnv(envName)
defer func() {
if oldValueSet {
_ = os.Setenv(envName, oldValue)
} else {
_ = os.Unsetenv(envName)
}
}()
_ = os.Setenv(envName, "initial")
restore := testing.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")
restore()
c.Check(os.Getenv(envName), gc.Equals, "initial")
}
func (*PatchEnvironmentSuite) TestPatchEnvironmentWithAbsentVar(c *gc.C) {
const envName = "TESTING_PATCH_ENVIRONMENT"
// remember the old value, and unset the var
oldValue, oldValueSet := os.LookupEnv(envName)
defer func() {
if oldValueSet {
_ = os.Setenv(envName, oldValue)
}
}()
_ = os.Unsetenv(envName)
restore := testing.PatchEnvironment(envName, "new value")
c.Check(os.Getenv(envName), gc.Equals, "new value")
restore()
_, set := os.LookupEnv(envName)
c.Check(set, gc.Equals, false)
}
func (*PatchEnvironmentSuite) TestRestorerAdd(c *gc.C) {
var order []string
first := testing.Restorer(func() { order = append(order, "first") })
second := testing.Restorer(func() { order = append(order, "second") })
restore := first.Add(second)
restore()
c.Assert(order, gc.DeepEquals, []string{"second", "first"})
}
func (*PatchEnvironmentSuite) TestPatchEnvPathPrepend(c *gc.C) {
oldPath := os.Getenv("PATH")
dir := "/bin/bar"
// just in case something goes wrong
defer func() { _ = os.Setenv("PATH", oldPath) }()
restore := testing.PatchEnvPathPrepend(dir)
expect := dir + string(os.PathListSeparator) + oldPath
c.Check(os.Getenv("PATH"), gc.Equals, expect)
restore()
c.Check(os.Getenv("PATH"), gc.Equals, oldPath)
}