...
1
16
17 package main
18
19 import (
20 "os"
21 "testing"
22 )
23
24 func TestEnv(t *testing.T) {
25 testCases := []struct {
26 desc string
27 preHook func()
28 env Getenver
29 expect map[string]string
30 }{
31 {
32 desc: "OS env",
33 env: &osEnv{},
34 preHook: func() {
35 t.Setenv("key1", "1")
36 },
37 expect: map[string]string{"key1": "1"},
38 }, {
39 desc: "OS env falls defaults to empty",
40 env: &osEnv{},
41 preHook: func() {
42 os.Unsetenv("key1")
43 },
44 expect: map[string]string{"key1": ""},
45 }, {
46 desc: "First choice of env respected",
47 env: &defaultEnver{
48 firstChoice: &explicitEnv{
49 vals: map[string]string{
50 "key1": "1",
51 },
52 },
53 defaults: map[string]string{
54 "key1": "default1",
55 "key2": "default2",
56 },
57 },
58 expect: map[string]string{
59 "key1": "1",
60 "key2": "default2",
61 },
62 },
63 }
64 for _, tc := range testCases {
65 t.Run(tc.desc, func(t *testing.T) {
66 for k, expectVal := range tc.expect {
67 if tc.preHook != nil {
68 tc.preHook()
69 }
70 val := tc.env.Getenv(k)
71 if val != expectVal {
72 t.Errorf("Expected %q but got %q", expectVal, val)
73 }
74 }
75 })
76 }
77 }
78
View as plain text