File: temp_env.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (28 lines) | stat: -rw-r--r-- 719 bytes parent folder | download | duplicates (2)
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
package testutil

import "os"

// Setenv sets the value of an environment variable for the duration of a test.
// It returns value.
func Setenv(c Cleanuper, name, value string) string {
	SaveEnv(c, name)
	os.Setenv(name, value)
	return value
}

// Setenv unsets an environment variable for the duration of a test.
func Unsetenv(c Cleanuper, name string) {
	SaveEnv(c, name)
	os.Unsetenv(name)
}

// SaveEnv saves the current value of an environment variable so that it will be
// restored after a test has finished.
func SaveEnv(c Cleanuper, name string) {
	oldValue, existed := os.LookupEnv(name)
	if existed {
		c.Cleanup(func() { os.Setenv(name, oldValue) })
	} else {
		c.Cleanup(func() { os.Unsetenv(name) })
	}
}