-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell_test.go
212 lines (206 loc) · 4.08 KB
/
shell_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
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
package cbsd
import (
"reflect"
"strings"
"testing"
)
func TestShellExec_Command(t *testing.T) {
type fields struct {
name string
value string
}
type args struct {
name string
arg []string
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
name: "ls",
fields: fields{
name: "NOCOLOR",
value: "1",
},
args: args{
name: "ls",
arg: []string{"-alh"},
},
want: "cbsd.go",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &ShellExec{}
s.SetEnv(tt.fields.name, tt.fields.value)
got, err := s.Command(tt.args.name, tt.args.arg...)
if (err != nil) != tt.wantErr {
t.Errorf("Command() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !strings.Contains(string(got), tt.want) {
t.Errorf("Command() got = %v, want %v", string(got), tt.want)
}
})
}
}
type TestStruct struct {
Name string `json:"name,omitempty"`
StringValue string `json:"string_value,omitempty"`
EmptyValue string `json:"empty_value,omitempty"`
NoTagValue string
Bool bool `json:"bool,omitempty"`
BoolFieldPtr *bool `json:"bool_field,omitempty"`
EmptyBoolFieldPtr *bool `json:"empty_bool_field,omitempty"`
}
func Test_structToSlice(t *testing.T) {
type args struct {
b *TestStruct
}
tests := []struct {
name string
args args
want []string
}{
{
name: "Test Struct Bool Ptr",
args: args{b: &TestStruct{
Name: "test",
StringValue: "test-value",
NoTagValue: "no-tag",
BoolFieldPtr: Bool(true),
}},
want: []string{
"name=test",
"string_value=test-value",
"bool=0",
"bool_field=1",
},
},
{
name: "Test Struct Bool Ptr False",
args: args{b: &TestStruct{
Name: "test",
StringValue: "test-value",
NoTagValue: "no-tag",
BoolFieldPtr: Bool(false),
}},
want: []string{
"name=test",
"string_value=test-value",
"bool=0",
"bool_field=0",
},
},
{
name: "Test Struct Bool",
args: args{b: &TestStruct{
Name: "test",
StringValue: "test-value",
NoTagValue: "no-tag",
Bool: true,
}},
want: []string{
"name=test",
"string_value=test-value",
"bool=1",
},
},
{
name: "Test Struct Bool False",
args: args{b: &TestStruct{
Name: "test",
StringValue: "test-value",
NoTagValue: "no-tag",
Bool: false,
}},
want: []string{
"name=test",
"string_value=test-value",
"bool=0",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := structToSlice(tt.args.b); !reflect.DeepEqual(got, tt.want) {
t.Errorf("structToSlice() = %v, want %v", got, tt.want)
}
})
}
}
func Test_quote(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{
name: "Empty",
s: "",
want: "''",
},
{
name: "Normal",
s: "1",
want: "1",
},
{
name: "Escape",
s: "1 2",
want: "'1 2'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := quote(tt.s); got != tt.want {
t.Errorf("quote() = %v, want %v", got, tt.want)
}
})
}
}
func TestShellExec_CommandWithInterface(t *testing.T) {
type args struct {
name string
i *TestStruct
arg []string
}
tests := []struct {
name string
args args
wantStr string
wantErr bool
}{
{
name: "CommandWithInterface",
args: args{
name: "ls",
i: &TestStruct{
Name: "test",
StringValue: "1",
},
arg: nil,
},
wantStr: "ls name=test string_value=1 bool=0",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &ShellExec{}
_, err := s.CommandWithInterface(tt.args.name, tt.args.i, tt.args.arg...)
if (err != nil) != tt.wantErr {
t.Errorf("CommandWithInterface() error = %v, wantErr %v", err, tt.wantErr)
return
}
if s.String() != tt.wantStr {
t.Errorf("CommandWithInterface() got = %v, want %v", s.String(), tt.wantStr)
}
})
}
}