This repository has been archived by the owner on Feb 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 454
/
diff_test.go
105 lines (94 loc) · 2.18 KB
/
diff_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"strings"
"testing"
)
var (
d1 = fmt.Sprintf(`--- Godeps/Godeps.json
+++ $GOPATH
@@ -1,13 +1,13 @@
{
"ImportPath": "C",
"GoVersion": "go1.2",
"GodepVersion": "v%d",
"Deps": [
{
"ImportPath": "D101",
- "Comment": "D202",
+ "Comment": "D303",
"Rev": ""
}
]
}
`, version)
d2 = fmt.Sprintf(`--- Godeps/Godeps.json
+++ $GOPATH
@@ -1,13 +1,18 @@
{
"ImportPath": "C",
"GoVersion": "go1.2",
"GodepVersion": "v%d",
"Deps": [
{
"ImportPath": "D101",
"Comment": "D202",
"Rev": ""
+ },
+ {
+ "ImportPath": "D102",
+ "Comment": "D203",
+ "Rev": ""
}
]
}
`, version)
)
var (
dep1 = Godeps{
ImportPath: "C",
GoVersion: "go1.2",
Deps: []Dependency{
{ImportPath: "D101", Comment: "D202"},
},
}
dep2 = Godeps{
ImportPath: "C",
GoVersion: "go1.2",
Deps: []Dependency{
{ImportPath: "D101", Comment: "D202"},
},
}
)
func TestDiff(t *testing.T) {
// Equiv Godeps, should yield an empty diff.
diff, _ := diffStr(&dep1, &dep2)
if diff != "" {
t.Errorf("Diff is %v want ''", diff)
}
// Test modifications in packages make it to the diff.
dep2.Deps[0].Comment = "D303"
diff, _ = diffStr(&dep1, &dep2)
if !diffsEqual(strings.Fields(diff), strings.Fields(d1)) {
t.Errorf("Expecting diffs to be equal. Obs <%s>. Exp <%s>", diff, d1)
}
// Test additional packages in new Godeps
dep2.Deps[0].Comment = "D202"
dep2.Deps = append(dep2.Deps, Dependency{ImportPath: "D102", Comment: "D203"})
diff, _ = diffStr(&dep1, &dep2)
if !diffsEqual(strings.Fields(diff), strings.Fields(d2)) {
t.Errorf("Expecting diffs to be equal. Obs <%v>. Exp <%v>", diff, d2)
}
}
// diffsEqual asserts that two slices are equivalent.
func diffsEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}