-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.go
More file actions
69 lines (56 loc) · 1.87 KB
/
Copy pathenv.go
File metadata and controls
69 lines (56 loc) · 1.87 KB
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
/*
Golang test helper library: sztest.
Copyright (C) 2023-2025 Leslie Dancsecs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package sztest
import (
"os"
)
// SetEnv sets or updates the named environment variable to the given value.
// Changes are reverted automatically when chk.Release() is called. Any error
// encountered is reported to the underlying *testingT.
func (chk *Chk) SetEnv(name, value string) {
var reverseFunc func() error
chk.T().Helper()
currentValue, found := os.LookupEnv(name)
if found {
reverseFunc = func() error {
return os.Setenv(name, currentValue)
}
} else {
reverseFunc = func() error {
return os.Unsetenv(name)
}
}
chk.PushPreReleaseFunc(reverseFunc)
chk.NoErr(os.Setenv(name, value))
}
// DelEnv removes the named environment variable if it exists. The removal is
// reverted automatically when chk.Release() is called. Any error encountered
// is reported to the underlying *testingT.
func (chk *Chk) DelEnv(name string) {
var reverseFunc func() error
chk.T().Helper()
currentValue, found := os.LookupEnv(name)
if found {
reverseFunc = func() error {
return os.Setenv(name, currentValue)
}
} else {
reverseFunc = func() error {
return os.Unsetenv(name)
}
}
chk.PushPreReleaseFunc(reverseFunc)
chk.NoErr(os.Unsetenv(name))
}