Skip to content

Commit a2794ba

Browse files
committed
fixed incorrect unit tests for target diff algorithm and fixed incorrect behaviour of the diff algorithm
1 parent 5e2c1bd commit a2794ba

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

service/reconfigure.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func diffTargets(oldTargets, newTargets []task.Target) (additions, removals []ta
139139
}
140140
}
141141
if !exists {
142-
removals = append(removals, newTarget)
142+
additions = append(additions, newTarget)
143143
}
144144
}
145145
for _, oldTarget := range oldTargets {
@@ -152,9 +152,9 @@ func diffTargets(oldTargets, newTargets []task.Target) (additions, removals []ta
152152
}
153153
}
154154
if !exists {
155-
additions = append(additions, oldTarget)
155+
removals = append(removals, oldTarget)
156156
} else if !reflect.DeepEqual(oldTarget, newTarget) {
157-
additions = append(additions, oldTarget)
157+
additions = append(additions, newTarget)
158158
}
159159
}
160160
return

service/reconfigure_test.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package service
22

33
import (
44
"fmt"
5-
"reflect"
65
"testing"
76

7+
"github.com/stretchr/testify/assert"
8+
89
"github.com/Southclaws/wadsworth/service/task"
910
)
1011

1112
func Test_diffTargets(t *testing.T) {
1213
type args struct {
13-
newTargets []task.Target
1414
oldTargets []task.Target
15+
newTargets []task.Target
1516
}
1617
tests := []struct {
1718
args args
@@ -34,6 +35,38 @@ func Test_diffTargets(t *testing.T) {
3435
nil,
3536
nil,
3637
},
38+
{
39+
args{
40+
oldTargets: []task.Target{},
41+
newTargets: []task.Target{
42+
{Name: "one"},
43+
{Name: "two"},
44+
{Name: "three"},
45+
},
46+
},
47+
[]task.Target{
48+
{Name: "one"},
49+
{Name: "two"},
50+
{Name: "three"},
51+
},
52+
nil,
53+
},
54+
{
55+
args{
56+
oldTargets: []task.Target{
57+
{Name: "one"},
58+
{Name: "two"},
59+
{Name: "three"},
60+
},
61+
newTargets: []task.Target{},
62+
},
63+
nil,
64+
[]task.Target{
65+
{Name: "one"},
66+
{Name: "two"},
67+
{Name: "three"},
68+
},
69+
},
3770
{
3871
args{
3972
oldTargets: []task.Target{
@@ -89,13 +122,9 @@ func Test_diffTargets(t *testing.T) {
89122
}
90123
for ii, tt := range tests {
91124
t.Run(fmt.Sprint(ii), func(t *testing.T) {
92-
gotAdditions, gotRemovals := diffTargets(tt.args.newTargets, tt.args.oldTargets)
93-
if !reflect.DeepEqual(gotAdditions, tt.wantAdditions) {
94-
t.Errorf("diffTargets() gotAdditions = %v, want %v", gotAdditions, tt.wantAdditions)
95-
}
96-
if !reflect.DeepEqual(gotRemovals, tt.wantRemovals) {
97-
t.Errorf("diffTargets() gotRemovals = %v, want %v", gotRemovals, tt.wantRemovals)
98-
}
125+
gotAdditions, gotRemovals := diffTargets(tt.args.oldTargets, tt.args.newTargets)
126+
assert.Equal(t, tt.wantAdditions, gotAdditions, "additions mismatch")
127+
assert.Equal(t, tt.wantRemovals, gotRemovals, "removals mismatch")
99128
})
100129
}
101130
}

0 commit comments

Comments
 (0)