Skip to content

Commit 5e32c06

Browse files
committed
Address review feedback 2
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent 6f010e1 commit 5e32c06

9 files changed

+32
-72
lines changed

pkg/devfile/parser/data/v2/commands.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,15 @@ func (d *DevfileV2) UpdateCommand(command v1.Command) {
5757
// DeleteCommand removes the specified command
5858
func (d *DevfileV2) DeleteCommand(id string) error {
5959

60-
found := false
6160
for i := range d.Commands {
6261
if d.Commands[i].Id == id {
63-
found = true
6462
d.Commands = append(d.Commands[:i], d.Commands[i+1:]...)
65-
break
63+
return nil
6664
}
6765
}
6866

69-
if !found {
70-
return &common.FieldNotFoundError{
71-
Field: "command",
72-
Name: id,
73-
}
67+
return &common.FieldNotFoundError{
68+
Field: "command",
69+
Name: id,
7470
}
75-
76-
return nil
7771
}

pkg/devfile/parser/data/v2/commands_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,8 @@ func TestDeleteCommands(t *testing.T) {
373373
err := d.DeleteCommand(tt.commandToDelete)
374374
if (err != nil) != tt.wantErr {
375375
t.Errorf("DeleteCommand() error = %v, wantErr %v", err, tt.wantErr)
376-
}
377-
378-
if err == nil && !tt.wantErr {
376+
return
377+
} else if err == nil {
379378
assert.Equal(t, tt.wantCommands, d.Commands, "The two values should be the same.")
380379
}
381380
})

pkg/devfile/parser/data/v2/components.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,15 @@ func (d *DevfileV2) UpdateComponent(component v1.Component) {
8888
// DeleteComponent removes the specified component
8989
func (d *DevfileV2) DeleteComponent(name string) error {
9090

91-
found := false
9291
for i := range d.Components {
9392
if d.Components[i].Name == name {
94-
found = true
9593
d.Components = append(d.Components[:i], d.Components[i+1:]...)
96-
break
94+
return nil
9795
}
9896
}
9997

100-
if !found {
101-
return &common.FieldNotFoundError{
102-
Field: "component",
103-
Name: name,
104-
}
98+
return &common.FieldNotFoundError{
99+
Field: "component",
100+
Name: name,
105101
}
106-
107-
return nil
108102
}

pkg/devfile/parser/data/v2/components_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,7 @@ func TestDeleteComponents(t *testing.T) {
488488
err := d.DeleteComponent(tt.componentToDelete)
489489
if (err != nil) != tt.wantErr {
490490
t.Errorf("DeleteComponent() error = %v, wantErr %v", err, tt.wantErr)
491-
}
492-
493-
if err == nil && !tt.wantErr {
491+
} else if err == nil {
494492
assert.Equal(t, tt.wantComponents, d.Components, "The two values should be the same.")
495493
}
496494
})

pkg/devfile/parser/data/v2/events_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ func TestDevfile200_AddEvents(t *testing.T) {
6868

6969
err := d.AddEvents(tt.newEvents)
7070

71-
if !tt.wantErr && err != nil {
72-
t.Errorf("TestDevfile200_AddEvents() unexpected error - %+v", err)
73-
} else if tt.wantErr && err == nil {
74-
t.Errorf("TestDevfile200_AddEvents() expected error but got nil")
71+
if (err != nil) != tt.wantErr {
72+
t.Errorf("DeleteCommand() error = %v, wantErr %v", err, tt.wantErr)
7573
}
7674

7775
})

pkg/devfile/parser/data/v2/projects.go

+8-20
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,17 @@ func (d *DevfileV2) UpdateProject(project v1.Project) {
5858
// DeleteProject removes the specified project
5959
func (d *DevfileV2) DeleteProject(name string) error {
6060

61-
found := false
6261
for i := range d.Projects {
6362
if d.Projects[i].Name == name {
64-
found = true
6563
d.Projects = append(d.Projects[:i], d.Projects[i+1:]...)
66-
break
64+
return nil
6765
}
6866
}
6967

70-
if !found {
71-
return &common.FieldNotFoundError{
72-
Field: "project",
73-
Name: name,
74-
}
68+
return &common.FieldNotFoundError{
69+
Field: "project",
70+
Name: name,
7571
}
76-
77-
return nil
7872
}
7973

8074
//GetStarterProjects returns the DevfileStarterProject parsed from devfile
@@ -128,21 +122,15 @@ func (d *DevfileV2) UpdateStarterProject(project v1.StarterProject) {
128122
// DeleteStarterProject removes the specified starter project
129123
func (d *DevfileV2) DeleteStarterProject(name string) error {
130124

131-
found := false
132125
for i := range d.StarterProjects {
133126
if d.StarterProjects[i].Name == name {
134-
found = true
135127
d.StarterProjects = append(d.StarterProjects[:i], d.StarterProjects[i+1:]...)
136-
break
128+
return nil
137129
}
138130
}
139131

140-
if !found {
141-
return &common.FieldNotFoundError{
142-
Field: "starter project",
143-
Name: name,
144-
}
132+
return &common.FieldNotFoundError{
133+
Field: "starter project",
134+
Name: name,
145135
}
146-
147-
return nil
148136
}

pkg/devfile/parser/data/v2/projects_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ func TestDevfile200_DeleteProject(t *testing.T) {
238238
err := d.DeleteProject(tt.projectToDelete)
239239
if (err != nil) != tt.wantErr {
240240
t.Errorf("DeleteProject() error = %v, wantErr %v", err, tt.wantErr)
241-
}
242-
243-
if err == nil && !tt.wantErr {
241+
} else if err == nil {
244242
assert.Equal(t, tt.wantProjects, d.Projects, "The two values should be the same.")
245243
}
246244
})
@@ -480,9 +478,7 @@ func TestDevfile200_DeleteStarterProject(t *testing.T) {
480478
err := d.DeleteStarterProject(tt.starterProjectToDelete)
481479
if (err != nil) != tt.wantErr {
482480
t.Errorf("DeleteStarterProject() error = %v, wantErr %v", err, tt.wantErr)
483-
}
484-
485-
if err == nil && !tt.wantErr {
481+
} else if err == nil {
486482
assert.Equal(t, tt.wantStarterProjects, d.StarterProjects, "The two values should be the same.")
487483
}
488484
})

pkg/devfile/parser/data/v2/volumes.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func (d *DevfileV2) DeleteVolumeMount(name string) error {
4747
found := false
4848
for i := range d.Components {
4949
if d.Components[i].Container != nil && d.Components[i].Name != name {
50+
// Volume Mounts can have multiple instances of a volume mounted at different paths
51+
// As arrays are rearraged/shifted for deletion, we lose one element every time there is a match
52+
// Looping backward is efficient, otherwise we would have to manually decrement counter
53+
// if we looped forward
5054
for j := len(d.Components[i].Container.VolumeMounts) - 1; j >= 0; j-- {
5155
if d.Components[i].Container.VolumeMounts[j].Name == name {
5256
found = true
@@ -68,17 +72,14 @@ func (d *DevfileV2) DeleteVolumeMount(name string) error {
6872

6973
// GetVolumeMountPath gets the mount path of the specified volume mount from the specified container component
7074
func (d *DevfileV2) GetVolumeMountPath(mountName, componentName string) (string, error) {
71-
mountFound := false
7275
componentFound := false
73-
var path string
7476

7577
for _, component := range d.Components {
7678
if component.Container != nil && component.Name == componentName {
7779
componentFound = true
7880
for _, volumeMount := range component.Container.VolumeMounts {
7981
if volumeMount.Name == mountName {
80-
mountFound = true
81-
path = volumeMount.Path
82+
return volumeMount.Path, nil
8283
}
8384
}
8485
}
@@ -89,9 +90,7 @@ func (d *DevfileV2) GetVolumeMountPath(mountName, componentName string) (string,
8990
Field: "container component",
9091
Name: componentName,
9192
}
92-
} else if !mountFound {
93-
return "", fmt.Errorf("volume %s not mounted to component %s", mountName, componentName)
9493
}
9594

96-
return path, nil
95+
return "", fmt.Errorf("volume %s not mounted to component %s", mountName, componentName)
9796
}

pkg/devfile/parser/data/v2/volumes_test.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ func TestDevfile200_AddVolumeMount(t *testing.T) {
162162
err := d.AddVolumeMounts(tt.args.componentName, tt.args.volumeMounts)
163163
if (err != nil) != tt.wantErr {
164164
t.Errorf("AddVolumeMounts() error = %v, wantErr %v", err, tt.wantErr)
165-
}
166-
167-
if err == nil && !tt.wantErr {
165+
} else if err == nil {
168166
assert.Equal(t, tt.wantComponents, d.Components, "The two values should be the same.")
169167
}
170168
})
@@ -257,9 +255,7 @@ func TestDevfile200_DeleteVolumeMounts(t *testing.T) {
257255
err := d.DeleteVolumeMount(tt.volMountToDelete)
258256
if (err != nil) != tt.wantErr {
259257
t.Errorf("DeleteVolumeMount() error = %v, wantErr %v", err, tt.wantErr)
260-
}
261-
262-
if err == nil && !tt.wantErr {
258+
} else if err == nil {
263259
assert.Equal(t, tt.wantComponents, d.Components, "The two values should be the same.")
264260
}
265261
})
@@ -355,9 +351,7 @@ func TestDevfile200_GetVolumeMountPath(t *testing.T) {
355351
gotPath, err := d.GetVolumeMountPath(tt.mountName, tt.componentName)
356352
if (err != nil) != tt.wantErr {
357353
t.Errorf("GetVolumeMountPath() error = %v, wantErr %v", err, tt.wantErr)
358-
}
359-
360-
if err == nil && !tt.wantErr {
354+
} else if err == nil {
361355
assert.Equal(t, tt.wantPath, gotPath, "The two values should be the same.")
362356
}
363357
})

0 commit comments

Comments
 (0)