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