-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli_test.go
138 lines (126 loc) · 4.12 KB
/
cli_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
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
package main
import (
"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{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{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{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{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{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{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)
}
}
}