-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli_test.go
More file actions
219 lines (204 loc) · 5.85 KB
/
cli_test.go
File metadata and controls
219 lines (204 loc) · 5.85 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"context"
"testing"
"github.com/oalders/is/ops"
"github.com/oalders/is/types"
"github.com/stretchr/testify/assert"
)
//nolint:paralleltest,nolintlint
func TestCliVersion(t *testing.T) {
const command = "tmux"
t.Setenv("PATH", prependPath("testdata/bin"))
type test struct {
Cmp VersionCmp
Error bool
Success bool
}
major := false
minor := false
patch := false
//nolint:godox
tests := []test{
{VersionCmp{command, ops.Eq, "3.3a", major, minor, patch}, false, true},
{VersionCmp{command, ops.Gt, "3.2a", major, minor, patch}, false, true},
{VersionCmp{command, ops.Lt, "3.3b", major, minor, patch}, false, true},
{VersionCmp{command, ops.Lt, "4", major, minor, patch}, false, true},
{VersionCmp{command, ops.Ne, "1", major, minor, patch}, false, true},
{VersionCmp{"tmuxzzz", ops.Ne, "1", major, minor, patch}, true, false},
{VersionCmp{command, ops.Eq, "1", major, minor, patch}, false, false},
{VersionCmp{command, ops.Eq, "zzz", major, minor, patch}, true, false},
{VersionCmp{command, ops.Unlike, "zzz", major, minor, patch}, false, true},
{VersionCmp{command, ops.Like, "", major, minor, patch}, false, true}, // FIXME
{VersionCmp{command, ops.Like, "3.*", major, minor, patch}, false, true},
{VersionCmp{command, ops.Eq, "3", true, minor, patch}, false, true},
{VersionCmp{command, ops.Eq, "3", major, true, patch}, false, true},
{VersionCmp{command, ops.Eq, "0", major, minor, true}, false, true},
}
for _, test := range tests {
ctx := &types.Context{
Context: context.Background(),
Debug: true,
}
cmd := CLICmd{Version: test.Cmp}
err := cmd.Run(ctx)
if test.Error {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
if test.Success {
assert.True(t, ctx.Success)
} else {
assert.False(t, ctx.Success)
}
}
}
//nolint:paralleltest,nolintlint
func TestCliAge(t *testing.T) {
t.Setenv("PATH", prependPath("testdata/bin"))
const command = "tmux"
{
ctx := &types.Context{
Context: context.Background(),
Debug: true,
}
cmd := CLICmd{Age: AgeCmp{command, ops.Gt, "1", "s"}}
err := cmd.Run(ctx)
assert.NoError(t, err)
assert.True(t, ctx.Success)
}
{
ctx := &types.Context{
Context: context.Background(),
Debug: true,
}
cmd := CLICmd{Age: AgeCmp{command, ops.Lt, "100000", "days"}}
err := cmd.Run(ctx)
assert.NoError(t, err)
assert.True(t, ctx.Success)
}
{
ctx := &types.Context{
Context: context.Background(),
Debug: true,
}
cmd := CLICmd{Age: AgeCmp{command, ops.Lt, "1.1", "d"}}
err := cmd.Run(ctx)
assert.Error(t, err)
assert.False(t, ctx.Success)
}
{
ctx := &types.Context{
Context: context.Background(),
Debug: true,
}
cmd := CLICmd{Age: AgeCmp{"tmuxxx", ops.Lt, "1", "d"}}
err := cmd.Run(ctx)
assert.Error(t, err)
assert.False(t, ctx.Success)
}
}
//nolint:paralleltest,nolintlint
func TestCliOutput(t *testing.T) {
t.Setenv("PATH", prependPath("testdata/bin"))
type test struct {
Cmp OutputCmp
Error bool
Success bool
}
command := "tmux"
args := []string{"-V"}
const optimistic = "optimistic"
tests := []test{
{OutputCmp{"stdout", command, ops.Eq, "tmux 3.3a", args, optimistic}, false, true},
{OutputCmp{"stdout", command, ops.Ne, "1", args, optimistic}, false, true},
{OutputCmp{"stdout", command, ops.Eq, "1", args, optimistic}, false, false},
{OutputCmp{"stderr", command, ops.Like, "xxx", args, optimistic}, false, false},
{OutputCmp{"stderr", command, ops.Unlike, "xxx", args, optimistic}, false, true},
{OutputCmp{"combined", command, ops.Like, "xxx", args, optimistic}, false, false},
{OutputCmp{"combined", command, ops.Unlike, "xxx", args, optimistic}, false, true},
{OutputCmp{"stdout", command, ops.Ne, "1", args, "string"}, false, true},
{OutputCmp{"stdout", command, ops.Ne, "1", args, "integer"}, true, false},
{OutputCmp{"stdout", command, ops.Ne, "1", args, "version"}, true, false},
{OutputCmp{"stdout", command, ops.Ne, "1", args, "float"}, true, false},
{OutputCmp{"stdout", "bash -c", ops.Eq, "1", []string{"date|wc -l"}, "integer"}, false, true},
}
for _, test := range tests {
ctx := &types.Context{
Context: context.Background(),
Debug: true,
}
cmd := CLICmd{Output: test.Cmp}
err := cmd.Run(ctx)
if test.Error {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
if test.Success {
assert.True(t, ctx.Success)
} else {
assert.False(t, ctx.Success)
}
}
}
//nolint:paralleltest,nolintlint
func TestParseCommand(t *testing.T) {
type test struct {
cmdLine string
args []string
wantCmd string
wantArgs []string
description string
}
tests := []test{
{
cmdLine: "uname",
args: []string{},
wantCmd: "uname",
wantArgs: []string{},
description: "simple command without args",
},
{
cmdLine: "uname -a",
args: []string{},
wantCmd: "uname",
wantArgs: []string{"-a"},
description: "command with embedded args",
},
{
cmdLine: "uname -m -n",
args: []string{},
wantCmd: "uname",
wantArgs: []string{"-m", "-n"},
description: "command with multiple embedded args",
},
{
cmdLine: "uname",
args: []string{"-a", "-m"},
wantCmd: "uname",
wantArgs: []string{"-a", "-m"},
description: "command with explicit --arg flags",
},
{
cmdLine: "bash -c",
args: []string{"date|wc -l"},
wantCmd: "bash",
wantArgs: []string{"-c", "date|wc -l"},
description: "special bash -c case",
},
{
cmdLine: "cat file.txt",
args: []string{},
wantCmd: "cat",
wantArgs: []string{"file.txt"},
description: "command with filename argument",
},
}
for _, test := range tests {
cmd, args := parseCommand(test.cmdLine, test.args)
assert.Equal(t, test.wantCmd, cmd, test.description)
assert.Equal(t, test.wantArgs, args, test.description)
}
}