-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathoptions_test.go
98 lines (83 loc) · 2.11 KB
/
options_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package envbuilder_test
import (
"bytes"
"testing"
"github.com/coder/envbuilder"
"github.com/coder/serpent"
"github.com/stretchr/testify/require"
)
// TestEnvOptionParsing tests that given environment variables of different types are handled as expected.
func TestEnvOptionParsing(t *testing.T) {
t.Run("string", func(t *testing.T) {
const val = "setup.sh"
t.Setenv("SETUP_SCRIPT", val)
o := runCLI()
require.Equal(t, o.SetupScript, val)
})
t.Run("int", func(t *testing.T) {
t.Setenv("CACHE_TTL_DAYS", "7")
o := runCLI()
require.Equal(t, o.CacheTTLDays, int64(7))
})
t.Run("string array", func(t *testing.T) {
t.Setenv("IGNORE_PATHS", "/var,/temp")
o := runCLI()
require.Equal(t, o.IgnorePaths, []string{"/var", "/temp"})
})
t.Run("bool", func(t *testing.T) {
t.Run("lowercase", func(t *testing.T) {
t.Setenv("SKIP_REBUILD", "true")
t.Setenv("GIT_CLONE_SINGLE_BRANCH", "false")
o := runCLI()
require.True(t, o.SkipRebuild)
require.False(t, o.GitCloneSingleBranch)
})
t.Run("uppercase", func(t *testing.T) {
t.Setenv("SKIP_REBUILD", "TRUE")
t.Setenv("GIT_CLONE_SINGLE_BRANCH", "FALSE")
o := runCLI()
require.True(t, o.SkipRebuild)
require.False(t, o.GitCloneSingleBranch)
})
t.Run("numeric", func(t *testing.T) {
t.Setenv("SKIP_REBUILD", "1")
t.Setenv("GIT_CLONE_SINGLE_BRANCH", "0")
o := runCLI()
require.True(t, o.SkipRebuild)
require.False(t, o.GitCloneSingleBranch)
})
t.Run("empty", func(t *testing.T) {
t.Setenv("GIT_CLONE_SINGLE_BRANCH", "")
o := runCLI()
require.False(t, o.GitCloneSingleBranch)
})
})
}
func runCLI() envbuilder.Options {
var o envbuilder.Options
cmd := serpent.Command{
Options: o.CLI(),
Handler: func(inv *serpent.Invocation) error {
return nil
},
}
i := cmd.Invoke().WithOS()
fakeIO(i)
err := i.Run()
if err != nil {
panic("failed to run CLI: " + err.Error())
}
return o
}
type ioBufs struct {
Stdin bytes.Buffer
Stdout bytes.Buffer
Stderr bytes.Buffer
}
func fakeIO(i *serpent.Invocation) *ioBufs {
var b ioBufs
i.Stdout = &b.Stdout
i.Stderr = &b.Stderr
i.Stdin = &b.Stdin
return &b
}