t.Setenv
and friends instead of os.Setenv
for test file(s) GO-W1032 86 Context("when FLY_HOME is unset and HOME is set", func() {
87 BeforeEach(func() {
88 os.Setenv("FLY_HOME", "")
89 os.Setenv("HOME", tmpDir) 90 })
91 It("loads from the path set in HOME", func() {
92 targets, err := rc.LoadTargets()
85
86 Context("when FLY_HOME is unset and HOME is set", func() {
87 BeforeEach(func() {
88 os.Setenv("FLY_HOME", "") 89 os.Setenv("HOME", tmpDir)
90 })
91 It("loads from the path set in HOME", func() {
74 Context("when FLY_HOME is set", func() {
75 BeforeEach(func() {
76 os.Setenv("FLY_HOME", tmpDir)
77 os.Setenv("HOME", "/an/invalid/path") 78 })
79 It("loads from the path set in FLY_HOME in preference to HOME", func() {
80 targets, err := rc.LoadTargets()
73
74 Context("when FLY_HOME is set", func() {
75 BeforeEach(func() {
76 os.Setenv("FLY_HOME", tmpDir) 77 os.Setenv("HOME", "/an/invalid/path")
78 })
79 It("loads from the path set in FLY_HOME in preference to HOME", func() {
20 tmpDir, err = os.MkdirTemp("", "fly-test")
21 Expect(err).ToNot(HaveOccurred())
22
23 os.Setenv("HOME", tmpDir) 24
25 flyrc = filepath.Join(userHomeDir(), ".flyrc")
26 })
For tests, environment variables are sometimes temporarily set. It is also essential and
even a good practice to unset those temporary values as they might interfere with others
trying to use the same environment variable. It is recommended that for *_test.go
file(s)
one should use Setenv
method from the "testing" package as it unsets the same after the
test has concluded. Following Setenv
calls os.Setenv(key, value)
and uses Cleanup
to
restore the environment variable to its original value after the test.
func setup() {
os.Setenv("a", "b")
}
func TestF(t *testing.T) {
os.Setenv("a", "b")
}
func TB(tb testing.TB) {
os.Setenv("a", "b")
}
func FuzzF(f *testing.F) {
os.Setenv("a", "b")
}
func setup() {
testing.Setenv("a", "b")
}
func TestF(t *testing.T) {
t.Setenv("a", "b")
}
func TB(tb testing.TB) {
tb.Setenv("a", "b")
}
func FuzzF(f *testing.F) {
f.Setenv("a", "b")
}