MCPcopy
hub / github.com/kptdev/kpt / SetupDefaultRepoAndWorkspace

Function SetupDefaultRepoAndWorkspace

internal/testutil/testutil.go:437–479  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
437func 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
481func checkoutBranch(repo string, branch string, create bool) error {
482 var args []string

Callers 15

TestCmd_executeFunction · 0.92
TestCmdExecuteFunction · 0.92
TestCmd_executeFunction · 0.92
TestCmd_failUnCommittedFunction · 0.92
TestGitUtil_DefaultRefFunction · 0.92
TestCommand_RunFunction · 0.92
TestCommand_Run_subdirFunction · 0.92
TestCommand_Run_branchFunction · 0.92
TestCommand_Run_tagFunction · 0.92

Calls 8

SetupTestGitRepoMethod · 0.95
SetupTestWorkspaceMethod · 0.95
CheckoutBranchMethod · 0.95
RemoveAllMethod · 0.95
RemoveAllMethod · 0.95
NewLocalGitRunnerFunction · 0.92
RunMethod · 0.45
StringMethod · 0.45

Tested by 15

TestCmd_executeFunction · 0.74
TestCmdExecuteFunction · 0.74
TestCmd_executeFunction · 0.74
TestCmd_failUnCommittedFunction · 0.74
TestGitUtil_DefaultRefFunction · 0.74
TestCommand_RunFunction · 0.74
TestCommand_Run_subdirFunction · 0.74
TestCommand_Run_branchFunction · 0.74
TestCommand_Run_tagFunction · 0.74