Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not add to list if the element exist already #116

Merged
merged 1 commit into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pkg/devfile/parser/data/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ func (d *DevfileV2) GetCommands(options common.DevfileOptions) ([]v1.Command, er
func (d *DevfileV2) AddCommands(commands []v1.Command) error {
var errorsList []string
for _, command := range commands {
var err error
for _, devfileCommand := range d.Commands {
if command.Id == devfileCommand.Id {
errorsList = append(errorsList, (&common.FieldAlreadyExistError{Name: command.Id, Field: "command"}).Error())
continue
err = &common.FieldAlreadyExistError{Name: command.Id, Field: "command"}
errorsList = append(errorsList, err.Error())
break
}
}
d.Commands = append(d.Commands, command)
if err == nil {
d.Commands = append(d.Commands, command)
}
}
if len(errorsList) > 0 {
return fmt.Errorf("errors while adding commands:\n%s", strings.Join(errorsList, "\n"))
Expand Down
46 changes: 43 additions & 3 deletions pkg/devfile/parser/data/v2/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func TestDevfile200_AddCommands(t *testing.T) {
name string
currentCommands []v1.Command
newCommands []v1.Command
wantCommands []v1.Command
wantErr *string
}{
{
Expand All @@ -261,6 +262,26 @@ func TestDevfile200_AddCommands(t *testing.T) {
},
},
},
wantCommands: []v1.Command{
{
Id: "command1",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
{
Id: "command2",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
{
Id: "command3",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
},
wantErr: nil,
},
{
Expand Down Expand Up @@ -299,6 +320,26 @@ func TestDevfile200_AddCommands(t *testing.T) {
},
},
},
wantCommands: []v1.Command{
{
Id: "command1",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
{
Id: "command2",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
{
Id: "command3",
CommandUnion: v1.CommandUnion{
Exec: &v1.ExecCommand{},
},
},
},
wantErr: &multipleDupError,
},
}
Expand All @@ -321,9 +362,8 @@ func TestDevfile200_AddCommands(t *testing.T) {
} else if tt.wantErr != nil {
assert.Regexp(t, *tt.wantErr, err.Error(), "TestDevfile200_AddCommands(): Error message should match")
} else {
wantCommands := append(tt.currentCommands, tt.newCommands...)
if !reflect.DeepEqual(d.Commands, wantCommands) {
t.Errorf("TestDevfile200_AddCommands() wanted: %v, got: %v, difference at %v", wantCommands, d.Commands, pretty.Compare(wantCommands, d.Commands))
if !reflect.DeepEqual(d.Commands, tt.wantCommands) {
t.Errorf("TestDevfile200_AddCommands() wanted: %v, got: %v, difference at %v", tt.wantCommands, d.Commands, pretty.Compare(tt.wantCommands, d.Commands))
}
}

Expand Down
10 changes: 7 additions & 3 deletions pkg/devfile/parser/data/v2/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,17 @@ func (d *DevfileV2) GetDevfileVolumeComponents(options common.DevfileOptions) ([
func (d *DevfileV2) AddComponents(components []v1.Component) error {
var errorsList []string
for _, component := range components {
var err error
for _, devfileComponent := range d.Components {
if component.Name == devfileComponent.Name {
errorsList = append(errorsList, (&common.FieldAlreadyExistError{Name: component.Name, Field: "component"}).Error())
continue
err = &common.FieldAlreadyExistError{Name: component.Name, Field: "component"}
errorsList = append(errorsList, err.Error())
break
}
}
d.Components = append(d.Components, component)
if err == nil {
d.Components = append(d.Components, component)
}
}
if len(errorsList) > 0 {
return fmt.Errorf("errors while adding components:\n%s", strings.Join(errorsList, "\n"))
Expand Down
46 changes: 43 additions & 3 deletions pkg/devfile/parser/data/v2/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestDevfile200_AddComponent(t *testing.T) {
name string
currentComponents []v1.Component
newComponents []v1.Component
wantComponents []v1.Component
wantErr *string
}{
{
Expand All @@ -46,6 +47,26 @@ func TestDevfile200_AddComponent(t *testing.T) {
},
},
},
wantComponents: []v1.Component{
{
Name: "component1",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{},
},
},
{
Name: "component2",
ComponentUnion: v1.ComponentUnion{
Volume: &v1.VolumeComponent{},
},
},
{
Name: "component3",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{},
},
},
},
wantErr: nil,
},
{
Expand Down Expand Up @@ -84,6 +105,26 @@ func TestDevfile200_AddComponent(t *testing.T) {
},
},
},
wantComponents: []v1.Component{
{
Name: "component1",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{},
},
},
{
Name: "component2",
ComponentUnion: v1.ComponentUnion{
Volume: &v1.VolumeComponent{},
},
},
{
Name: "component3",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{},
},
},
},
wantErr: &multipleDupError,
},
}
Expand All @@ -106,9 +147,8 @@ func TestDevfile200_AddComponent(t *testing.T) {
} else if tt.wantErr != nil {
assert.Regexp(t, *tt.wantErr, err.Error(), "TestDevfile200_AddComponents(): Error message should match")
} else {
wantComponents := append(tt.currentComponents, tt.newComponents...)
if !reflect.DeepEqual(d.Components, wantComponents) {
t.Errorf("TestDevfile200_AddComponents() wanted: %v, got: %v, difference at %v", wantComponents, d.Components, pretty.Compare(wantComponents, d.Components))
if !reflect.DeepEqual(d.Components, tt.wantComponents) {
t.Errorf("TestDevfile200_AddComponents() wanted: %v, got: %v, difference at %v", tt.wantComponents, d.Components, pretty.Compare(tt.wantComponents, d.Components))
}
}
})
Expand Down
39 changes: 32 additions & 7 deletions pkg/devfile/parser/data/v2/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ func TestDevfile200_AddProjects(t *testing.T) {
multipleDupError := fmt.Sprintf("%s\n%s", "project java-starter already exists in devfile", "project quarkus-starter already exists in devfile")

tests := []struct {
name string
args []v1.Project
wantErr *string
name string
args []v1.Project
wantProjects []v1.Project
wantErr *string
}{
{
name: "It should add project",
Expand All @@ -209,6 +210,22 @@ func TestDevfile200_AddProjects(t *testing.T) {
Name: "spring-pet-clinic",
},
},
wantProjects: []v1.Project{
{
Name: "java-starter",
ClonePath: "/project",
},
{
Name: "quarkus-starter",
ClonePath: "/test",
},
{
Name: "nodejs",
},
{
Name: "spring-pet-clinic",
},
},
wantErr: nil,
},

Expand All @@ -222,6 +239,16 @@ func TestDevfile200_AddProjects(t *testing.T) {
Name: "quarkus-starter",
},
},
wantProjects: []v1.Project{
{
Name: "java-starter",
ClonePath: "/project",
},
{
Name: "quarkus-starter",
ClonePath: "/test",
},
},
wantErr: &multipleDupError,
},
}
Expand All @@ -234,10 +261,8 @@ func TestDevfile200_AddProjects(t *testing.T) {
} else if tt.wantErr != nil {
assert.Regexp(t, *tt.wantErr, err.Error(), "TestDevfile200_AddProjects(): Error message should match")
} else if err == nil {
wantProjects := append(currentProject, tt.args...)

if !reflect.DeepEqual(d.Projects, wantProjects) {
t.Errorf("TestDevfile200_AddProjects() error: wanted: %v, got: %v, difference at %v", wantProjects, d.Projects, pretty.Compare(wantProjects, d.Projects))
if !reflect.DeepEqual(d.Projects, tt.wantProjects) {
t.Errorf("TestDevfile200_AddProjects() error: wanted: %v, got: %v, difference at %v", tt.wantProjects, d.Projects, pretty.Compare(tt.wantProjects, d.Projects))
}
}
})
Expand Down