SetupDefaultRepoAndWorkspace handles setting up a default repo to clone, and a workspace to clone into. returns a cleanup function to remove the git repo and workspace.
(t *testing.T, dataset string)
| 435 | // SetupDefaultRepoAndWorkspace handles setting up a default repo to clone, and a workspace to clone into. |
| 436 | // returns a cleanup function to remove the git repo and workspace. |
| 437 | func SetupDefaultRepoAndWorkspace(t *testing.T, dataset string) (*TestGitRepo, *TestWorkspace, func()) { |
| 438 | // Capture the current working directory so we can set it back to the |
| 439 | // original path after test has completed. |
| 440 | cwd, err := os.Getwd() |
| 441 | if err != nil { |
| 442 | assert.NoError(t, err) |
| 443 | } |
| 444 | |
| 445 | // setup the repo to clone from |
| 446 | g := &TestGitRepo{} |
| 447 | err = g.SetupTestGitRepo(dataset) |
| 448 | assert.NoError(t, err) |
| 449 | |
| 450 | // setup the directory to clone to |
| 451 | w := &TestWorkspace{ |
| 452 | PackageDir: g.RepoName, |
| 453 | } |
| 454 | err = w.SetupTestWorkspace() |
| 455 | assert.NoError(t, err) |
| 456 | err = os.Chdir(w.WorkspaceDirectory) |
| 457 | assert.NoError(t, err) |
| 458 | |
| 459 | gr := gitutil.NewLocalGitRunner("./") |
| 460 | if !assert.NoError(t, gr.Run("init")) { |
| 461 | assert.FailNowf(t, "%s %s", gr.Stdout.String(), gr.Stderr.String()) |
| 462 | } |
| 463 | |
| 464 | // make sure that both master and main branches are created in the test repo |
| 465 | // do not error if they already exist or |
| 466 | _ = g.CheckoutBranch("master", true) |
| 467 | _ = g.CheckoutBranch("main", true) |
| 468 | |
| 469 | // checkout to master branch |
| 470 | err = g.CheckoutBranch("master", false) |
| 471 | assert.NoError(t, err) |
| 472 | |
| 473 | return g, w, func() { |
| 474 | // ignore cleanup failures |
| 475 | _ = g.RemoveAll() |
| 476 | _ = w.RemoveAll() |
| 477 | _ = os.Chdir(cwd) |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | func checkoutBranch(repo string, branch string, create bool) error { |
| 482 | var args []string |