Skip to content

Commit ad9fecd

Browse files
committed
Address 2nd set of code review comments
1 parent c9ea73a commit ad9fecd

5 files changed

+252
-171
lines changed

tests/src/tests/command_test_utils.go

+65-63
Original file line numberDiff line numberDiff line change
@@ -11,114 +11,102 @@ import (
1111
schema "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
1212
)
1313

14+
// Return a specifed number of env attributes in a schema structure
1415
func AddEnv(numEnv int) []schema.EnvVar {
1516
commandEnvs := make([]schema.EnvVar, numEnv)
1617
for i := 0; i < numEnv; i++ {
1718
commandEnvs[i].Name = "Name_" + GetRandomString(5, false)
1819
commandEnvs[i].Value = "Value_" + GetRandomString(5, false)
19-
LogMessage(fmt.Sprintf(" ....... Add Env: %s", commandEnvs[i]))
20+
LogInfoMessage(fmt.Sprintf("Add Env: %s", commandEnvs[i]))
2021
}
2122
return commandEnvs
2223
}
2324

25+
// Return a specifed number of attributes in a schema structure
2426
func AddAttributes(numAtrributes int) map[string]string {
2527
attributes := make(map[string]string)
2628
for i := 0; i < numAtrributes; i++ {
2729
AttributeName := "Name_" + GetRandomString(6, false)
2830
attributes[AttributeName] = "Value_" + GetRandomString(6, false)
29-
LogMessage(fmt.Sprintf(" ....... Add attribute : %s = %s", AttributeName, attributes[AttributeName]))
31+
LogInfoMessage(fmt.Sprintf("Add attribute : %s = %s", AttributeName, attributes[AttributeName]))
3032
}
3133
return attributes
3234
}
3335

36+
// Create and return a group in a schema structure
3437
func addGroup() *schema.CommandGroup {
3538

3639
commandGroup := schema.CommandGroup{}
37-
3840
commandGroup.Kind = GetRandomGroupKind()
39-
LogMessage(fmt.Sprintf(" ....... group Kind: %s", commandGroup.Kind))
41+
LogInfoMessage(fmt.Sprintf("group Kind: %s", commandGroup.Kind))
4042
commandGroup.IsDefault = GetBinaryDecision()
41-
LogMessage(fmt.Sprintf(" ....... group isDefault: %t", commandGroup.IsDefault))
42-
43+
LogInfoMessage(fmt.Sprintf("group isDefault: %t", commandGroup.IsDefault))
4344
return &commandGroup
4445
}
4546

47+
// Add a command of the specified type to the schema
4648
func (devfile *TestDevfile) addCommand(commandType schema.CommandType) string {
47-
48-
var commands []schema.Command
49-
index := 0
50-
if devfile.SchemaDevFile.Commands != nil {
51-
// Commands already exist so expand the current slice
52-
currentSize := len(devfile.SchemaDevFile.Commands)
53-
commands = make([]schema.Command, currentSize+1)
54-
for _, command := range devfile.SchemaDevFile.Commands {
55-
commands[index] = command
56-
index++
57-
}
58-
} else {
59-
commands = make([]schema.Command, 1)
60-
}
61-
62-
generateCommand(&commands[index], commandType)
63-
devfile.SchemaDevFile.Commands = commands
64-
65-
return commands[index].Id
66-
49+
command := generateCommand(commandType)
50+
devfile.SchemaDevFile.Commands = append(devfile.SchemaDevFile.Commands, command)
51+
return command.Id
6752
}
6853

