Skip to content

Commit 0e4da91

Browse files
authored
Merge pull request #169 from jack-w-shaw/JUJU-2271_fix_bug_unsetting_envvars
[JUJU-2271] PatchEnvironment now correctly unsets env vars
2 parents 4ac48ac + 1327cda commit 0e4da91

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

go.mod

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
module github.com/juju/testing
22

3-
go 1.17
3+
go 1.19
44

55
require (
6-
github.com/juju/errors v0.0.0-20220203013757-bd733f3c86b9
7-
github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4
6+
github.com/juju/errors v1.0.0
7+
github.com/juju/loggo v1.0.0
88
github.com/juju/utils/v3 v3.0.0
99
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
1010
gopkg.in/yaml.v2 v2.4.0
1111
)
1212

1313
require (
14-
github.com/juju/clock v0.0.0-20220203021603-d9deb868a28a // indirect
15-
github.com/kr/pretty v0.2.1 // indirect
14+
github.com/juju/clock v1.0.2 // indirect
15+
github.com/kr/pretty v0.3.1 // indirect
1616
github.com/kr/text v0.2.0 // indirect
17-
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
18-
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
17+
github.com/rogpeppe/go-internal v1.9.0 // indirect
18+
golang.org/x/crypto v0.3.0 // indirect
19+
golang.org/x/net v0.2.0 // indirect
1920
)

go.sum

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ github.com/juju/clock v0.0.0-20190205081909-9c5c9712527c/go.mod h1:nD0vlnrUjcjJh
2121
github.com/juju/clock v0.0.0-20220202072423-1b0f830854c4/go.mod h1:zDZCPSgCJQINeZtQwHx2/cFk4seaBC8Yiqe8V82xiP0=
2222
github.com/juju/clock v0.0.0-20220203021603-d9deb868a28a h1:Az/6CM/P5guGHNy7r6TkOCctv3lDmN3W1uhku7QMupk=
2323
github.com/juju/clock v0.0.0-20220203021603-d9deb868a28a/go.mod h1:GZ/FY8Cqw3KHG6DwRVPUKbSPTAwyrU28xFi5cqZnLsc=
24+
github.com/juju/clock v1.0.2 h1:dJFdUGjtR/76l6U5WLVVI/B3i6+u3Nb9F9s1m+xxrxo=
25+
github.com/juju/clock v1.0.2/go.mod h1:HIBvJ8kiV/n7UHwKuCkdYL4l/MDECztHR2sAvWDxxf0=
2426
github.com/juju/cmd v0.0.0-20171107070456-e74f39857ca0/go.mod h1:yWJQHl73rdSX4DHVKGqkAip+huBslxRwS8m9CrOLq18=
2527
github.com/juju/cmd/v3 v3.0.0-20220202061353-b1cc80b193b0/go.mod h1:EoGJiEG+vbMwO9l+Es0SDTlaQPjH6nLcnnc4NfZB3cY=
2628
github.com/juju/collections v0.0.0-20200605021417-0d0ec82b7271/go.mod h1:5XgO71dV1JClcOJE+4dzdn4HrI5LiyKd7PlVG6eZYhY=
@@ -30,12 +32,16 @@ github.com/juju/errors v0.0.0-20200330140219-3fe23663418f/go.mod h1:W54LbzXuIE0b
3032
github.com/juju/errors v0.0.0-20210818161939-5560c4c073ff/go.mod h1:i1eL7XREII6aHpQ2gApI/v6FkVUDEBremNkcBCKYAcY=
3133
github.com/juju/errors v0.0.0-20220203013757-bd733f3c86b9 h1:EJHbsNpQyupmMeWTq7inn+5L/WZ7JfzCVPJ+DP9McCQ=
3234
github.com/juju/errors v0.0.0-20220203013757-bd733f3c86b9/go.mod h1:TRm7EVGA3mQOqSVcBySRY7a9Y1/gyVhh/WTCnc5sD4U=
35+
github.com/juju/errors v1.0.0 h1:yiq7kjCLll1BiaRuNY53MGI0+EQ3rF6GB+wvboZDefM=
36+
github.com/juju/errors v1.0.0/go.mod h1:B5x9thDqx0wIMH3+aLIMP9HjItInYWObRovoCFM5Qe8=
3337
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
3438
github.com/juju/httpprof v0.0.0-20141217160036-14bf14c30767/go.mod h1:+MaLYz4PumRkkyHYeXJ2G5g5cIW0sli2bOfpmbaMV/g=
3539
github.com/juju/loggo v0.0.0-20170605014607-8232ab8918d9/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U=
3640
github.com/juju/loggo v0.0.0-20200526014432-9ce3a2e09b5e/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U=
3741
github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4 h1:NO5tuyw++EGLnz56Q8KMyDZRwJwWO8jQnj285J3FOmY=
3842
github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4/go.mod h1:NIXFioti1SmKAlKNuUwbMenNdef59IF52+ZzuOmHYkg=
43+
github.com/juju/loggo v1.0.0 h1:Y6ZMQOGR9Aj3BGkiWx7HBbIx6zNwNkxhVNOHU2i1bl0=
44+
github.com/juju/loggo v1.0.0/go.mod h1:NIXFioti1SmKAlKNuUwbMenNdef59IF52+ZzuOmHYkg=
3945
github.com/juju/mgo/v2 v2.0.0-20210302023703-70d5d206e208/go.mod h1:0OChplkvPTZ174D2FYZXg4IB9hbEwyHkD+zT+/eK+Fg=
4046
github.com/juju/mutex v0.0.0-20171110020013-1fe2a4bf0a3a/go.mod h1:Y3oOzHH8CQ0Ppt0oCKJ2JFO81/EsWenH5AEqigLH+yY=
4147
github.com/juju/mutex/v2 v2.0.0-20220128011612-57176ebdcfa3/go.mod h1:TTCG9BJD9rCC4DZFz3jA0QvCqFDHw8Eqz0jstwY7RTQ=
@@ -65,6 +71,8 @@ github.com/julienschmidt/httprouter v1.1.1-0.20151013225520-77a895ad01eb/go.mod
6571
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
6672
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
6773
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
74+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
75+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
6876
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
6977
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
7078
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -83,7 +91,10 @@ github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c/go.mod h1:M+lRXTBq
8391
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
8492
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
8593
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
94+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
8695
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
96+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
97+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
8798
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8899
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
89100
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
@@ -96,6 +107,8 @@ golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9/go.mod h1:LzIPMQfyMNhhGPh
96107
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
97108
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
98109
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
110+
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
111+
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
99112
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
100113
golang.org/x/net v0.0.0-20180406214816-61147c48b25b/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
101114
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -107,6 +120,8 @@ golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qx
107120
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
108121
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f h1:hEYJvxw1lSnWIl8X9ofsYMklzaDs90JI2az5YMd4fPM=
109122
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
123+
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
124+
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
110125
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
111126
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
112127
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

