diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index e41ebf06..bbf0ef85 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -201,26 +201,20 @@ func TestGetContainers(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("TestGetContainers() error = %v, wantErr %v", err, tt.wantErr) - return - } - - // Expected error and got an err - if tt.wantErr && err != nil { - return - } - - for _, container := range containers { - if container.Name != tt.wantContainerName { - t.Errorf("TestGetContainers error: Name mismatch - got: %s, wanted: %s", container.Name, tt.wantContainerName) - } - if container.Image != tt.wantContainerImage { - t.Errorf("TestGetContainers error: Image mismatch - got: %s, wanted: %s", container.Image, tt.wantContainerImage) - } - if len(container.Env) > 0 && !reflect.DeepEqual(container.Env, tt.wantContainerEnv) { - t.Errorf("TestGetContainers error: Env mismatch - got: %+v, wanted: %+v", container.Env, tt.wantContainerEnv) - } - if len(container.VolumeMounts) > 0 && !reflect.DeepEqual(container.VolumeMounts, tt.wantContainerVolMount) { - t.Errorf("TestGetContainers error: Vol Mount mismatch - got: %+v, wanted: %+v", container.VolumeMounts, tt.wantContainerVolMount) + } else if err == nil { + for _, container := range containers { + if container.Name != tt.wantContainerName { + t.Errorf("TestGetContainers error: Name mismatch - got: %s, wanted: %s", container.Name, tt.wantContainerName) + } + if container.Image != tt.wantContainerImage { + t.Errorf("TestGetContainers error: Image mismatch - got: %s, wanted: %s", container.Image, tt.wantContainerImage) + } + if len(container.Env) > 0 && !reflect.DeepEqual(container.Env, tt.wantContainerEnv) { + t.Errorf("TestGetContainers error: Env mismatch - got: %+v, wanted: %+v", container.Env, tt.wantContainerEnv) + } + if len(container.VolumeMounts) > 0 && !reflect.DeepEqual(container.VolumeMounts, tt.wantContainerVolMount) { + t.Errorf("TestGetContainers error: Vol Mount mismatch - got: %+v, wanted: %+v", container.VolumeMounts, tt.wantContainerVolMount) + } } } }) diff --git a/pkg/devfile/generator/utils_test.go b/pkg/devfile/generator/utils_test.go index 1da25e65..71f9e065 100644 --- a/pkg/devfile/generator/utils_test.go +++ b/pkg/devfile/generator/utils_test.go @@ -402,11 +402,11 @@ func TestAddSyncFolder(t *testing.T) { if !tt.wantErr == (err != nil) { t.Errorf("expected %v, actual %v", tt.wantErr, err) - } - - for _, env := range container.Env { - if env.Name == EnvProjectsSrc && env.Value != tt.want { - t.Errorf("expected %s, actual %s", tt.want, env.Value) + } else if err == nil { + for _, env := range container.Env { + if env.Name == EnvProjectsSrc && env.Value != tt.want { + t.Errorf("expected %s, actual %s", tt.want, env.Value) + } } } }) @@ -764,26 +764,20 @@ func TestGetServiceSpec(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("TestGetServiceSpec() error = %v, wantErr %v", err, tt.wantErr) - return - } - - // Expected error and got an err - if tt.wantErr && err != nil { - return - } - - if !reflect.DeepEqual(serviceSpec.Selector, tt.labels) { - t.Errorf("expected service selector is %v, actual %v", tt.labels, serviceSpec.Selector) - } - if len(serviceSpec.Ports) != len(tt.wantPorts) { - t.Errorf("expected service ports length is %v, actual %v", len(tt.wantPorts), len(serviceSpec.Ports)) - } else { - for i := range serviceSpec.Ports { - if serviceSpec.Ports[i].Name != tt.wantPorts[i].Name { - t.Errorf("expected name %s, actual name %s", tt.wantPorts[i].Name, serviceSpec.Ports[i].Name) - } - if serviceSpec.Ports[i].Port != tt.wantPorts[i].Port { - t.Errorf("expected port number is %v, actual %v", tt.wantPorts[i].Port, serviceSpec.Ports[i].Port) + } else if err == nil { + if !reflect.DeepEqual(serviceSpec.Selector, tt.labels) { + t.Errorf("expected service selector is %v, actual %v", tt.labels, serviceSpec.Selector) + } + if len(serviceSpec.Ports) != len(tt.wantPorts) { + t.Errorf("expected service ports length is %v, actual %v", len(tt.wantPorts), len(serviceSpec.Ports)) + } else { + for i := range serviceSpec.Ports { + if serviceSpec.Ports[i].Name != tt.wantPorts[i].Name { + t.Errorf("expected name %s, actual name %s", tt.wantPorts[i].Name, serviceSpec.Ports[i].Name) + } + if serviceSpec.Ports[i].Port != tt.wantPorts[i].Port { + t.Errorf("expected port number is %v, actual %v", tt.wantPorts[i].Port, serviceSpec.Ports[i].Port) + } } } } @@ -1095,11 +1089,10 @@ func TestGetPortExposure(t *testing.T) { } mapCreated, err := getPortExposure(devObj, tt.filterOptions) - if !tt.wantErr && err != nil { - t.Errorf("TestGetPortExposure unexpected error: %v", err) - } else if tt.wantErr && err == nil { - t.Errorf("TestGetPortExposure expected error but got nil") - } else if !reflect.DeepEqual(mapCreated, tt.wantMap) { + // Checks for unexpected error cases + if !tt.wantErr == (err != nil) { + t.Errorf("TestGetPortExposure unexpected error %v, wantErr %v", err, tt.wantErr) + } else if err == nil && !reflect.DeepEqual(mapCreated, tt.wantMap) { t.Errorf("TestGetPortExposure Expected: %v, got %v", tt.wantMap, mapCreated) } @@ -1233,16 +1226,16 @@ func TestGetPVCSpec(t *testing.T) { // Checks for unexpected error cases if !tt.wantErr == (err != nil) { t.Errorf("resource.ParseQuantity unexpected error %v, wantErr %v", err, tt.wantErr) - } - - pvcSpec := getPVCSpec(quantity) - if pvcSpec.AccessModes[0] != corev1.ReadWriteOnce { - t.Errorf("AccessMode Error: expected %s, actual %s", corev1.ReadWriteMany, pvcSpec.AccessModes[0]) - } + } else if err == nil { + pvcSpec := getPVCSpec(quantity) + if pvcSpec.AccessModes[0] != corev1.ReadWriteOnce { + t.Errorf("AccessMode Error: expected %s, actual %s", corev1.ReadWriteMany, pvcSpec.AccessModes[0]) + } - pvcSpecQuantity := pvcSpec.Resources.Requests["storage"] - if pvcSpecQuantity.String() != quantity.String() { - t.Errorf("pvcSpec.Resources.Requests Error: expected %v, actual %v", pvcSpecQuantity.String(), quantity.String()) + pvcSpecQuantity := pvcSpec.Resources.Requests["storage"] + if pvcSpecQuantity.String() != quantity.String() { + t.Errorf("pvcSpec.Resources.Requests Error: expected %v, actual %v", pvcSpecQuantity.String(), quantity.String()) + } } }) } diff --git a/pkg/devfile/parser/data/v2/commands.go b/pkg/devfile/parser/data/v2/commands.go index 9579740a..618be2aa 100644 --- a/pkg/devfile/parser/data/v2/commands.go +++ b/pkg/devfile/parser/data/v2/commands.go @@ -66,19 +66,13 @@ func (d *DevfileV2) AddCommands(commands []v1.Command) error { // UpdateCommand updates the command with the given id // return an error if the command is not found func (d *DevfileV2) UpdateCommand(command v1.Command) error { - found := false for i := range d.Commands { if d.Commands[i].Id == command.Id { - found = true d.Commands[i] = command - d.Commands[i].Id = d.Commands[i].Id - break + return nil } } - if !found { - return fmt.Errorf("update command failed: command %s not found", command.Id) - } - return nil + return fmt.Errorf("update command failed: command %s not found", command.Id) } // DeleteCommand removes the specified command diff --git a/pkg/devfile/parser/data/v2/commands_test.go b/pkg/devfile/parser/data/v2/commands_test.go index a12e684d..24ec24bc 100644 --- a/pkg/devfile/parser/data/v2/commands_test.go +++ b/pkg/devfile/parser/data/v2/commands_test.go @@ -295,11 +295,10 @@ func TestDevfile200_AddCommands(t *testing.T) { }, } - got := d.AddCommands(tt.newCommands) - if !tt.wantErr && got != nil { - t.Errorf("TestDevfile200_AddCommands() unexpected error - %v", got) - } else if tt.wantErr && got == nil { - t.Errorf("TestDevfile200_AddCommands() wanted an error but got nil") + err := d.AddCommands(tt.newCommands) + // Unexpected error + if (err != nil) != tt.wantErr { + t.Errorf("TestDevfile200_AddCommands() error = %v, wantErr %v", err, tt.wantErr) } }) } @@ -383,30 +382,26 @@ func TestDevfile200_UpdateCommands(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("TestDevfile200_UpdateCommands() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return - } - - commands, err := d.GetCommands(common.DevfileOptions{}) - if err != nil { - t.Errorf("TestDevfile200_UpdateCommands() unxpected error %v", err) - return - } + } else if err == nil { + commands, err := d.GetCommands(common.DevfileOptions{}) + if err != nil { + t.Errorf("TestDevfile200_UpdateCommands() unxpected error %v", err) + return + } - matched := false - for _, devfileCommand := range commands { - if tt.newCommand.Id == devfileCommand.Id { - matched = true - if !reflect.DeepEqual(devfileCommand, tt.newCommand) { - t.Errorf("TestDevfile200_UpdateCommands() command mismatch - wanted %+v, got %+v", tt.newCommand, devfileCommand) + matched := false + for _, devfileCommand := range commands { + if tt.newCommand.Id == devfileCommand.Id { + matched = true + if !reflect.DeepEqual(devfileCommand, tt.newCommand) { + t.Errorf("TestDevfile200_UpdateCommands() command mismatch - wanted %+v, got %+v", tt.newCommand, devfileCommand) + } } } - } - if !matched { - t.Errorf("TestDevfile200_UpdateCommands() command mismatch - did not find command with id %s", tt.newCommand.Id) + if !matched { + t.Errorf("TestDevfile200_UpdateCommands() command mismatch - did not find command with id %s", tt.newCommand.Id) + } } }) } @@ -483,7 +478,6 @@ func TestDeleteCommands(t *testing.T) { err := d.DeleteCommand(tt.commandToDelete) if (err != nil) != tt.wantErr { t.Errorf("DeleteCommand() error = %v, wantErr %v", err, tt.wantErr) - return } else if err == nil { assert.Equal(t, tt.wantCommands, d.Commands, "The two values should be the same.") } diff --git a/pkg/devfile/parser/data/v2/common/command_helper_test.go b/pkg/devfile/parser/data/v2/common/command_helper_test.go index c4454407..a4398f9d 100644 --- a/pkg/devfile/parser/data/v2/common/command_helper_test.go +++ b/pkg/devfile/parser/data/v2/common/command_helper_test.go @@ -324,9 +324,7 @@ func TestGetCommandType(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("TestGetCommandType() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.commandType { + } else if err == nil && got != tt.commandType { t.Errorf("TestGetCommandType error: command type mismatch, expected: %v got: %v", tt.commandType, got) } }) diff --git a/pkg/devfile/parser/data/v2/common/component_helper_test.go b/pkg/devfile/parser/data/v2/common/component_helper_test.go index aa16d1c8..5266509b 100644 --- a/pkg/devfile/parser/data/v2/common/component_helper_test.go +++ b/pkg/devfile/parser/data/v2/common/component_helper_test.go @@ -179,9 +179,7 @@ func TestGetComponentType(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("TestGetComponentType() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.componentType { + } else if err == nil && got != tt.componentType { t.Errorf("TestGetComponentType error: component type mismatch, expected: %v got: %v", tt.componentType, got) } }) diff --git a/pkg/devfile/parser/data/v2/common/options_test.go b/pkg/devfile/parser/data/v2/common/options_test.go index 80369c28..9a6147fa 100644 --- a/pkg/devfile/parser/data/v2/common/options_test.go +++ b/pkg/devfile/parser/data/v2/common/options_test.go @@ -63,11 +63,10 @@ func TestFilterDevfileObject(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { filterIn, err := FilterDevfileObject(tt.attributes, tt.options) - if !tt.wantErr && err != nil { - t.Errorf("TestFilterDevfileObject unexpected error - %v", err) - } else if tt.wantErr && err == nil { - t.Errorf("TestFilterDevfileObject wanted error got nil") - } else if filterIn != tt.wantFilter { + // Unexpected error + if (err != nil) != tt.wantErr { + t.Errorf("TestFilterDevfileObject() error = %v, wantErr %v", err, tt.wantErr) + } else if err == nil && filterIn != tt.wantFilter { t.Errorf("TestFilterDevfileObject error - expected %v got %v", tt.wantFilter, filterIn) } }) diff --git a/pkg/devfile/parser/data/v2/common/project_helper_test.go b/pkg/devfile/parser/data/v2/common/project_helper_test.go index 397a3adb..484f0381 100644 --- a/pkg/devfile/parser/data/v2/common/project_helper_test.go +++ b/pkg/devfile/parser/data/v2/common/project_helper_test.go @@ -103,16 +103,16 @@ func TestGitLikeProjectSource_GetDefaultSource(t *testing.T) { got1, got2, got3, err := GetDefaultSource(tt.gitLikeProjectSource) if (err != nil) != tt.wantErr { t.Errorf("GitLikeProjectSource.GetDefaultSource() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got1 != tt.want1 { - t.Errorf("GitLikeProjectSource.GetDefaultSource() got1 = %v, want %v", got1, tt.want1) - } - if got2 != tt.want2 { - t.Errorf("GitLikeProjectSource.GetDefaultSource() got2 = %v, want %v", got2, tt.want2) - } - if got3 != tt.want3 { - t.Errorf("GitLikeProjectSource.GetDefaultSource() got2 = %v, want %v", got3, tt.want3) + } else if err == nil { + if got1 != tt.want1 { + t.Errorf("GitLikeProjectSource.GetDefaultSource() got1 = %v, want %v", got1, tt.want1) + } + if got2 != tt.want2 { + t.Errorf("GitLikeProjectSource.GetDefaultSource() got2 = %v, want %v", got2, tt.want2) + } + if got3 != tt.want3 { + t.Errorf("GitLikeProjectSource.GetDefaultSource() got2 = %v, want %v", got3, tt.want3) + } } }) } @@ -170,9 +170,7 @@ func TestGetProjectSrcType(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("TestGetProjectSrcType() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.projectSrcType { + } else if err == nil && got != tt.projectSrcType { t.Errorf("TestGetProjectSrcType error: project src type mismatch, expected: %v got: %v", tt.projectSrcType, got) } }) diff --git a/pkg/devfile/parser/data/v2/components.go b/pkg/devfile/parser/data/v2/components.go index f62d30f8..98c4badc 100644 --- a/pkg/devfile/parser/data/v2/components.go +++ b/pkg/devfile/parser/data/v2/components.go @@ -90,18 +90,13 @@ func (d *DevfileV2) AddComponents(components []v1.Component) error { // UpdateComponent updates the component with the given name // return an error if the component is not found func (d *DevfileV2) UpdateComponent(component v1.Component) error { - found := false for i := range d.Components { if d.Components[i].Name == component.Name { d.Components[i] = component - found = true - break + return nil } } - if !found { - return fmt.Errorf("update component failed: component %s not found", component.Name) - } - return nil + return fmt.Errorf("update component failed: component %s not found", component.Name) } // DeleteComponent removes the specified component diff --git a/pkg/devfile/parser/data/v2/components_test.go b/pkg/devfile/parser/data/v2/components_test.go index 72bcaeb8..0d82a902 100644 --- a/pkg/devfile/parser/data/v2/components_test.go +++ b/pkg/devfile/parser/data/v2/components_test.go @@ -84,14 +84,11 @@ func TestDevfile200_AddComponent(t *testing.T) { }, } - got := d.AddComponents(tt.newComponents) - - if !tt.wantErr && got != nil { - t.Errorf("TestDevfile200_AddComponents() unexpected error - %+v", got) - } else if tt.wantErr && got == nil { - t.Errorf("TestDevfile200_AddComponents() expected error but got nil") + err := d.AddComponents(tt.newComponents) + // Unexpected error + if (err != nil) != tt.wantErr { + t.Errorf("TestDevfile200_AddComponents() error = %v, wantErr %v", err, tt.wantErr) } - }) } } @@ -179,28 +176,24 @@ func TestDevfile200_UpdateComponent(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("TestDevfile200_UpdateComponent() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return - } - - components, err := d.GetComponents(common.DevfileOptions{}) - if err != nil { - t.Errorf("TestDevfile200_UpdateComponent() unxpected error %v", err) - return - } + } else if err == nil { + components, err := d.GetComponents(common.DevfileOptions{}) + if err != nil { + t.Errorf("TestDevfile200_UpdateComponent() unxpected error %v", err) + return + } - matched := false - for _, component := range components { - if reflect.DeepEqual(component, tt.newComponent) { - matched = true - break + matched := false + for _, component := range components { + if reflect.DeepEqual(component, tt.newComponent) { + matched = true + break + } } - } - if !matched { - t.Error("TestDevfile200_UpdateComponent() error updating the component") + if !matched { + t.Error("TestDevfile200_UpdateComponent() error updating the component") + } } }) } @@ -508,11 +501,9 @@ func TestGetDevfileContainerComponents(t *testing.T) { } devfileComponents, err := d.GetDevfileContainerComponents(tt.filterOptions) - if !tt.wantErr && err != nil { - t.Errorf("TestGetDevfileContainerComponents unexpected error: %v", err) - } else if tt.wantErr && err == nil { - t.Errorf("TestGetDevfileContainerComponents expected error but got nil") - } else if len(devfileComponents) != tt.expectedMatchesCount { + if (err != nil) != tt.wantErr { + t.Errorf("TestGetDevfileContainerComponents() error = %v, wantErr %v", err, tt.wantErr) + } else if err == nil && len(devfileComponents) != tt.expectedMatchesCount { t.Errorf("TestGetDevfileContainerComponents error: wrong number of components matched: expected %v, actual %v", tt.expectedMatchesCount, len(devfileComponents)) } }) @@ -619,11 +610,9 @@ func TestGetDevfileVolumeComponents(t *testing.T) { }, } devfileComponents, err := d.GetDevfileVolumeComponents(tt.filterOptions) - if !tt.wantErr && err != nil { - t.Errorf("TestGetDevfileVolumeComponents unexpected error: %v", err) - } else if tt.wantErr && err == nil { - t.Errorf("TestGetDevfileVolumeComponents expected error but got nil") - } else if len(devfileComponents) != tt.expectedMatchesCount { + if (err != nil) != tt.wantErr { + t.Errorf("TestGetDevfileVolumeComponents() error = %v, wantErr %v", err, tt.wantErr) + } else if err == nil && len(devfileComponents) != tt.expectedMatchesCount { t.Errorf("TestGetDevfileVolumeComponents error: wrong number of components matched: expected %v, actual %v", tt.expectedMatchesCount, len(devfileComponents)) } }) diff --git a/pkg/devfile/parser/data/v2/projects.go b/pkg/devfile/parser/data/v2/projects.go index 733c8a42..1499a551 100644 --- a/pkg/devfile/parser/data/v2/projects.go +++ b/pkg/devfile/parser/data/v2/projects.go @@ -60,18 +60,13 @@ func (d *DevfileV2) AddProjects(projects []v1.Project) error { // UpdateProject updates the slice of Devfile projects parsed from the Devfile // return an error if the project is not found func (d *DevfileV2) UpdateProject(project v1.Project) error { - found := false for i := range d.Projects { if d.Projects[i].Name == project.Name { - found = true d.Projects[i] = project - break + return nil } } - if !found { - return fmt.Errorf("update project failed: project %s not found", project.Name) - } - return nil + return fmt.Errorf("update project failed: project %s not found", project.Name) } // DeleteProject removes the specified project @@ -142,18 +137,13 @@ func (d *DevfileV2) AddStarterProjects(projects []v1.StarterProject) error { // UpdateStarterProject updates the slice of Devfile starter projects parsed from the Devfile func (d *DevfileV2) UpdateStarterProject(project v1.StarterProject) error { - found := false for i := range d.StarterProjects { if d.StarterProjects[i].Name == project.Name { - found = true d.StarterProjects[i] = project - break + return nil } } - if !found { - return fmt.Errorf("update starter project failed: starter project %s not found", project.Name) - } - return nil + return fmt.Errorf("update starter project failed: starter project %s not found", project.Name) } // DeleteStarterProject removes the specified starter project diff --git a/pkg/devfile/parser/data/v2/projects_test.go b/pkg/devfile/parser/data/v2/projects_test.go index d8de22d6..cc8394be 100644 --- a/pkg/devfile/parser/data/v2/projects_test.go +++ b/pkg/devfile/parser/data/v2/projects_test.go @@ -223,20 +223,14 @@ func TestDevfile200_AddProjects(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := d.AddProjects(tt.args) - if err != nil { - if !tt.wantErr { - t.Errorf("unexpected error: %v", err) - } - return - } - if tt.wantErr { - t.Errorf("expected error, got %v", err) - return - } - wantProjects := append(currentProject, tt.args...) + if (err != nil) != tt.wantErr { + t.Errorf("TestDevfile200_AddProjects() error = %v, wantErr %v", err, tt.wantErr) + } else if err == nil { + wantProjects := append(currentProject, tt.args...) - if !reflect.DeepEqual(d.Projects, wantProjects) { - t.Errorf("wanted: %v, got: %v, difference at %v", wantProjects, d.Projects, pretty.Compare(wantProjects, d.Projects)) + if !reflect.DeepEqual(d.Projects, wantProjects) { + t.Errorf("wanted: %v, got: %v, difference at %v", wantProjects, d.Projects, pretty.Compare(wantProjects, d.Projects)) + } } }) } @@ -296,7 +290,7 @@ func TestDevfile200_UpdateProject(t *testing.T) { wantErr: false, }, { - name: "It should not fail to update project for non existing project", + name: "It should fail to update project for non existing project", args: v1.Project{ Name: "quarkus-starter", ClonePath: "/project", @@ -329,13 +323,7 @@ func TestDevfile200_UpdateProject(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("TestDevfile200_UpdateProject() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return - } - - if !reflect.DeepEqual(tt.devfilev2, tt.expectedDevfilev2) { + } else if err == nil && !reflect.DeepEqual(tt.devfilev2, tt.expectedDevfilev2) { t.Errorf("TestDevfile200_UpdateProject() - wanted: %v, got: %v, difference at %v", tt.expectedDevfilev2, tt.devfilev2, pretty.Compare(tt.expectedDevfilev2, tt.devfilev2)) } }) @@ -614,20 +602,14 @@ func TestDevfile200_AddStarterProjects(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := d.AddStarterProjects(tt.args) - if err != nil { - if !tt.wantErr { - t.Errorf("unexpected error: %v", err) - } - return - } - if tt.wantErr { - t.Errorf("expected error, got %v", err) - return - } - wantProjects := append(currentProject, tt.args...) + if (err != nil) != tt.wantErr { + t.Errorf("TestDevfile200_AddStarterProjects() error = %v, wantErr %v", err, tt.wantErr) + } else if err == nil { + wantProjects := append(currentProject, tt.args...) - if !reflect.DeepEqual(d.StarterProjects, wantProjects) { - t.Errorf("wanted: %v, got: %v, difference at %v", wantProjects, d.StarterProjects, pretty.Compare(wantProjects, d.StarterProjects)) + if !reflect.DeepEqual(d.StarterProjects, wantProjects) { + t.Errorf("wanted: %v, got: %v, difference at %v", wantProjects, d.StarterProjects, pretty.Compare(wantProjects, d.StarterProjects)) + } } }) } @@ -720,13 +702,7 @@ func TestDevfile200_UpdateStarterProject(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("TestDevfile200_UpdateStarterProject() error = %v, wantErr %v", err, tt.wantErr) - return - } - if err != nil { - return - } - - if !reflect.DeepEqual(tt.devfilev2, tt.expectedDevfilev2) { + } else if err == nil && !reflect.DeepEqual(tt.devfilev2, tt.expectedDevfilev2) { t.Errorf("TestDevfile200_UpdateStarterProject() - wanted: %v, got: %v, difference at %v", tt.expectedDevfilev2, tt.devfilev2, pretty.Compare(tt.expectedDevfilev2, tt.devfilev2)) } }) diff --git a/pkg/devfile/parser/parse_test.go b/pkg/devfile/parser/parse_test.go index 9a11b8c1..d32976ea 100644 --- a/pkg/devfile/parser/parse_test.go +++ b/pkg/devfile/parser/parse_test.go @@ -2159,15 +2159,7 @@ func Test_parseParentAndPluginFromURI(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("parseParentAndPlugin() error = %v, wantErr %v", err, tt.wantErr) - return - } - - // Expected error and got an err - if tt.wantErr && err != nil { - return - } - - if !reflect.DeepEqual(tt.args.devFileObj.Data, tt.wantDevFile.Data) { + } else if err == nil && !reflect.DeepEqual(tt.args.devFileObj.Data, tt.wantDevFile.Data) { t.Errorf("wanted: %v, got: %v, difference at %v", tt.wantDevFile.Data, tt.args.devFileObj.Data, pretty.Compare(tt.args.devFileObj.Data, tt.wantDevFile.Data)) } }) @@ -2753,15 +2745,7 @@ func Test_parseParentFromRegistry(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("parseParentAndPlugin() error = %v, wantErr %v", err, tt.wantErr) - return - } - - // Expected error and got an err - if tt.wantErr && err != nil { - return - } - - if !reflect.DeepEqual(tt.mainDevfile.Data, tt.wantDevFile.Data) { + } else if err == nil && !reflect.DeepEqual(tt.mainDevfile.Data, tt.wantDevFile.Data) { t.Errorf("wanted: %v, got: %v, difference at %v", tt.wantDevFile.Data, tt.mainDevfile.Data, pretty.Compare(tt.mainDevfile.Data, tt.wantDevFile.Data)) } @@ -3053,15 +3037,7 @@ func Test_parseParentFromKubeCRD(t *testing.T) { // Unexpected error if (err != nil) != tt.wantErr { t.Errorf("parseParentAndPlugin() error = %v, wantErr %v", err, tt.wantErr) - return - } - - // Expected error and got an err - if tt.wantErr && err != nil { - return - } - - if !reflect.DeepEqual(tt.mainDevfile.Data, tt.wantDevFile.Data) { + } else if err == nil && !reflect.DeepEqual(tt.mainDevfile.Data, tt.wantDevFile.Data) { t.Errorf("wanted: %v, got: %v, difference at %v", tt.wantDevFile.Data, tt.mainDevfile.Data, pretty.Compare(tt.mainDevfile.Data, tt.wantDevFile.Data)) } @@ -3294,10 +3270,7 @@ func Test_parseFromURI(t *testing.T) { got, err := parseFromURI(tt.importReference, tt.curDevfileCtx, &resolutionContextTree{}, resolverTools{}) if tt.wantErr == (err == nil) { t.Errorf("Test_parseFromURI() error = %v, wantErr %v", err, tt.wantErr) - return - } - - if !reflect.DeepEqual(got.Data, tt.wantDevFile.Data) { + } else if err == nil && !reflect.DeepEqual(got.Data, tt.wantDevFile.Data) { t.Errorf("wanted: %v, got: %v, difference at %v", tt.wantDevFile, got, pretty.Compare(tt.wantDevFile, got)) } }) @@ -3448,10 +3421,7 @@ func Test_parseFromRegistry(t *testing.T) { got, err := parseFromRegistry(tt.importReference, &resolutionContextTree{}, tt.tool) if tt.wantErr == (err == nil) { t.Errorf("Test_parseFromRegistry() error = %v, wantErr %v", err, tt.wantErr) - return - } - - if err == nil && !reflect.DeepEqual(got.Data, tt.wantDevFile.Data) { + } else if err == nil && !reflect.DeepEqual(got.Data, tt.wantDevFile.Data) { t.Errorf("wanted: %v, got: %v, difference at %v", tt.wantDevFile, got, pretty.Compare(tt.wantDevFile, got)) } }) @@ -3550,10 +3520,7 @@ func Test_parseFromKubeCRD(t *testing.T) { got, err := parseFromKubeCRD(tt.importReference, &resolutionContextTree{}, tool) if tt.wantErr == (err == nil) { t.Errorf("Test_parseFromKubeCRD() error = %v, wantErr %v", err, tt.wantErr) - return - } - - if err == nil && !reflect.DeepEqual(got.Data, tt.wantDevFile.Data) { + } else if err == nil && !reflect.DeepEqual(got.Data, tt.wantDevFile.Data) { t.Errorf("wanted: %v, got: %v, difference at %v", tt.wantDevFile, got, pretty.Compare(tt.wantDevFile, got)) } }) diff --git a/pkg/devfile/parser/sourceAttribute_test.go b/pkg/devfile/parser/sourceAttribute_test.go index f85fd55d..2f977451 100644 --- a/pkg/devfile/parser/sourceAttribute_test.go +++ b/pkg/devfile/parser/sourceAttribute_test.go @@ -145,14 +145,7 @@ func TestAddSourceAttributesForOverrideAndMerge(t *testing.T) { if tt.wantErr == (err == nil) { t.Errorf("Test_AddSourceAttributesForOverrideAndMerge() error = %v, wantErr %v", err, tt.wantErr) - return - } - - if err != nil { - return - } - - if !reflect.DeepEqual(tt.template, tt.wantResult) { + } else if err == nil && !reflect.DeepEqual(tt.template, tt.wantResult) { t.Errorf("wanted: %v, got: %v, difference at %v", tt.wantResult, tt.template, pretty.Compare(tt.template, tt.wantResult)) }