-
Notifications
You must be signed in to change notification settings - Fork 273
/
args_test.go
84 lines (73 loc) · 2.14 KB
/
args_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
package kingpin
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestArgRemainder(t *testing.T) {
app := New("test", "")
v := app.Arg("test", "").Strings()
args := []string{"hello", "world"}
_, err := app.Parse(args)
assert.NoError(t, err)
assert.Equal(t, args, *v)
}
func TestArgRemainderErrorsWhenNotLast(t *testing.T) {
a := newArgGroup()
a.Arg("test", "").Strings()
a.Arg("test2", "").String()
assert.Error(t, a.init())
}
func TestArgMultipleRequired(t *testing.T) {
terminated := false
app := New("test", "")
app.Version("0.0.0").Writer(ioutil.Discard)
app.Arg("a", "").Required().String()
app.Arg("b", "").Required().String()
app.Terminate(func(int) { terminated = true })
_, err := app.Parse([]string{})
assert.Error(t, err)
_, err = app.Parse([]string{"A"})
assert.Error(t, err)
_, err = app.Parse([]string{"A", "B"})
assert.NoError(t, err)
_, _ = app.Parse([]string{"--version"})
assert.True(t, terminated)
}
func TestInvalidArgsDefaultCanBeOverridden(t *testing.T) {
app := New("test", "")
app.Arg("a", "").Default("invalid").Bool()
_, err := app.Parse([]string{})
assert.Error(t, err)
}
func TestArgMultipleValuesDefault(t *testing.T) {
app := New("test", "")
a := app.Arg("a", "").Default("default1", "default2").Strings()
_, err := app.Parse([]string{})
assert.NoError(t, err)
assert.Equal(t, []string{"default1", "default2"}, *a)
}
func TestRequiredArgWithEnvarMissingErrors(t *testing.T) {
app := newTestApp()
app.Arg("t", "").Envar("TEST_ARG_ENVAR").Required().Int()
_, err := app.Parse([]string{})
assert.Error(t, err)
}
func TestArgRequiredWithEnvar(t *testing.T) {
os.Setenv("TEST_ARG_ENVAR", "123")
app := newTestApp()
flag := app.Arg("t", "").Envar("TEST_ARG_ENVAR").Required().Int()
_, err := app.Parse([]string{})
assert.NoError(t, err)
assert.Equal(t, 123, *flag)
}
func TestSubcommandArgRequiredWithEnvar(t *testing.T) {
os.Setenv("TEST_ARG_ENVAR", "123")
app := newTestApp()
cmd := app.Command("command", "")
flag := cmd.Arg("t", "").Envar("TEST_ARG_ENVAR").Required().Int()
_, err := app.Parse([]string{"command"})
assert.NoError(t, err)
assert.Equal(t, 123, *flag)
}