69-
func generateCommand(command *schema.Command, commandType schema.CommandType) {
54+
// Create a command of a specified type in a schema structure
55+
func generateCommand(commandType schema.CommandType) schema.Command {
56+
command := schema.Command{}
7057
command.Id = GetRandomUniqueString(8, true)
71-
LogMessage(fmt.Sprintf(" ....... id: %s", command.Id))
58+
LogInfoMessage(fmt.Sprintf("command Id: %s", command.Id))
7259

7360
if commandType == schema.ExecCommandType {
7461
command.Exec = createExecCommand()
7562
} else if commandType == schema.CompositeCommandType {
7663
command.Composite = createCompositeCommand()
7764
}
65+
return command
7866
}
7967

68+
// Update the values of a specified command
8069
func (devfile *TestDevfile) UpdateCommand(parserCommand *schema.Command) error {
8170

82-
errorString := ""
71+
var errorString []string
8372
testCommand, found := getSchemaCommand(devfile.SchemaDevFile.Commands, parserCommand.Id)
8473
if found {
85-
LogMessage(fmt.Sprintf(" ....... Updating command id: %s", parserCommand.Id))
74+
LogInfoMessage(fmt.Sprintf("Updating command id: %s", parserCommand.Id))
8675
if testCommand.Exec != nil {
8776
setExecCommandValues(parserCommand.Exec)
8877
} else if testCommand.Composite != nil {
8978
setCompositeCommandValues(parserCommand.Composite)
9079
}
9180
devfile.replaceSchemaCommand(*parserCommand)
9281
} else {
93-
errorString += LogMessage(fmt.Sprintf(" ....... Command not found in test : %s", parserCommand.Id))
82+
errorString = append(errorString, LogErrorMessage(fmt.Sprintf("Command not found in test : %s", parserCommand.Id)))
9483
}
9584

96-
var err error
97-
if errorString != "" {
98-
err = errors.New(errorString)
85+
var returnError error
86+
if len(errorString) > 0 {
87+
returnError = errors.New(fmt.Sprint(errorString))
9988
}
100-
return err
89+
return returnError
10190
}
10291

92+
// Create and return an exec command in a schema structure
10393
func createExecCommand() *schema.ExecCommand {
10494

105-
LogMessage("Create an exec command :")
106-
95+
LogInfoMessage("Create an exec command :")
10796
execCommand := schema.ExecCommand{}
108-
10997
setExecCommandValues(&execCommand)
110-
11198
return &execCommand
11299

113100
}
114101

102+
// Set the attribute values of an exec command
115103
func setExecCommandValues(execCommand *schema.ExecCommand) {
116104

117105
execCommand.Component = GetRandomString(8, false)
118-
LogMessage(fmt.Sprintf(" ....... component: %s", execCommand.Component))
106+
LogInfoMessage(fmt.Sprintf("....... component: %s", execCommand.Component))
119107

120108
execCommand.CommandLine = GetRandomString(4, false) + " " + GetRandomString(4, false)
121-
LogMessage(fmt.Sprintf(" ....... commandLine: %s", execCommand.CommandLine))
109+
LogInfoMessage(fmt.Sprintf("....... commandLine: %s", execCommand.CommandLine))
122110

123111
if GetRandomDecision(2, 1) {
124112
execCommand.Group = addGroup()
@@ -128,20 +116,20 @@ func setExecCommandValues(execCommand *schema.ExecCommand) {
128116

129117
if GetBinaryDecision() {
130118
execCommand.Label = GetRandomString(12, false)
131-
LogMessage(fmt.Sprintf(" ....... label: %s", execCommand.Label))
119+
LogInfoMessage(fmt.Sprintf("....... label: %s", execCommand.Label))
132120
} else {
133121
execCommand.Label = ""
134122
}
135123

136124
if GetBinaryDecision() {
137125
execCommand.WorkingDir = "./tmp"
138-
LogMessage(fmt.Sprintf(" ....... WorkingDir: %s", execCommand.WorkingDir))
126+
LogInfoMessage(fmt.Sprintf("....... WorkingDir: %s", execCommand.WorkingDir))
139127
} else {
140128
execCommand.WorkingDir = ""
141129
}
142130

143131
execCommand.HotReloadCapable = GetBinaryDecision()
144-
LogMessage(fmt.Sprintf(" ....... HotReloadCapable: %t", execCommand.HotReloadCapable))
132+
LogInfoMessage(fmt.Sprintf("....... HotReloadCapable: %t", execCommand.HotReloadCapable))
145133

146134
if GetBinaryDecision() {
147135
execCommand.Env = AddEnv(GetRandomNumber(4))
@@ -151,6 +139,7 @@ func setExecCommandValues(execCommand *schema.ExecCommand) {
151139

152140
}
153141

142+
// Use the specified command to replace the command in the schema structure with the same Id.
154143
func (devfile TestDevfile) replaceSchemaCommand(command schema.Command) {
155144
for i := 0; i < len(devfile.SchemaDevFile.Commands); i++ {
156145
if devfile.SchemaDevFile.Commands[i].Id == command.Id {
@@ -160,6 +149,7 @@ func (devfile TestDevfile) replaceSchemaCommand(command schema.Command) {
160149
}
161150
}
162151

152+
// Get a command from the schema structure
163153
func getSchemaCommand(commands []schema.Command, id string) (*schema.Command, bool) {
164154
found := false
165155
var schemaCommand schema.Command
@@ -173,22 +163,23 @@ func getSchemaCommand(commands []schema.Command, id string) (*schema.Command, bo
173163
return &schemaCommand, found
174164
}
175165

166+
// Create a composite command in a schema structure
176167
func createCompositeCommand() *schema.CompositeCommand {
177168

169+
LogInfoMessage("Create a composite command :")
178170
compositeCommand := schema.CompositeCommand{}
179-
180171
setCompositeCommandValues(&compositeCommand)
181-
182172
return &compositeCommand
183173
}
184174

175+
// Set the attribute values for a composite command
185176
func setCompositeCommandValues(compositeCommand *schema.CompositeCommand) {
186177
numCommands := GetRandomNumber(3)
187178

188179
compositeCommand.Commands = make([]string, numCommands)
189180
for i := 0; i < numCommands; i++ {
190181
compositeCommand.Commands[i] = GetRandomUniqueString(8, false)
191-
LogMessage(fmt.Sprintf(" ....... command %d of %d : %s", i, numCommands, compositeCommand.Commands[i]))
182+
LogInfoMessage(fmt.Sprintf("....... command %d of %d : %s", i, numCommands, compositeCommand.Commands[i]))
192183
}
193184

194185
if GetRandomDecision(2, 1) {
@@ -197,60 +188,71 @@ func setCompositeCommandValues(compositeCommand *schema.CompositeCommand) {
197188

198189
if GetBinaryDecision() {
199190
compositeCommand.Label = GetRandomString(12, false)
200-
LogMessage(fmt.Sprintf(" ....... label: %s", compositeCommand.Label))
191+
LogInfoMessage(fmt.Sprintf("....... label: %s", compositeCommand.Label))
201192
}
202193

203194
if GetBinaryDecision() {
204195
compositeCommand.Parallel = true
205-
LogMessage(fmt.Sprintf(" ....... Parallel: %t", compositeCommand.Parallel))
196+
LogInfoMessage(fmt.Sprintf("....... Parallel: %t", compositeCommand.Parallel))
206197
}
207198
}
208199

200+
// Verify commands returned by the parser match with those saved in the schema
209201
func (devfile TestDevfile) VerifyCommands(parserCommands []schema.Command) error {
210202

211-
LogMessage("Enter VerifyCommands")
212-
errorString := ""
203+
LogInfoMessage("Enter VerifyCommands")
204+
var errorString []string
213205

214206
// Compare entire array of commands
215207
if !cmp.Equal(parserCommands, devfile.SchemaDevFile.Commands) {
216-
errorString += LogMessage(fmt.Sprintf(" --> ERROR: Command array compare failed."))
208+
errorString = append(errorString, LogErrorMessage(fmt.Sprintf("Command array compare failed.")))
217209
// Array compare failed. Narrow down by comparing indivdual commands
218210
for _, command := range parserCommands {
219211
if testCommand, found := getSchemaCommand(devfile.SchemaDevFile.Commands, command.Id); found {
220212
if !cmp.Equal(command, *testCommand) {
221213
parserFilename := AddSuffixToFileName(devfile.FileName, "_"+command.Id+"_Parser")
222214
testFilename := AddSuffixToFileName(devfile.FileName, "_"+command.Id+"_Test")
223-
LogMessage(fmt.Sprintf(" .......marshall and write devfile %s", devfile.FileName))
215+
LogInfoMessage(fmt.Sprintf(".......marshall and write devfile %s", devfile.FileName))
224216
c, err := yaml.Marshal(command)
225-
if err == nil {
217+
if err != nil {
218+
errorString = append(errorString, LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", parserFilename)))
219+
} else {
226220
err = ioutil.WriteFile(parserFilename, c, 0644)
221+
if err != nil {
222+
errorString = append(errorString, LogErrorMessage(fmt.Sprintf(".......write devfile %s", parserFilename)))
223+
}
227224
}
228-
LogMessage(fmt.Sprintf(" .......marshall and write devfile %s", devfile.FileName))
225+
LogInfoMessage(fmt.Sprintf(".......marshall and write devfile %s", testFilename))
229226
c, err = yaml.Marshal(testCommand)
230-
if err == nil {
227+
if err != nil {
228+
errorString = append(errorString, LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", testFilename)))
229+
} else {
231230
err = ioutil.WriteFile(testFilename, c, 0644)
231+
if err != nil {
232+
errorString = append(errorString, LogErrorMessage(fmt.Sprintf(".......write devfile %s", testFilename)))
233+
}
232234
}
233-
errorString += LogMessage(fmt.Sprintf(" --> ERROR: Command %s did not match, see files : %s and %s", command.Id, parserFilename, testFilename))
235+
errorString = append(errorString, LogInfoMessage(fmt.Sprintf("Command %s did not match, see files : %s and %s", command.Id, parserFilename, testFilename)))
234236
} else {
235-
LogMessage(fmt.Sprintf(" --> Command matched : %s", command.Id))
237+
LogInfoMessage(fmt.Sprintf(" --> Command matched : %s", command.Id))
236238
}
237239
} else {
238-
errorString += LogMessage(fmt.Sprintf(" --> ERROR: Command from parser not known to test - id : %s ", command.Id))
240+
errorString = append(errorString, LogErrorMessage(fmt.Sprintf("Command from parser not known to test - id : %s ", command.Id)))
239241
}
240242

241243
}
242244
for _, command := range devfile.SchemaDevFile.Commands {
243245
if _, found := getSchemaCommand(parserCommands, command.Id); !found {
244-
errorString += LogMessage(fmt.Sprintf(" --> ERROR: Command from test not returned by parser : %s ", command.Id))
246+
errorString = append(errorString, LogErrorMessage(fmt.Sprintf("Command from test not returned by parser : %s ", command.Id)))
245247
}
246248
}
247249
} else {
248-
LogMessage(fmt.Sprintf(" --> Command structures matched"))
250+
LogInfoMessage(fmt.Sprintf(" --> Command structures matched"))
249251
}
250252

251253
var err error
252-
if errorString != "" {
253-
err = errors.New(errorString)
254+
if len(errorString) > 0 {
255+
err = errors.New(fmt.Sprint(errorString))
254256
}
255257
return err
256258
}

0 commit comments

Comments
 (0)