-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
69 lines (64 loc) · 1.79 KB
/
env_test.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
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
package clo
import (
"github.com/boggydigital/testo"
"os"
"strconv"
"strings"
"testing"
)
func TestArgEnv(t *testing.T) {
tests := []struct {
app, cmd, arg string
env string
}{
{"p", "c", "", ""},
{"", "", "a", "A"},
{"", "c", "a", "C_A"},
{"p", "c", "a", "P_C_A"},
}
for ii, tt := range tests {
t.Run(strconv.Itoa(ii), func(t *testing.T) {
testo.EqualValues(t, argEnv(tt.app, tt.cmd, tt.arg), tt.env)
})
}
}
func TestReadEnvArg(t *testing.T) {
tests := []struct {
cmd, arg string
expError bool
}{
{"cmd-that-doesnt-exist", "arg-that-doesnt-exist", true},
{"command1", "argument1", false},
}
for _, tt := range tests {
t.Run(tt.cmd+tt.arg, func(t *testing.T) {
req := request{}
testo.Error(t, req.readEnvArgs(nil), true)
// setup - make sure argument1 expects value from env
defs := mockDefinitions()
// request arguments should use argument1 with 0 values
req.Command = tt.cmd
req.Arguments = map[string][]string{
tt.arg: {},
}
// trivial validation that we're starting from an empty value
testo.EqualValues(t, len(req.Arguments[tt.arg]), 0)
// store existing value of the env. variable
envToken := argEnv(appName(), tt.cmd, tt.arg)
envValue := strings.ToLower(envToken)
storedEnv := os.Getenv(envToken)
// set the value we'll expect to see as argument1 value
testo.Error(t, os.Setenv(envToken, envValue), false)
// read empty arguments values from env
testo.Error(t, req.readEnvArgs(defs), tt.expError)
// there should be a value we got from env. variable
expArgValLen := 1
if tt.expError {
expArgValLen = 0
}
testo.EqualValues(t, len(req.Arguments[tt.arg]), expArgValLen)
// reset env. variable value to original stored value
testo.Error(t, os.Setenv(envToken, storedEnv), false)
})
}
}