1 package validate
2
3 import (
4 "testing"
5
6 "github.com/devfile/library/v2/pkg/devfile/parser/data"
7
8 devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
9 devfileParser "github.com/devfile/library/v2/pkg/devfile/parser"
10 parsercommon "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
11 "github.com/devfile/library/v2/pkg/testingutil"
12
13 "github.com/redhat-developer/odo/pkg/util"
14 )
15
16 func Test_getCommands(t *testing.T) {
17
18 component := []devfilev1.Component{
19 testingutil.GetFakeContainerComponent("alias1"),
20 }
21
22 tests := []struct {
23 name string
24 execCommands []devfilev1.Command
25 compCommands []devfilev1.Command
26 expectedCommands []devfilev1.Command
27 }{
28 {
29 name: "Case 1: One command",
30 execCommands: []devfilev1.Command{
31 {
32 Id: "somecommand",
33 CommandUnion: devfilev1.CommandUnion{
34 Exec: &devfilev1.ExecCommand{
35 HotReloadCapable: util.GetBool(false),
36 },
37 },
38 },
39 },
40 expectedCommands: []devfilev1.Command{
41 {
42 Id: "somecommand",
43 CommandUnion: devfilev1.CommandUnion{
44 Exec: &devfilev1.ExecCommand{
45 HotReloadCapable: util.GetBool(false),
46 },
47 },
48 },
49 },
50 },
51 {
52 name: "Case 2: Multiple commands",
53 execCommands: []devfilev1.Command{
54 {
55 Id: "somecommand",
56 CommandUnion: devfilev1.CommandUnion{
57 Exec: &devfilev1.ExecCommand{
58 HotReloadCapable: util.GetBool(false),
59 },
60 },
61 },
62 {
63 Id: "somecommand2",
64 CommandUnion: devfilev1.CommandUnion{
65 Exec: &devfilev1.ExecCommand{
66 HotReloadCapable: util.GetBool(false),
67 },
68 },
69 },
70 },
71 compCommands: []devfilev1.Command{
72 {
73 Id: "mycomposite",
74 CommandUnion: devfilev1.CommandUnion{
75 Composite: &devfilev1.CompositeCommand{
76 Commands: []string{},
77 },
78 },
79 },
80 },
81 expectedCommands: []devfilev1.Command{
82 {
83 Id: "somecommand",
84 CommandUnion: devfilev1.CommandUnion{
85 Exec: &devfilev1.ExecCommand{
86 HotReloadCapable: util.GetBool(false),
87 },
88 },
89 },
90 {
91 Id: "somecommand2",
92 CommandUnion: devfilev1.CommandUnion{
93 Exec: &devfilev1.ExecCommand{
94 HotReloadCapable: util.GetBool(false),
95 },
96 },
97 },
98 {
99 Id: "mycomposite",
100 CommandUnion: devfilev1.CommandUnion{
101 Composite: &devfilev1.CompositeCommand{
102 Commands: []string{},
103 },
104 },
105 },
106 },
107 },
108 }
109 for _, tt := range tests {
110 t.Run(tt.name, func(t *testing.T) {
111 devObj := devfileParser.DevfileObj{
112 Data: func() data.DevfileData {
113 devfileData, err := data.NewDevfileData(string(data.APISchemaVersion200))
114 if err != nil {
115 t.Error(err)
116 }
117 err = devfileData.AddComponents(component)
118 if err != nil {
119 t.Error(err)
120 }
121 err = devfileData.AddCommands(tt.execCommands)
122 if err != nil {
123 t.Error(err)
124 }
125 err = devfileData.AddCommands(tt.compCommands)
126 if err != nil {
127 t.Error(err)
128 }
129 return devfileData
130 }(),
131 }
132
133 commands, err := devObj.Data.GetCommands(parsercommon.DevfileOptions{})
134 if err != nil {
135 t.Errorf("unexpected error: %v", err)
136 }
137
138 commandsMap := getCommandsMap(commands)
139 if len(commandsMap) != len(tt.expectedCommands) {
140 t.Errorf("TestGetCommands error: number of returned commands don't match: %v got: %v", len(tt.expectedCommands), len(commandsMap))
141 }
142 for _, command := range tt.expectedCommands {
143 _, ok := commandsMap[command.Id]
144 if !ok {
145 t.Errorf("TestGetCommands error: command %v not found in map", command.Id)
146 }
147 }
148 })
149 }
150
151 }
152
View as plain text