patch.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ func PatchValue(dest, value interface{}) Restorer {
5353
// environment variable. A function is returned that will return the
5454
// environment to what it was before.
5555
func PatchEnvironment(name, value string) Restorer {
56-
oldValue := os.Getenv(name)
57-
os.Setenv(name, value)
56+
oldValue, oldValueSet := os.LookupEnv(name)
57+
_ = os.Setenv(name, value)
5858
return func() {
59-
os.Setenv(name, oldValue)
59+
if oldValueSet {
60+
_ = os.Setenv(name, oldValue)
61+
} else {
62+
_ = os.Unsetenv(name)
63+
}
6064
}
6165
}
6266

patch_test.go

+30-4
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,40 @@ var _ = gc.Suite(&PatchEnvironmentSuite{})
6565
func (*PatchEnvironmentSuite) TestPatchEnvironment(c *gc.C) {
6666
const envName = "TESTING_PATCH_ENVIRONMENT"
6767
// remember the old value, and set it to something we can check
68-
oldValue := os.Getenv(envName)
69-
os.Setenv(envName, "initial")
68+
oldValue, oldValueSet := os.LookupEnv(envName)
69+
defer func() {
70+
if oldValueSet {
71+
_ = os.Setenv(envName, oldValue)
72+
} else {
73+
_ = os.Unsetenv(envName)
74+
}
75+
}()
76+
77+
_ = os.Setenv(envName, "initial")
7078
restore := testing.PatchEnvironment(envName, "new value")
7179
// Using check to make sure the environment gets set back properly in the test.
7280
c.Check(os.Getenv(envName), gc.Equals, "new value")
7381
restore()
7482
c.Check(os.Getenv(envName), gc.Equals, "initial")
75-
os.Setenv(envName, oldValue)
83+
}
84+
85+
func (*PatchEnvironmentSuite) TestPatchEnvironmentWithAbsentVar(c *gc.C) {
86+
const envName = "TESTING_PATCH_ENVIRONMENT"
87+
// remember the old value, and unset the var
88+
oldValue, oldValueSet := os.LookupEnv(envName)
89+
defer func() {
90+
if oldValueSet {
91+
_ = os.Setenv(envName, oldValue)
92+
}
93+
}()
94+
95+
_ = os.Unsetenv(envName)
96+
restore := testing.PatchEnvironment(envName, "new value")
97+
98+
c.Check(os.Getenv(envName), gc.Equals, "new value")
99+
restore()
100+
_, set := os.LookupEnv(envName)
101+
c.Check(set, gc.Equals, false)
76102
}
77103

78104
func (*PatchEnvironmentSuite) TestRestorerAdd(c *gc.C) {
@@ -89,7 +115,7 @@ func (*PatchEnvironmentSuite) TestPatchEnvPathPrepend(c *gc.C) {
89115
dir := "/bin/bar"
90116

91117
// just in case something goes wrong
92-
defer os.Setenv("PATH", oldPath)
118+
defer func() { _ = os.Setenv("PATH", oldPath) }()
93119

94120
restore := testing.PatchEnvPathPrepend(dir)
95121

0 commit comments

Comments
 (0)