github.com/azunymous/cdx@v0.0.0-20201122180449-fbb46cc4d252/vcs/gogit/repo_test.go (about) 1 package gogit 2 3 import ( 4 "github.com/azunymous/cdx/test/check" 5 "github.com/go-git/go-billy/v5/memfs" 6 "github.com/go-git/go-git/v5" 7 "strconv" 8 "testing" 9 ) 10 11 // This test assumes that it is running from a git repository and asserts that it can be parsed 12 func TestNewRepo(t *testing.T) { 13 fs := memfs.New() 14 createGitRepo(fs) 15 16 repo, err := NewRepo() 17 check.Ok(t, err) 18 log, err := repo.gitRepo.Log(&git.LogOptions{}) 19 check.Ok(t, err) 20 _, err = log.Next() 21 check.Ok(t, err) 22 } 23 24 func TestTagsForHead(t *testing.T) { 25 fs := memfs.New() 26 createGitRepo(fs) 27 createVersionTag(fs, "app-0.0.1") 28 29 repo := newTestRepo(fs) 30 tagsForHead, err := repo.TagsForHead("app") 31 check.Ok(t, err) 32 check.Equals(t, 1, len(tagsForHead)) 33 check.Equals(t, []string{"app-0.0.1"}, tagsForHead) 34 } 35 36 func TestTagsForHeadAnnotated(t *testing.T) { 37 fs := memfs.New() 38 createGitRepo(fs) 39 createAnnotatedVersionTag(fs, "app-0.0.1") 40 41 repo := newTestRepo(fs) 42 tagsForHead, err := repo.TagsForHead("app") 43 check.Ok(t, err) 44 check.Equals(t, 1, len(tagsForHead)) 45 check.Equals(t, []string{"app-0.0.1"}, tagsForHead) 46 } 47 48 func TestTagsForHeadForAppWithDashes(t *testing.T) { 49 fs := memfs.New() 50 createGitRepo(fs) 51 createVersionTag(fs, "my-app-0.0.1") 52 53 repo := newTestRepo(fs) 54 tagsForHead, err := repo.TagsForHead("my-app") 55 check.Ok(t, err) 56 check.Equals(t, 1, len(tagsForHead)) 57 check.Equals(t, []string{"my-app-0.0.1"}, tagsForHead) 58 } 59 60 func TestTagsForHeadForAppWithOverlappingNamePrefix(t *testing.T) { 61 fs := memfs.New() 62 createGitRepo(fs) 63 createVersionTag(fs, "my-app-0.0.1") 64 createVersionTag(fs, "app-0.0.2") 65 66 repo := newTestRepo(fs) 67 tagsForHead, err := repo.TagsForHead("my-app") 68 check.Ok(t, err) 69 check.Equals(t, 1, len(tagsForHead)) 70 check.Equals(t, []string{"my-app-0.0.1"}, tagsForHead) 71 } 72 73 func TestTagsForHeadForAppWithOverlappingNameSuffix(t *testing.T) { 74 fs := memfs.New() 75 createGitRepo(fs) 76 createVersionTag(fs, "app-two-0.0.1") 77 createVersionTag(fs, "app-0.0.2") 78 79 repo := newTestRepo(fs) 80 tagsForHead, err := repo.TagsForHead("app-two") 81 check.Ok(t, err) 82 check.Equals(t, 1, len(tagsForHead)) 83 check.Equals(t, []string{"app-two-0.0.1"}, tagsForHead) 84 } 85 86 func TestTagsForHeadForAppWithOverlappingNameNumberSuffix(t *testing.T) { 87 fs := memfs.New() 88 createGitRepo(fs) 89 createVersionTag(fs, "app-2-0.0.1") 90 createVersionTag(fs, "app-0.0.2") 91 92 repo := newTestRepo(fs) 93 tagsForHead, err := repo.TagsForHead("app-2") 94 check.Ok(t, err) 95 check.Equals(t, 1, len(tagsForHead)) 96 check.Equals(t, []string{"app-2-0.0.1"}, tagsForHead) 97 } 98 99 func TestTagsForHeadForAppWithOverlappingName(t *testing.T) { 100 fs := memfs.New() 101 createGitRepo(fs) 102 createVersionTag(fs, "my-app-0.0.2") 103 createVersionTag(fs, "app-0.0.1") 104 105 repo := newTestRepo(fs) 106 tagsForHead, err := repo.TagsForHead("app") 107 check.Ok(t, err) 108 check.Equals(t, 1, len(tagsForHead)) 109 check.Equals(t, []string{"app-0.0.1"}, tagsForHead) 110 } 111 112 func TestTagsForHead_emptyStage(t *testing.T) { 113 fs := memfs.New() 114 createGitRepo(fs) 115 createVersionTag(fs, "app-0.0.1") 116 117 repo := newTestRepo(fs) 118 tagsForHead, err := repo.TagsForHead("app", "") 119 check.Ok(t, err) 120 check.Equals(t, 1, len(tagsForHead)) 121 check.Equals(t, []string{"app-0.0.1"}, tagsForHead) 122 } 123 124 func TestTagsForHeadForMultipleTags(t *testing.T) { 125 fs := memfs.New() 126 createGitRepo(fs) 127 createVersionTag(fs, "app-0.0.2") 128 createVersionTag(fs, "app-0.0.1") 129 130 repo := newTestRepo(fs) 131 tagsForHead, err := repo.TagsForHead("app") 132 check.Ok(t, err) 133 check.Equals(t, 2, len(tagsForHead)) 134 check.Equals(t, []string{"app-0.0.1", "app-0.0.2"}, tagsForHead) 135 } 136 137 func TestTagsForHeadAreSorted(t *testing.T) { 138 fs := memfs.New() 139 createGitRepo(fs) 140 var expectedTags []string 141 for i := 0; i < 10; i++ { 142 is := strconv.Itoa(i) 143 tag := "app-0.0." + is 144 createVersionTag(fs, tag) 145 expectedTags = append(expectedTags, tag) 146 } 147 148 repo := newTestRepo(fs) 149 tagsForHead, err := repo.TagsForHead("app") 150 check.Ok(t, err) 151 check.Equals(t, 10, len(tagsForHead)) 152 check.Equals(t, expectedTags, tagsForHead) 153 } 154 155 func TestTagsForHeadForMultipleCommits(t *testing.T) { 156 fs := memfs.New() 157 createGitRepo(fs) 158 createVersionTag(fs, "app-0.0.1") 159 createCommit(fs, "New Version", "Hello world 2") 160 createVersionTag(fs, "app-0.0.2") 161 162 repo := newTestRepo(fs) 163 tagsForHead, err := repo.TagsForHead("app") 164 check.Ok(t, err) 165 check.Equals(t, 1, len(tagsForHead)) 166 167 check.Equals(t, []string{"app-0.0.2"}, tagsForHead) 168 } 169 170 func TestTagsForHeadWhenNone(t *testing.T) { 171 fs := memfs.New() 172 createGitRepo(fs) 173 174 repo := newTestRepo(fs) 175 tagsForHead, err := repo.TagsForHead("app") 176 check.Ok(t, err) 177 check.Equals(t, 0, len(tagsForHead)) 178 } 179 180 func TestTagsForHeadWhenPartialModuleNameMatch(t *testing.T) { 181 fs := memfs.New() 182 createGitRepo(fs) 183 createVersionTag(fs, "ap-0.0.1") 184 185 repo := newTestRepo(fs) 186 tagsForHead, err := repo.TagsForHead("app") 187 check.Ok(t, err) 188 check.Equals(t, 0, len(tagsForHead)) 189 } 190 191 func TestTagsForHeadWhenIncorrectVersioning(t *testing.T) { 192 fs := memfs.New() 193 createGitRepo(fs) 194 createVersionTag(fs, "app-.0.1") 195 196 repo := newTestRepo(fs) 197 tagsForHead, err := repo.TagsForHead("app") 198 check.Ok(t, err) 199 check.Equals(t, 0, len(tagsForHead)) 200 } 201 202 func TestTagsForHeadWhenDashSeparatedPartialModuleNameMatch(t *testing.T) { 203 fs := memfs.New() 204 createGitRepo(fs) 205 createVersionTag(fs, "another-app-0.0.1") 206 207 repo := newTestRepo(fs) 208 tagsForHead, err := repo.TagsForHead("app") 209 check.Ok(t, err) 210 check.Equals(t, 0, len(tagsForHead)) 211 } 212 213 func TestTagsForHeadWhenOnlyPromoted(t *testing.T) { 214 fs := memfs.New() 215 createGitRepo(fs) 216 createVersionTag(fs, "app-0.0.1+promoted") 217 218 repo := newTestRepo(fs) 219 tagsForHead, err := repo.TagsForHead("app") 220 check.Ok(t, err) 221 check.Equals(t, 0, len(tagsForHead)) 222 } 223 224 func TestTagsForHead_stage(t *testing.T) { 225 fs := memfs.New() 226 createGitRepo(fs) 227 createVersionTag(fs, "app-0.0.1") 228 createVersionTag(fs, "app-0.0.1+promoted") 229 230 repo := newTestRepo(fs) 231 tagsForHead, err := repo.TagsForHead("app", "promoted") 232 check.Ok(t, err) 233 check.Equals(t, 1, len(tagsForHead)) 234 check.Equals(t, []string{"app-0.0.1+promoted"}, tagsForHead) 235 } 236 237 func TestTagsForHeadForMultipleCommits_stage(t *testing.T) { 238 fs := memfs.New() 239 createGitRepo(fs) 240 createVersionTag(fs, "app-0.0.1") 241 createVersionTag(fs, "app-0.0.1+promoted") 242 createCommit(fs, "New Version", "Hello world 2") 243 createVersionTag(fs, "app-0.0.2") 244 createVersionTag(fs, "app-0.0.2+promoted") 245 246 repo := newTestRepo(fs) 247 tagsForHead, err := repo.TagsForHead("app", "promoted") 248 check.Ok(t, err) 249 check.Equals(t, 1, len(tagsForHead)) 250 check.Equals(t, []string{"app-0.0.2+promoted"}, tagsForHead) 251 } 252 253 func TestTagsForHeadAreSorted_stage(t *testing.T) { 254 fs := memfs.New() 255 createGitRepo(fs) 256 var expectedTags []string 257 for i := 0; i < 10; i++ { 258 is := strconv.Itoa(i) 259 tag := "app-0.0." + is + "+promoted" 260 createVersionTag(fs, tag) 261 expectedTags = append(expectedTags, tag) 262 } 263 264 repo := newTestRepo(fs) 265 tagsForHead, err := repo.TagsForHead("app", "promoted") 266 check.Ok(t, err) 267 check.Equals(t, 10, len(tagsForHead)) 268 check.Equals(t, expectedTags, tagsForHead) 269 } 270 271 func TestTagsForHeadWhenNone_stage(t *testing.T) { 272 fs := memfs.New() 273 createGitRepo(fs) 274 275 repo := newTestRepo(fs) 276 tagsForHead, err := repo.TagsForHead("app", "promoted") 277 check.Ok(t, err) 278 check.Equals(t, 0, len(tagsForHead)) 279 } 280 281 func TestTagsForHeadWhenOnlyUnpromoted_stage(t *testing.T) { 282 fs := memfs.New() 283 createGitRepo(fs) 284 createVersionTag(fs, "app-0.0.1") 285 286 repo := newTestRepo(fs) 287 tagsForHead, err := repo.TagsForHead("app", "promoted") 288 check.Ok(t, err) 289 check.Equals(t, 0, len(tagsForHead)) 290 } 291 292 func TestTagsForHeadWhenPartialMatch_stage(t *testing.T) { 293 fs := memfs.New() 294 createGitRepo(fs) 295 createVersionTag(fs, "ap-0.0.1") 296 297 repo := newTestRepo(fs) 298 tagsForHead, err := repo.TagsForHead("app", "promoted") 299 check.Ok(t, err) 300 check.Equals(t, 0, len(tagsForHead)) 301 } 302 303 func TestTagsForHeadWhenIncorrectVersioning_stage(t *testing.T) { 304 fs := memfs.New() 305 createGitRepo(fs) 306 createVersionTag(fs, "app-.0.1") 307 308 repo := newTestRepo(fs) 309 tagsForHead, err := repo.TagsForHead("app", "promoted") 310 check.Ok(t, err) 311 check.Equals(t, 0, len(tagsForHead)) 312 } 313 314 func TestTagsForModule(t *testing.T) { 315 fs := memfs.New() 316 createGitRepo(fs) 317 createVersionTag(fs, "app-0.0.1") 318 319 repo := newTestRepo(fs) 320 tagsForModule, err := repo.TagsForModule("app") 321 check.Ok(t, err) 322 check.Equals(t, 1, len(tagsForModule)) 323 check.Equals(t, []string{"app-0.0.1"}, tagsForModule) 324 } 325 326 func TestAnnotatedTagsForModule(t *testing.T) { 327 fs := memfs.New() 328 createGitRepo(fs) 329 createAnnotatedVersionTag(fs, "app-0.0.1") 330 331 repo := newTestRepo(fs) 332 tagsForModule, err := repo.TagsForModule("app") 333 check.Ok(t, err) 334 check.Equals(t, 1, len(tagsForModule)) 335 check.Equals(t, []string{"app-0.0.1"}, tagsForModule) 336 } 337 338 func TestTagsForModuleWithOverlappingNamePrefix(t *testing.T) { 339 fs := memfs.New() 340 createGitRepo(fs) 341 createVersionTag(fs, "my-app-0.0.1") 342 createVersionTag(fs, "app-0.0.2") 343 344 repo := newTestRepo(fs) 345 tagsForModule, err := repo.TagsForModule("my-app") 346 check.Ok(t, err) 347 check.Equals(t, 1, len(tagsForModule)) 348 check.Equals(t, []string{"my-app-0.0.1"}, tagsForModule) 349 } 350 351 func TestTagsForModuleWithOverlappingNameSuffix(t *testing.T) { 352 fs := memfs.New() 353 createGitRepo(fs) 354 createVersionTag(fs, "app-two-0.0.1") 355 createVersionTag(fs, "app-0.0.2") 356 357 repo := newTestRepo(fs) 358 tagsForModule, err := repo.TagsForModule("app-two") 359 check.Ok(t, err) 360 check.Equals(t, 1, len(tagsForModule)) 361 check.Equals(t, []string{"app-two-0.0.1"}, tagsForModule) 362 } 363 364 func TestTagsForModuleWithOverlappingNameNumberSuffix(t *testing.T) { 365 fs := memfs.New() 366 createGitRepo(fs) 367 createVersionTag(fs, "app-2-0.0.1") 368 createVersionTag(fs, "app-0.0.2") 369 370 repo := newTestRepo(fs) 371 tagsForModule, err := repo.TagsForModule("app-2") 372 check.Ok(t, err) 373 check.Equals(t, 1, len(tagsForModule)) 374 check.Equals(t, []string{"app-2-0.0.1"}, tagsForModule) 375 } 376 377 func TestTagsForModuleWithOverlappingName(t *testing.T) { 378 fs := memfs.New() 379 createGitRepo(fs) 380 createVersionTag(fs, "my-app-0.0.2") 381 createVersionTag(fs, "app-0.0.1") 382 383 repo := newTestRepo(fs) 384 tagsForModule, err := repo.TagsForModule("app") 385 check.Ok(t, err) 386 check.Equals(t, 1, len(tagsForModule)) 387 check.Equals(t, []string{"app-0.0.1"}, tagsForModule) 388 } 389 390 func TestTagsForModule_emptyStage(t *testing.T) { 391 fs := memfs.New() 392 createGitRepo(fs) 393 createVersionTag(fs, "app-0.0.1") 394 395 repo := newTestRepo(fs) 396 tagsForModule, err := repo.TagsForModule("app", "") 397 check.Ok(t, err) 398 check.Equals(t, 1, len(tagsForModule)) 399 check.Equals(t, []string{"app-0.0.1"}, tagsForModule) 400 } 401 402 func TestTagsForModuleForMultipleCommitsAndTags(t *testing.T) { 403 fs := memfs.New() 404 createGitRepo(fs) 405 createVersionTag(fs, "app-0.0.1") 406 createCommit(fs, "New Version", "Hello world 2") 407 createVersionTag(fs, "app-0.0.2") 408 409 repo := newTestRepo(fs) 410 tagsForModule, err := repo.TagsForModule("app") 411 check.Ok(t, err) 412 check.Equals(t, 2, len(tagsForModule)) 413 check.Equals(t, []string{"app-0.0.1", "app-0.0.2"}, tagsForModule) 414 } 415 416 func TestTagsForModuleIsSorted_Patch(t *testing.T) { 417 fs := memfs.New() 418 createGitRepo(fs) 419 var expectedTags []string 420 for i := 0; i < 101; i++ { 421 is := strconv.Itoa(i) 422 createCommit(fs, "New Version "+is, "Hello world "+is) 423 tag := "app-0.0." + is 424 createVersionTag(fs, tag) 425 expectedTags = append(expectedTags, tag) 426 } 427 428 repo := newTestRepo(fs) 429 tagsForModule, err := repo.TagsForModule("app") 430 check.Ok(t, err) 431 check.Equals(t, 101, len(tagsForModule)) 432 check.Equals(t, expectedTags, tagsForModule) 433 } 434 435 func TestTagsForModuleIsSorted_Minor(t *testing.T) { 436 fs := memfs.New() 437 createGitRepo(fs) 438 var expectedTags []string 439 for i := 0; i < 101; i++ { 440 is := strconv.Itoa(i) 441 createCommit(fs, "New Version "+is, "Hello world "+is) 442 tag := "app-0." + is + ".0" 443 createVersionTag(fs, tag) 444 expectedTags = append(expectedTags, tag) 445 } 446 447 repo := newTestRepo(fs) 448 tagsForModule, err := repo.TagsForModule("app") 449 check.Ok(t, err) 450 check.Equals(t, 101, len(tagsForModule)) 451 check.Equals(t, expectedTags, tagsForModule) 452 } 453 454 func TestTagsForModuleIsSorted_Major(t *testing.T) { 455 fs := memfs.New() 456 createGitRepo(fs) 457 var expectedTags []string 458 for i := 0; i < 101; i++ { 459 is := strconv.Itoa(i) 460 createCommit(fs, "New Version "+is, "Hello world "+is) 461 tag := "app-" + is + ".0.0" 462 createVersionTag(fs, tag) 463 expectedTags = append(expectedTags, tag) 464 } 465 466 repo := newTestRepo(fs) 467 tagsForModule, err := repo.TagsForModule("app") 468 check.Ok(t, err) 469 check.Equals(t, 101, len(tagsForModule)) 470 check.Equals(t, expectedTags, tagsForModule) 471 } 472 473 func TestTagsForModuleWhenNone(t *testing.T) { 474 fs := memfs.New() 475 createGitRepo(fs) 476 477 repo := newTestRepo(fs) 478 tagsForModule, err := repo.TagsForModule("app") 479 check.Ok(t, err) 480 check.Equals(t, 0, len(tagsForModule)) 481 } 482 483 func TestTagsForModuleWhenOnlyPromoted(t *testing.T) { 484 fs := memfs.New() 485 createGitRepo(fs) 486 createVersionTag(fs, "app-0.0.1+promoted") 487 488 repo := newTestRepo(fs) 489 tagsForModule, err := repo.TagsForModule("app") 490 check.Ok(t, err) 491 check.Equals(t, 0, len(tagsForModule)) 492 } 493 494 func TestTagsForModuleWhenPartialModuleNameMatch(t *testing.T) { 495 fs := memfs.New() 496 createGitRepo(fs) 497 createVersionTag(fs, "ap-0.0.1") 498 499 repo := newTestRepo(fs) 500 tagsForModule, err := repo.TagsForModule("app") 501 check.Ok(t, err) 502 check.Equals(t, 0, len(tagsForModule)) 503 } 504 505 func TestTagsForModuleWhenDashSeparatedPartialModuleNameMatch(t *testing.T) { 506 fs := memfs.New() 507 createGitRepo(fs) 508 createVersionTag(fs, "another-app-0.0.1") 509 510 repo := newTestRepo(fs) 511 tagsForModule, err := repo.TagsForModule("app") 512 check.Ok(t, err) 513 check.Equals(t, 0, len(tagsForModule)) 514 } 515 516 func TestTagsForModuleWhenIncorrectVersioning(t *testing.T) { 517 fs := memfs.New() 518 createGitRepo(fs) 519 createVersionTag(fs, "app-.0.1") 520 521 repo := newTestRepo(fs) 522 tagsForModule, err := repo.TagsForModule("app") 523 check.Ok(t, err) 524 check.Equals(t, 0, len(tagsForModule)) 525 } 526 527 func TestTagsForModule_stage(t *testing.T) { 528 fs := memfs.New() 529 createGitRepo(fs) 530 createVersionTag(fs, "app-0.0.1") 531 createVersionTag(fs, "app-0.0.1+promoted") 532 533 repo := newTestRepo(fs) 534 tagsForModule, err := repo.TagsForModule("app", "promoted") 535 check.Ok(t, err) 536 check.Equals(t, 1, len(tagsForModule)) 537 check.Equals(t, []string{"app-0.0.1+promoted"}, tagsForModule) 538 } 539 540 func TestTagsForModuleForMultipleCommitsAndTags_stage(t *testing.T) { 541 fs := memfs.New() 542 createGitRepo(fs) 543 createVersionTag(fs, "app-0.0.1") 544 createVersionTag(fs, "app-0.0.1+promoted") 545 createCommit(fs, "New Version", "Hello world 2") 546 createVersionTag(fs, "app-0.0.2") 547 createVersionTag(fs, "app-0.0.2+promoted") 548 549 repo := newTestRepo(fs) 550 tagsForModule, err := repo.TagsForModule("app", "promoted") 551 check.Ok(t, err) 552 check.Equals(t, 2, len(tagsForModule)) 553 check.Equals(t, []string{"app-0.0.1+promoted", "app-0.0.2+promoted"}, tagsForModule) 554 } 555 556 func TestTagsForModuleIsSorted_stage(t *testing.T) { 557 fs := memfs.New() 558 createGitRepo(fs) 559 var expectedTags []string 560 for i := 0; i < 10; i++ { 561 is := strconv.Itoa(i) 562 createCommit(fs, "New Version "+is, "Hello world "+is) 563 tag := "app-0.0." + is + "+promoted" 564 createVersionTag(fs, tag) 565 expectedTags = append(expectedTags, tag) 566 } 567 568 repo := newTestRepo(fs) 569 tagsForModule, err := repo.TagsForModule("app", "promoted") 570 check.Ok(t, err) 571 check.Equals(t, 10, len(tagsForModule)) 572 check.Equals(t, expectedTags, tagsForModule) 573 } 574 575 func TestTagsForModuleWhenNone_stage(t *testing.T) { 576 fs := memfs.New() 577 createGitRepo(fs) 578 579 repo := newTestRepo(fs) 580 tagsForModule, err := repo.TagsForModule("app", "promoted") 581 check.Ok(t, err) 582 check.Equals(t, 0, len(tagsForModule)) 583 } 584 585 func TestTagsForModuleOnlyUnpromoted_stage(t *testing.T) { 586 fs := memfs.New() 587 createGitRepo(fs) 588 createVersionTag(fs, "app-0.0.1") 589 590 repo := newTestRepo(fs) 591 tagsForModule, err := repo.TagsForModule("app", "promoted") 592 check.Ok(t, err) 593 check.Equals(t, 0, len(tagsForModule)) 594 } 595 596 func TestTagsForModulePartialModuleNameMatch_stage(t *testing.T) { 597 fs := memfs.New() 598 createGitRepo(fs) 599 createVersionTag(fs, "ap-0.0.1+promoted") 600 601 repo := newTestRepo(fs) 602 tagsForModule, err := repo.TagsForModule("app", "promoted") 603 check.Ok(t, err) 604 check.Equals(t, 0, len(tagsForModule)) 605 } 606 607 func TestTagsForModuleIncorrectVersioning_stage(t *testing.T) { 608 fs := memfs.New() 609 createGitRepo(fs) 610 createVersionTag(fs, "app-.0.1+promoted") 611 612 repo := newTestRepo(fs) 613 tagsForModule, err := repo.TagsForModule("app", "promoted") 614 check.Ok(t, err) 615 check.Equals(t, 0, len(tagsForModule)) 616 }