|
1 | 1 | package vcs |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "io/ioutil" |
5 | 6 | "path/filepath" |
6 | 7 | "time" |
@@ -356,3 +357,128 @@ func TestGitInit(t *testing.T) { |
356 | 357 | t.Error(err) |
357 | 358 | } |
358 | 359 | } |
| 360 | + |
| 361 | +func TestGitSubmoduleHandling(t *testing.T) { |
| 362 | + tempDir, err := ioutil.TempDir("", "go-vcs-git-submodule-tests") |
| 363 | + if err != nil { |
| 364 | + t.Fatal(err) |
| 365 | + } |
| 366 | + defer func() { |
| 367 | + err = os.RemoveAll(tempDir) |
| 368 | + if err != nil { |
| 369 | + t.Error(err) |
| 370 | + } |
| 371 | + }() |
| 372 | + |
| 373 | + dumplocal := func(err error) string { |
| 374 | + if terr, ok := err.(*LocalError); ok { |
| 375 | + return fmt.Sprintf("msg: %s\norig: %s\nout: %s", terr.Error(), terr.Original(), terr.Out()) |
| 376 | + } |
| 377 | + return err.Error() |
| 378 | + } |
| 379 | + |
| 380 | + subdirExists := func(dir ...string) bool { |
| 381 | + _, err := os.Stat(filepath.Join(append([]string{tempDir}, dir...)...)) |
| 382 | + return err == nil |
| 383 | + } |
| 384 | + |
| 385 | + // Initial clone should get version with two submodules, each of which have |
| 386 | + // their own submodule |
| 387 | + repo, err := NewGitRepo("https://github.com/sdboyer/subm", tempDir) |
| 388 | + if err != nil { |
| 389 | + t.Fatal(dumplocal(err)) |
| 390 | + } |
| 391 | + err = repo.Get() |
| 392 | + if err != nil { |
| 393 | + t.Fatalf("unable to clone Git repo. Err was %s", dumplocal(err)) |
| 394 | + } |
| 395 | + |
| 396 | + // Verify we are on the right version. |
| 397 | + v, err := repo.Version() |
| 398 | + if v != "18e3a5f6fc7f6d577e732e7a5ab2caf990efbf8f" { |
| 399 | + t.Fatalf("did not start from expected rev, tests could fail - bailing out (got %s)", v) |
| 400 | + } |
| 401 | + if err != nil { |
| 402 | + t.Fatal(dumplocal(err)) |
| 403 | + } |
| 404 | + |
| 405 | + if !subdirExists("subm1", ".git") { |
| 406 | + t.Fatal("subm1 submodule does not exist on initial clone/checkout") |
| 407 | + } |
| 408 | + if !subdirExists("subm1", "dep-test", ".git") { |
| 409 | + t.Fatal("dep-test submodule nested under subm1 does not exist on initial clone/checkout") |
| 410 | + } |
| 411 | + |
| 412 | + if !subdirExists("subm-again", ".git") { |
| 413 | + t.Fatal("subm-again submodule does not exist on initial clone/checkout") |
| 414 | + } |
| 415 | + if !subdirExists("subm-again", "dep-test", ".git") { |
| 416 | + t.Fatal("dep-test submodule nested under subm-again does not exist on initial clone/checkout") |
| 417 | + } |
| 418 | + |
| 419 | + // Now switch to version with no submodules, make sure they all go away |
| 420 | + err = repo.UpdateVersion("e677f82015f72ac1c8fafa66b5463163b3597af2") |
| 421 | + if err != nil { |
| 422 | + t.Fatalf("checking out needed version failed with err: %s", dumplocal(err)) |
| 423 | + } |
| 424 | + |
| 425 | + if subdirExists("subm1") { |
| 426 | + t.Fatal("checking out version without submodule did not clean up immediate submodules") |
| 427 | + } |
| 428 | + if subdirExists("subm1", "dep-test") { |
| 429 | + t.Fatal("checking out version without submodule did not clean up nested submodules") |
| 430 | + } |
| 431 | + if subdirExists("subm-again") { |
| 432 | + t.Fatal("checking out version without submodule did not clean up immediate submodules") |
| 433 | + } |
| 434 | + if subdirExists("subm-again", "dep-test") { |
| 435 | + t.Fatal("checking out version without submodule did not clean up nested submodules") |
| 436 | + } |
| 437 | + |
| 438 | + err = repo.UpdateVersion("aaf7aa1bc4c3c682cc530eca8f80417088ee8540") |
| 439 | + if err != nil { |
| 440 | + t.Fatalf("checking out needed version failed with err: %s", dumplocal(err)) |
| 441 | + } |
| 442 | + |
| 443 | + if !subdirExists("subm1", ".git") { |
| 444 | + t.Fatal("checking out version with immediate submodule did not set up git subrepo") |
| 445 | + } |
| 446 | + |
| 447 | + err = repo.UpdateVersion("6cc4669af468f3b4f16e7e96275ad01ade5b522f") |
| 448 | + if err != nil { |
| 449 | + t.Fatalf("checking out needed version failed with err: %s", dumplocal(err)) |
| 450 | + } |
| 451 | + |
| 452 | + if !subdirExists("subm1", "dep-test", ".git") { |
| 453 | + t.Fatal("checking out version with nested submodule did not set up nested git subrepo") |
| 454 | + } |
| 455 | + |
| 456 | + err = repo.UpdateVersion("aaf7aa1bc4c3c682cc530eca8f80417088ee8540") |
| 457 | + if err != nil { |
| 458 | + t.Fatalf("checking out needed version failed with err: %s", dumplocal(err)) |
| 459 | + } |
| 460 | + |
| 461 | + if subdirExists("subm1", "dep-test") { |
| 462 | + t.Fatal("rolling back to version without nested submodule did not clean up the nested submodule") |
| 463 | + } |
| 464 | + |
| 465 | + err = repo.UpdateVersion("18e3a5f6fc7f6d577e732e7a5ab2caf990efbf8f") |
| 466 | + if err != nil { |
| 467 | + t.Fatalf("checking out needed version failed with err: %s", dumplocal(err)) |
| 468 | + } |
| 469 | + |
| 470 | + if !subdirExists("subm1", ".git") { |
| 471 | + t.Fatal("subm1 submodule does not exist after switch from other commit") |
| 472 | + } |
| 473 | + if !subdirExists("subm1", "dep-test", ".git") { |
| 474 | + t.Fatal("dep-test submodule nested under subm1 does not exist after switch from other commit") |
| 475 | + } |
| 476 | + |
| 477 | + if !subdirExists("subm-again", ".git") { |
| 478 | + t.Fatal("subm-again submodule does not exist after switch from other commit") |
| 479 | + } |
| 480 | + if !subdirExists("subm-again", "dep-test", ".git") { |
| 481 | + t.Fatal("dep-test submodule nested under subm-again does not exist after switch from other commit") |
| 482 | + } |
| 483 | + |
| 484 | +} |
0 commit comments