@@ -12,9 +12,6 @@ import (
12
12
13
13
func TestDevfile200_GetCommands (t * testing.T ) {
14
14
15
- type args struct {
16
- name string
17
- }
18
15
tests := []struct {
19
16
name string
20
17
currentCommands []v1.Command
@@ -23,7 +20,7 @@ func TestDevfile200_GetCommands(t *testing.T) {
23
20
wantErr bool
24
21
}{
25
22
{
26
- name : "case 1: get the necessary commands" ,
23
+ name : "Get all the commands" ,
27
24
currentCommands : []v1.Command {
28
25
{
29
26
Id : "command1" ,
@@ -38,12 +35,11 @@ func TestDevfile200_GetCommands(t *testing.T) {
38
35
},
39
36
},
40
37
},
41
- filterOptions : common.DevfileOptions {},
42
- wantCommands : []string {"command1" , "command2" },
43
- wantErr : false ,
38
+ wantCommands : []string {"command1" , "command2" },
39
+ wantErr : false ,
44
40
},
45
41
{
46
- name : "case 2: get the filtered commands" ,
42
+ name : "Get the filtered commands" ,
47
43
currentCommands : []v1.Command {
48
44
{
49
45
Id : "command1" ,
@@ -52,7 +48,15 @@ func TestDevfile200_GetCommands(t *testing.T) {
52
48
"secondString" : "secondStringValue" ,
53
49
}),
54
50
CommandUnion : v1.CommandUnion {
55
- Exec : & v1.ExecCommand {},
51
+ Exec : & v1.ExecCommand {
52
+ LabeledCommand : v1.LabeledCommand {
53
+ BaseCommand : v1.BaseCommand {
54
+ Group : & v1.CommandGroup {
55
+ Kind : v1 .BuildCommandGroupKind ,
56
+ },
57
+ },
58
+ },
59
+ },
56
60
},
57
61
},
58
62
{
@@ -65,18 +69,74 @@ func TestDevfile200_GetCommands(t *testing.T) {
65
69
Composite : & v1.CompositeCommand {},
66
70
},
67
71
},
72
+ {
73
+ Id : "command3" ,
74
+ Attributes : attributes.Attributes {}.FromStringMap (map [string ]string {
75
+ "firstString" : "firstStringValue" ,
76
+ "thirdString" : "thirdStringValue" ,
77
+ }),
78
+ CommandUnion : v1.CommandUnion {
79
+ Composite : & v1.CompositeCommand {
80
+ LabeledCommand : v1.LabeledCommand {
81
+ BaseCommand : v1.BaseCommand {
82
+ Group : & v1.CommandGroup {
83
+ Kind : v1 .BuildCommandGroupKind ,
84
+ },
85
+ },
86
+ },
87
+ },
88
+ },
89
+ },
90
+ {
91
+ Id : "command4" ,
92
+ Attributes : attributes.Attributes {}.FromStringMap (map [string ]string {
93
+ "thirdString" : "thirdStringValue" ,
94
+ }),
95
+ CommandUnion : v1.CommandUnion {
96
+ Apply : & v1.ApplyCommand {
97
+ LabeledCommand : v1.LabeledCommand {
98
+ BaseCommand : v1.BaseCommand {
99
+ Group : & v1.CommandGroup {
100
+ Kind : v1 .BuildCommandGroupKind ,
101
+ },
102
+ },
103
+ },
104
+ },
105
+ },
106
+ },
107
+ {
108
+ Id : "command5" ,
109
+ Attributes : attributes.Attributes {}.FromStringMap (map [string ]string {
110
+ "firstString" : "firstStringValue" ,
111
+ "thirdString" : "thirdStringValue" ,
112
+ }),
113
+ CommandUnion : v1.CommandUnion {
114
+ Composite : & v1.CompositeCommand {
115
+ LabeledCommand : v1.LabeledCommand {
116
+ BaseCommand : v1.BaseCommand {
117
+ Group : & v1.CommandGroup {
118
+ Kind : v1 .RunCommandGroupKind ,
119
+ },
120
+ },
121
+ },
122
+ },
123
+ },
124
+ },
68
125
},
69
126
filterOptions : common.DevfileOptions {
70
127
Filter : map [string ]interface {}{
71
- "firstString" : "firstStringValue" ,
72
- "secondString" : "secondStringValue" ,
128
+ "firstString" : "firstStringValue" ,
129
+ },
130
+ CommandOptions : common.CommandOptions {
131
+ CommandGroupKind : v1 .BuildCommandGroupKind ,
132
+ CommandType : v1 .CompositeCommandType ,
73
133
},
74
134
},
75
- wantCommands : []string {"command1 " },
135
+ wantCommands : []string {"command3 " },
76
136
wantErr : false ,
77
137
},
78
138
{
79
- name : "case 3: get the wrong filtered commands" ,
139
+ name : "Wrong filter for commands" ,
80
140
currentCommands : []v1.Command {
81
141
{
82
142
Id : "command1" ,
@@ -104,8 +164,25 @@ func TestDevfile200_GetCommands(t *testing.T) {
104
164
"firstStringIsWrong" : "firstStringValue" ,
105
165
},
106
166
},
107
- wantCommands : []string {},
108
- wantErr : false ,
167
+ wantErr : false ,
168
+ },
169
+ {
170
+ name : "Invalid command type" ,
171
+ currentCommands : []v1.Command {
172
+ {
173
+ Id : "command1" ,
174
+ Attributes : attributes.Attributes {}.FromStringMap (map [string ]string {
175
+ "firstString" : "firstStringValue" ,
176
+ }),
177
+ CommandUnion : v1.CommandUnion {},
178
+ },
179
+ },
180
+ filterOptions : common.DevfileOptions {
181
+ Filter : map [string ]interface {}{
182
+ "firstString" : "firstStringValue" ,
183
+ },
184
+ },
185
+ wantErr : true ,
109
186
},
110
187
}
111
188
for _ , tt := range tests {
@@ -121,26 +198,27 @@ func TestDevfile200_GetCommands(t *testing.T) {
121
198
}
122
199
123
200
commands , err := d .GetCommands (tt .filterOptions )
124
- if ! tt .wantErr && err != nil {
125
- t .Errorf ("TestDevfile200_GetCommands() unexpected error - %v" , err )
126
- return
127
- } else if tt .wantErr && err == nil {
128
- t .Errorf ("TestDevfile200_GetCommands() expected an error but got nil %v" , commands )
129
- return
130
- } else if tt .wantErr && err != nil {
131
- return
132
- }
201
+ if (err != nil ) != tt .wantErr {
202
+ t .Errorf ("TestDevfile200_GetCommands() error = %v, wantErr %v" , err , tt .wantErr )
203
+ } else if err == nil {
204
+ // confirm the length of actual vs expected
205
+ if len (commands ) != len (tt .wantCommands ) {
206
+ t .Errorf ("TestDevfile200_GetCommands() error - length of expected commands is not the same as the length of actual commands" )
207
+ return
208
+ }
133
209
134
- for _ , wantCommand := range tt .wantCommands {
135
- matched := false
136
- for _ , devfileCommand := range commands {
137
- if wantCommand == devfileCommand .Id {
138
- matched = true
210
+ // compare the command slices for content
211
+ for _ , wantCommand := range tt .wantCommands {
212
+ matched := false
213
+ for _ , command := range commands {
214
+ if wantCommand == command .Id {
215
+ matched = true
216
+ }
139
217
}
140
- }
141
218
142
- if ! matched {
143
- t .Errorf ("TestDevfile200_GetCommands() error - command %s not found in the devfile" , wantCommand )
219
+ if ! matched {
220
+ t .Errorf ("TestDevfile200_GetCommands() error - command %s not found in the devfile" , wantCommand )
221
+ }
144
222
}
145
223
}
146
224
})
0 commit comments