Skip to content

Commit e62b6f1

Browse files
committed
check for attribute and print out all default command and it's import reference if any
Signed-off-by: Stephanie <[email protected]>
1 parent 4ba7837 commit e62b6f1

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

pkg/validation/commands.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ func validateCommand(command v1alpha2.Command, parentCommands map[string]string,
6868
// 2. with more than one default, err out
6969
func validateGroup(commands []v1alpha2.Command) error {
7070
defaultCommandCount := 0
71-
71+
var defaultCommands []v1alpha2.Command
7272
if len(commands) > 1 {
7373
for _, command := range commands {
7474
if getGroup(command).IsDefault {
7575
defaultCommandCount++
76+
defaultCommands = append(defaultCommands, command)
7677
}
7778
}
7879
} else {
@@ -82,7 +83,13 @@ func validateGroup(commands []v1alpha2.Command) error {
8283
if defaultCommandCount == 0 {
8384
return fmt.Errorf("there should be exactly one default command, currently there is no default command")
8485
} else if defaultCommandCount > 1 {
85-
return fmt.Errorf("there should be exactly one default command, currently there is more than one default command")
86+
var commandsReference string
87+
for _, command := range defaultCommands {
88+
commandsReference += resolveErrorMessageWithImportArrtibutes(fmt.Errorf("; command: %s", command.Id), command.Attributes).Error()
89+
}
90+
// example: there should be exactly one default command, currently there is more than one default command;
91+
// command: <id1>; command: <id2>, imported from uri: http://127.0.0.1:8080, in parent overrides from main devfile"
92+
return fmt.Errorf("there should be exactly one default command, currently there is more than one default command%s", commandsReference)
8693
}
8794

8895
return nil

pkg/validation/commands_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,10 @@ func TestValidateGroup(t *testing.T) {
332332
component := "alias1"
333333

334334
noDefaultCmdErr := ".*there should be exactly one default command, currently there is no default command"
335-
multipleDefaultCmdErr := ".*there should be exactly one default command, currently there is more than one default command"
335+
multipleDefaultError := ".*there should be exactly one default command, currently there is more than one default command"
336+
multipleDefaultCmdErr := multipleDefaultError + "; command: run command; command: customcommand"
337+
invalidCmdErrWithImportAttributes := multipleDefaultError +
338+
"; command: run command; command: customcommand, imported from uri: http://127.0.0.1:8080, in parent overrides from main devfile"
336339

337340
tests := []struct {
338341
name string
@@ -347,6 +350,30 @@ func TestValidateGroup(t *testing.T) {
347350
},
348351
wantErr: &multipleDefaultCmdErr,
349352
},
353+
{
354+
name: "Two default run commands with import source attribute",
355+
commands: []v1alpha2.Command{
356+
generateDummyExecCommand("run command", component, &v1alpha2.CommandGroup{Kind: runGroup, IsDefault: true}),
357+
{
358+
Attributes: attributes.Attributes{}.PutString(ImportSourceAttribute,
359+
"uri: http://127.0.0.1:8080").PutString(ParentOverrideAttribute, "main devfile"),
360+
Id: "customcommand",
361+
CommandUnion: v1alpha2.CommandUnion{
362+
Exec: &v1alpha2.ExecCommand{
363+
LabeledCommand: v1alpha2.LabeledCommand{
364+
BaseCommand: v1alpha2.BaseCommand{
365+
Group: &v1alpha2.CommandGroup{Kind: runGroup, IsDefault: true},
366+
},
367+
},
368+
CommandLine: "command",
369+
Component: component,
370+
WorkingDir: "workDir",
371+
},
372+
},
373+
},
374+
},
375+
wantErr: &invalidCmdErrWithImportAttributes,
376+
},
350377
{
351378
name: "No default for more than one build commands",
352379
commands: []v1alpha2.Command{

0 commit comments

Comments
 (0)