-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
139 lines (133 loc) · 3.54 KB
/
cli_test.go
File metadata and controls
139 lines (133 loc) · 3.54 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"io"
"strings"
"testing"
)
// TestApplication implements the Applicator interface.
type TestApplication struct{}
func (t *TestApplication) Serve(address, port, configFile string) error {
return nil
}
func (t *TestApplication) ServeInDevelopment(address, port string, templateSuffixes []string, configFile string) error {
return nil
}
func (t *TestApplication) Init(directory string) error {
return nil
}
func (t *TestApplication) Demo(address, port string) error {
return nil
}
func TestParseCLI(t *testing.T) {
testApp := &TestApplication{}
tests := []struct {
name string
args []string
wantErrContains string
}{
{
name: "help",
args: []string{"program", "-h"},
},
{
name: "serve all options",
args: []string{"program", "serve", "-a", "127.0.0.1", "-p", "8001", "config.yaml"},
},
{
name: "serve only config",
args: []string{"program", "serve", "config.yaml"},
},
{
name: "serve no config",
args: []string{"program", "serve", "--address", "127.0.0.2"},
wantErrContains: "missing required argument",
},
{
name: "serve help",
args: []string{"program", "serve", "-h"},
},
{
name: "serve invalid address",
args: []string{"program", "serve", "-a", "hi", "-p", "8001", "config.yaml"},
wantErrContains: "invalid IP address",
},
{
name: "serve invalid port",
args: []string{"program", "serve", "-a", "127.0.0.3", "-p", "eight", "config.yaml"},
wantErrContains: "invalid port",
},
{
name: "init help",
args: []string{"program", "init", "-h"},
},
{
name: "init ok default",
args: []string{"program", "init"},
},
{
name: "init ok with tmp dir",
args: []string{"program", "init", "-d", "/tmp"},
},
{
name: "init failure",
args: []string{"program", "init", "-d", "/_DATA/tmp"},
wantErrContains: "does not exist",
},
{
name: "demo help",
args: []string{"program", "demo", "-h"},
},
{
name: "demo ok",
args: []string{"program", "demo", "-a", "127.0.0.1", "-p", "8001"},
},
{
name: "demo ok no args",
args: []string{"program", "demo"},
},
{
name: "demo invalid address",
args: []string{"program", "demo", "-a", "url", "-p", "8001"},
wantErrContains: "invalid IP address",
},
{
name: "development all options",
args: []string{"program", "develop", "-a", "127.0.0.1", "-p", "8001", "-s", "html", "config.yaml"},
},
{
name: "development only config",
args: []string{"program", "develop", "config.yaml"},
},
{
name: "development no config",
args: []string{"program", "develop", "--address", "127.0.0.2"},
wantErrContains: "missing required argument",
},
{
name: "development no suffix",
args: []string{"program", "develop", "-a", "127.0.0.1", "-p", "8001", "-s", "", "config.yaml"},
wantErrContains: "empty suffix argument",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := BuildCLI(testApp)
cmd.Writer = io.Discard
cmd.ErrWriter = io.Discard
err := cmd.Run(context.Background(), tt.args)
if tt.wantErrContains != "" {
if err == nil {
t.Fatalf("expected an error containing %q", tt.wantErrContains)
}
if got, want := err.Error(), tt.wantErrContains; !strings.Contains(got, want) {
t.Fatalf("got error that did not contain %q: %v", want, got)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
}