github.com/golang/dep@v0.5.4/internal/feedback/lockdiff_test.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package feedback 6 7 import ( 8 "bytes" 9 "testing" 10 11 "github.com/golang/dep/gps" 12 ) 13 14 // mkPI creates a ProjectIdentifier with the ProjectRoot as the provided 15 // string, and the Source unset. 16 // 17 // Call normalize() on the returned value if you need the Source to be be 18 // equal to the ProjectRoot. 19 func mkPI(root string) gps.ProjectIdentifier { 20 return gps.ProjectIdentifier{ 21 ProjectRoot: gps.ProjectRoot(root), 22 } 23 } 24 25 func TestStringDiff_NoChange(t *testing.T) { 26 diff := StringDiff{Previous: "foo", Current: "foo"} 27 want := "foo" 28 got := diff.String() 29 if got != want { 30 t.Fatalf("Expected '%s', got '%s'", want, got) 31 } 32 } 33 34 func TestStringDiff_Add(t *testing.T) { 35 diff := StringDiff{Current: "foo"} 36 got := diff.String() 37 if got != "+ foo" { 38 t.Fatalf("Expected '+ foo', got '%s'", got) 39 } 40 } 41 42 func TestStringDiff_Remove(t *testing.T) { 43 diff := StringDiff{Previous: "foo"} 44 want := "- foo" 45 got := diff.String() 46 if got != want { 47 t.Fatalf("Expected '%s', got '%s'", want, got) 48 } 49 } 50 51 func TestStringDiff_Modify(t *testing.T) { 52 diff := StringDiff{Previous: "foo", Current: "bar"} 53 want := "foo -> bar" 54 got := diff.String() 55 if got != want { 56 t.Fatalf("Expected '%s', got '%s'", want, got) 57 } 58 } 59 60 func TestDiffProjects_NoChange(t *testing.T) { 61 p1 := gps.NewLockedProject(mkPI("github.com/golang/dep/gps"), gps.NewVersion("v0.10.0"), []string{"gps"}) 62 p2 := gps.NewLockedProject(mkPI("github.com/golang/dep/gps"), gps.NewVersion("v0.10.0"), []string{"gps"}) 63 64 diff := DiffProjects(p1, p2) 65 if diff != nil { 66 t.Fatal("Expected the diff to be nil") 67 } 68 } 69 70 func TestDiffProjects_Modify(t *testing.T) { 71 p1 := gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewBranch("master").Pair("abc123"), []string{"baz", "qux"}) 72 p2 := gps.NewLockedProject(gps.ProjectIdentifier{ProjectRoot: "github.com/foo/bar", Source: "https://github.com/mcfork/gps.git"}, 73 gps.NewVersion("v1.0.0").Pair("def456"), []string{"baz", "derp"}) 74 75 diff := DiffProjects(p1, p2) 76 if diff == nil { 77 t.Fatal("Expected the diff to be populated") 78 } 79 80 wantSource := "+ https://github.com/mcfork/gps.git" 81 gotSource := diff.Source.String() 82 if gotSource != wantSource { 83 t.Fatalf("Expected diff.Source to be '%s', got '%s'", wantSource, diff.Source) 84 } 85 86 wantVersion := "+ v1.0.0" 87 gotVersion := diff.Version.String() 88 if gotVersion != wantVersion { 89 t.Fatalf("Expected diff.Version to be '%s', got '%s'", wantVersion, gotVersion) 90 } 91 92 wantRevision := "abc123 -> def456" 93 gotRevision := diff.Revision.String() 94 if gotRevision != wantRevision { 95 t.Fatalf("Expected diff.Revision to be '%s', got '%s'", wantRevision, gotRevision) 96 } 97 98 wantBranch := "- master" 99 gotBranch := diff.Branch.String() 100 if gotBranch != wantBranch { 101 t.Fatalf("Expected diff.Branch to be '%s', got '%s'", wantBranch, gotBranch) 102 } 103 104 fmtPkgs := func(pkgs []StringDiff) string { 105 b := bytes.NewBufferString("[") 106 for _, pkg := range pkgs { 107 b.WriteString(pkg.String()) 108 b.WriteString(",") 109 } 110 b.WriteString("]") 111 return b.String() 112 } 113 114 wantPackages := "[+ derp,- qux,]" 115 gotPackages := fmtPkgs(diff.Packages) 116 if gotPackages != wantPackages { 117 t.Fatalf("Expected diff.Packages to be '%s', got '%s'", wantPackages, gotPackages) 118 } 119 } 120 121 func TestDiffProjects_AddPackages(t *testing.T) { 122 p1 := gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewBranch("master").Pair("abc123"), []string{"foobar"}) 123 p2 := gps.NewLockedProject(gps.ProjectIdentifier{ProjectRoot: "github.com/foo/bar", Source: "https://github.com/mcfork/gps.git"}, 124 gps.NewVersion("v1.0.0").Pair("def456"), []string{"bazqux", "foobar", "zugzug"}) 125 126 diff := DiffProjects(p1, p2) 127 if diff == nil { 128 t.Fatal("Expected the diff to be populated") 129 } 130 131 if len(diff.Packages) != 2 { 132 t.Fatalf("Expected diff.Packages to have 2 packages, got %d", len(diff.Packages)) 133 } 134 135 want0 := "+ bazqux" 136 got0 := diff.Packages[0].String() 137 if got0 != want0 { 138 t.Fatalf("Expected diff.Packages[0] to contain %s, got %s", want0, got0) 139 } 140 141 want1 := "+ zugzug" 142 got1 := diff.Packages[1].String() 143 if got1 != want1 { 144 t.Fatalf("Expected diff.Packages[1] to contain %s, got %s", want1, got1) 145 } 146 } 147 148 func TestDiffProjects_RemovePackages(t *testing.T) { 149 p1 := gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewBranch("master").Pair("abc123"), []string{"athing", "foobar"}) 150 p2 := gps.NewLockedProject(gps.ProjectIdentifier{ProjectRoot: "github.com/foo/bar", Source: "https://github.com/mcfork/gps.git"}, 151 gps.NewVersion("v1.0.0").Pair("def456"), []string{"bazqux"}) 152 153 diff := DiffProjects(p1, p2) 154 if diff == nil { 155 t.Fatal("Expected the diff to be populated") 156 } 157 158 if len(diff.Packages) > 3 { 159 t.Fatalf("Expected diff.Packages to have 3 packages, got %d", len(diff.Packages)) 160 } 161 162 want0 := "- athing" 163 got0 := diff.Packages[0].String() 164 if got0 != want0 { 165 t.Fatalf("Expected diff.Packages[0] to contain %s, got %s", want0, got0) 166 } 167 168 // diff.Packages[1] is '+ bazqux' 169 170 want2 := "- foobar" 171 got2 := diff.Packages[2].String() 172 if got2 != want2 { 173 t.Fatalf("Expected diff.Packages[2] to contain %s, got %s", want2, got2) 174 } 175 } 176 177 func TestDiffLocks_NoChange(t *testing.T) { 178 l1 := gps.SimpleLock{ 179 gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewVersion("v1.0.0"), nil), 180 } 181 l2 := gps.SimpleLock{ 182 gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewVersion("v1.0.0"), nil), 183 } 184 185 diff := DiffLocks(l1, l2) 186 if diff != nil { 187 t.Fatal("Expected the diff to be nil") 188 } 189 } 190 191 func TestDiffLocks_AddProjects(t *testing.T) { 192 l1 := gps.SimpleLock{ 193 gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewVersion("v1.0.0"), nil), 194 } 195 l2 := gps.SimpleLock{ 196 gps.NewLockedProject(gps.ProjectIdentifier{ProjectRoot: "github.com/baz/qux", Source: "https://github.com/mcfork/bazqux.git"}, 197 gps.NewVersion("v0.5.0").Pair("def456"), []string{"p1", "p2"}), 198 gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewVersion("v1.0.0"), nil), 199 gps.NewLockedProject(mkPI("github.com/zug/zug"), gps.NewVersion("v1.0.0"), nil), 200 } 201 202 diff := DiffLocks(l1, l2) 203 if diff == nil { 204 t.Fatal("Expected the diff to be populated") 205 } 206 207 if len(diff.Add) != 2 { 208 t.Fatalf("Expected diff.Add to have 2 projects, got %d", len(diff.Add)) 209 } 210 211 want0 := "github.com/baz/qux" 212 got0 := string(diff.Add[0].Name) 213 if got0 != want0 { 214 t.Fatalf("Expected diff.Add[0] to contain %s, got %s", want0, got0) 215 } 216 217 want1 := "github.com/zug/zug" 218 got1 := string(diff.Add[1].Name) 219 if got1 != want1 { 220 t.Fatalf("Expected diff.Add[1] to contain %s, got %s", want1, got1) 221 } 222 223 add0 := diff.Add[0] 224 wantSource := "https://github.com/mcfork/bazqux.git" 225 gotSource := add0.Source.String() 226 if gotSource != wantSource { 227 t.Fatalf("Expected diff.Add[0].Source to be '%s', got '%s'", wantSource, add0.Source) 228 } 229 230 wantVersion := "v0.5.0" 231 gotVersion := add0.Version.String() 232 if gotVersion != wantVersion { 233 t.Fatalf("Expected diff.Add[0].Version to be '%s', got '%s'", wantVersion, gotVersion) 234 } 235 236 wantRevision := "def456" 237 gotRevision := add0.Revision.String() 238 if gotRevision != wantRevision { 239 t.Fatalf("Expected diff.Add[0].Revision to be '%s', got '%s'", wantRevision, gotRevision) 240 } 241 242 wantBranch := "" 243 gotBranch := add0.Branch.String() 244 if gotBranch != wantBranch { 245 t.Fatalf("Expected diff.Add[0].Branch to be '%s', got '%s'", wantBranch, gotBranch) 246 } 247 248 fmtPkgs := func(pkgs []StringDiff) string { 249 b := bytes.NewBufferString("[") 250 for _, pkg := range pkgs { 251 b.WriteString(pkg.String()) 252 b.WriteString(",") 253 } 254 b.WriteString("]") 255 return b.String() 256 } 257 258 wantPackages := "[p1,p2,]" 259 gotPackages := fmtPkgs(add0.Packages) 260 if gotPackages != wantPackages { 261 t.Fatalf("Expected diff.Add[0].Packages to be '%s', got '%s'", wantPackages, gotPackages) 262 } 263 } 264 265 func TestDiffLocks_RemoveProjects(t *testing.T) { 266 l1 := gps.SimpleLock{ 267 gps.NewLockedProject(gps.ProjectIdentifier{ProjectRoot: "github.com/a/thing", Source: "https://github.com/mcfork/athing.git"}, 268 gps.NewBranch("master").Pair("def456"), []string{"p1", "p2"}), 269 gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewVersion("v1.0.0"), nil), 270 } 271 l2 := gps.SimpleLock{ 272 gps.NewLockedProject(mkPI("github.com/baz/qux"), gps.NewVersion("v1.0.0"), nil), 273 } 274 275 diff := DiffLocks(l1, l2) 276 if diff == nil { 277 t.Fatal("Expected the diff to be populated") 278 } 279 280 if len(diff.Remove) != 2 { 281 t.Fatalf("Expected diff.Remove to have 2 projects, got %d", len(diff.Remove)) 282 } 283 284 want0 := "github.com/a/thing" 285 got0 := string(diff.Remove[0].Name) 286 if got0 != want0 { 287 t.Fatalf("Expected diff.Remove[0] to contain %s, got %s", want0, got0) 288 } 289 290 want1 := "github.com/foo/bar" 291 got1 := string(diff.Remove[1].Name) 292 if got1 != want1 { 293 t.Fatalf("Expected diff.Remove[1] to contain %s, got %s", want1, got1) 294 } 295 296 remove0 := diff.Remove[0] 297 wantSource := "https://github.com/mcfork/athing.git" 298 gotSource := remove0.Source.String() 299 if gotSource != wantSource { 300 t.Fatalf("Expected diff.Remove[0].Source to be '%s', got '%s'", wantSource, remove0.Source) 301 } 302 303 wantVersion := "" 304 gotVersion := remove0.Version.String() 305 if gotVersion != wantVersion { 306 t.Fatalf("Expected diff.Remove[0].Version to be '%s', got '%s'", wantVersion, gotVersion) 307 } 308 309 wantRevision := "def456" 310 gotRevision := remove0.Revision.String() 311 if gotRevision != wantRevision { 312 t.Fatalf("Expected diff.Remove[0].Revision to be '%s', got '%s'", wantRevision, gotRevision) 313 } 314 315 wantBranch := "master" 316 gotBranch := remove0.Branch.String() 317 if gotBranch != wantBranch { 318 t.Fatalf("Expected diff.Remove[0].Branch to be '%s', got '%s'", wantBranch, gotBranch) 319 } 320 321 fmtPkgs := func(pkgs []StringDiff) string { 322 b := bytes.NewBufferString("[") 323 for _, pkg := range pkgs { 324 b.WriteString(pkg.String()) 325 b.WriteString(",") 326 } 327 b.WriteString("]") 328 return b.String() 329 } 330 331 wantPackages := "[p1,p2,]" 332 gotPackages := fmtPkgs(remove0.Packages) 333 if gotPackages != wantPackages { 334 t.Fatalf("Expected diff.Remove[0].Packages to be '%s', got '%s'", wantPackages, gotPackages) 335 } 336 } 337 338 func TestDiffLocks_ModifyProjects(t *testing.T) { 339 l1 := gps.SimpleLock{ 340 gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewVersion("v1.0.0"), nil), 341 gps.NewLockedProject(mkPI("github.com/foo/bu"), gps.NewVersion("v1.0.0"), nil), 342 gps.NewLockedProject(mkPI("github.com/zig/zag"), gps.NewVersion("v1.0.0"), nil), 343 } 344 l2 := gps.SimpleLock{ 345 gps.NewLockedProject(mkPI("github.com/baz/qux"), gps.NewVersion("v1.0.0"), nil), 346 gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewVersion("v2.0.0"), nil), 347 gps.NewLockedProject(mkPI("github.com/zig/zag"), gps.NewVersion("v2.0.0"), nil), 348 gps.NewLockedProject(mkPI("github.com/zug/zug"), gps.NewVersion("v1.0.0"), nil), 349 } 350 351 diff := DiffLocks(l1, l2) 352 if diff == nil { 353 t.Fatal("Expected the diff to be populated") 354 } 355 356 if len(diff.Modify) != 2 { 357 t.Fatalf("Expected diff.Remove to have 2 projects, got %d", len(diff.Remove)) 358 } 359 360 want0 := "github.com/foo/bar" 361 got0 := string(diff.Modify[0].Name) 362 if got0 != want0 { 363 t.Fatalf("Expected diff.Modify[0] to contain %s, got %s", want0, got0) 364 } 365 366 want1 := "github.com/zig/zag" 367 got1 := string(diff.Modify[1].Name) 368 if got1 != want1 { 369 t.Fatalf("Expected diff.Modify[1] to contain %s, got %s", want1, got1) 370 } 371 } 372 373 func TestDiffLocks_EmptyInitialLock(t *testing.T) { 374 l2 := gps.SimpleLock{ 375 gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewVersion("v1.0.0"), nil), 376 } 377 378 diff := DiffLocks(nil, l2) 379 380 if len(diff.Add) != 1 { 381 t.Fatalf("Expected diff.Add to contain 1 project, got %d", len(diff.Add)) 382 } 383 } 384 385 func TestDiffLocks_EmptyFinalLock(t *testing.T) { 386 l1 := gps.SimpleLock{ 387 gps.NewLockedProject(mkPI("github.com/foo/bar"), gps.NewVersion("v1.0.0"), nil), 388 } 389 390 diff := DiffLocks(l1, nil) 391 392 if len(diff.Remove) != 1 { 393 t.Fatalf("Expected diff.Remove to contain 1 project, got %d", len(diff.Remove)) 394 } 395 } 396 397 func TestDiffLocks_EmptyLocks(t *testing.T) { 398 diff := DiffLocks(nil, nil) 399 if diff != nil { 400 t.Fatal("Expected the diff to be empty") 401 } 402 }