-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.go
38 lines (35 loc) · 926 Bytes
/
env.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
package bintest
import (
"fmt"
"strings"
"testing"
)
// ExpectEnv asserts that certain environment vars/values exist, otherwise
// an error is reported to T and a matching error is returned (for Before)
func ExpectEnv(t *testing.T, environ []string, expect ...string) error {
for _, e := range expect {
pair := strings.Split(e, "=")
actual, ok := GetEnv(pair[0], environ)
if !ok {
err := fmt.Errorf("Expected %s, %s wasn't set in environment", e, pair[0])
t.Error(err)
return err
}
if actual != pair[1] {
err := fmt.Errorf("Expected %s, got %q", e, actual)
t.Error(err)
return err
}
}
return nil
}
// GetEnv returns the value for a given env in the invocation
func GetEnv(key string, environ []string) (string, bool) {
for _, e := range environ {
pair := strings.Split(e, "=")
if strings.ToUpper(pair[0]) == strings.ToUpper(key) {
return pair[1], true
}
}
return "", false
}