Skip to content

Commit

Permalink
git: duplicate all tests using v2 client
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
  • Loading branch information
stevekuznetsov committed Nov 22, 2019
1 parent dff11de commit 1aac531
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 35 deletions.
30 changes: 27 additions & 3 deletions prow/external-plugins/cherrypicker/server_test.go
Expand Up @@ -164,7 +164,15 @@ index 1ea52dc..5bd70a9 100644
var body = "This PR updates the magic number.\n\n```release-note\nUpdate the magic number from 42 to 49\n```"

func TestCherryPickIC(t *testing.T) {
lg, c, err := localgit.New()
testCherryPickIC(localgit.New, t)
}

func TestCherryPickICV2(t *testing.T) {
testCherryPickIC(localgit.NewV2, t)
}

func testCherryPickIC(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
Expand Down Expand Up @@ -254,7 +262,15 @@ func TestCherryPickIC(t *testing.T) {
}

func TestCherryPickPR(t *testing.T) {
lg, c, err := localgit.New()
testCherryPickPR(localgit.New, t)
}

func TestCherryPickPRV2(t *testing.T) {
testCherryPickPR(localgit.NewV2, t)
}

func testCherryPickPR(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
Expand Down Expand Up @@ -417,7 +433,15 @@ func TestCherryPickPR(t *testing.T) {
}

func TestCherryPickPRWithLabels(t *testing.T) {
lg, c, err := localgit.New()
testCherryPickPRWithLabels(localgit.New, t)
}

func TestCherryPickPRWithLabelsV2(t *testing.T) {
testCherryPickPRWithLabels(localgit.NewV2, t)
}

func testCherryPickPRWithLabels(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion prow/flagutil/git.go
Expand Up @@ -86,5 +86,5 @@ func (o *GitOptions) GitClient(userClient github.UserClient, token func() []byte
}
return user.Login, nil
}
return git.NewClientFactory(o.host, o.useSSH, username, token, gitUser, censor), nil
return git.NewClientFactory(o.host, o.useSSH, username, token, gitUser, censor)
}
46 changes: 36 additions & 10 deletions prow/git/git_test.go
Expand Up @@ -31,7 +31,15 @@ import (
)

func TestClone(t *testing.T) {
lg, c, err := localgit.New()
testClone(localgit.New, t)
}

func TestCloneV2(t *testing.T) {
testClone(localgit.NewV2, t)
}

func testClone(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
if err != nil {
t.Fatalf("Making local git repo: %v", err)
}
Expand Down Expand Up @@ -98,10 +106,15 @@ func TestClone(t *testing.T) {
}

func TestCheckoutPR(t *testing.T) {
lg, c, err := localgit.New()
if err != nil {
t.Fatalf("Making local git repo: %v", err)
}
testCheckoutPR(localgit.New, t)
}

func TestCheckoutPRV2(t *testing.T) {
testCheckoutPR(localgit.NewV2, t)
}

func testCheckoutPR(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
defer func() {
if err := lg.Clean(); err != nil {
t.Errorf("Error cleaning LocalGit: %v", err)
Expand Down Expand Up @@ -139,10 +152,15 @@ func TestCheckoutPR(t *testing.T) {
}

func TestMergeCommitsExistBetween(t *testing.T) {
lg, c, err := localgit.New()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
testMergeCommitsExistBetween(localgit.New, t)
}

func TestMergeCommitsExistBetweenV2(t *testing.T) {
testMergeCommitsExistBetween(localgit.NewV2, t)
}

func testMergeCommitsExistBetween(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
defer func() {
if err := lg.Clean(); err != nil {
t.Errorf("Cleaning up localgit: %v", err)
Expand Down Expand Up @@ -259,6 +277,14 @@ func TestMergeCommitsExistBetween(t *testing.T) {
}

func TestMergeAndCheckout(t *testing.T) {
testMergeAndCheckout(localgit.New, t)
}

func TestMergeAndCheckoutV2(t *testing.T) {
testMergeAndCheckout(localgit.NewV2, t)
}

func testMergeAndCheckout(clients localgit.Clients, t *testing.T) {
testCases := []struct {
name string
setBaseSHA bool
Expand Down Expand Up @@ -323,7 +349,7 @@ func TestMergeAndCheckout(t *testing.T) {
tc := tc
t.Parallel()

lg, c, err := localgit.New()
lg, c, err := clients()
if err != nil {
t.Fatalf("Making local git repo: %v", err)
}
Expand Down
24 changes: 24 additions & 0 deletions prow/git/localgit/localgit.go
Expand Up @@ -30,6 +30,8 @@ import (
v2 "k8s.io/test-infra/prow/git/v2"
)

type Clients func() (*LocalGit, v2.ClientFactory, error)

// LocalGit stores the repos in a temp dir. Create with New and delete with
// Clean.
type LocalGit struct {
Expand Down Expand Up @@ -176,3 +178,25 @@ func (lg *LocalGit) Rebase(org, repo, commitlike string) (string, error) {
rdir := filepath.Join(lg.Dir, org, repo)
return runCmdOutput(lg.Git, rdir, "rebase", commitlike)
}

// NewV2 creates a LocalGit and a v2 client factory pointing at it.
func NewV2() (*LocalGit, v2.ClientFactory, error) {
g, err := exec.LookPath("git")
if err != nil {
return nil, nil, err
}
t, err := ioutil.TempDir("", "localgit")
if err != nil {
return nil, nil, err
}
c, err := v2.NewLocalClientFactory(t,
func() (name, email string, err error) { return "robot", "robot@beep.boop", nil },
func(content []byte) []byte { return content })
if err != nil {
return nil, nil, err
}
return &LocalGit{
Dir: t,
Git: g,
}, c, nil
}
20 changes: 18 additions & 2 deletions prow/plugins/buildifier/buildifier_test.go
Expand Up @@ -91,7 +91,15 @@ var e = &github.GenericCommentEvent{
}

func TestBuildify(t *testing.T) {
lg, c, err := localgit.New()
testBuildify(localgit.New, t)
}

func TestBuildifyV2(t *testing.T) {
testBuildify(localgit.NewV2, t)
}

func testBuildify(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
Expand Down Expand Up @@ -146,7 +154,15 @@ func TestBuildify(t *testing.T) {
}

func TestModifiedBazelFiles(t *testing.T) {
lg, c, err := localgit.New()
testModifiedBazelFiles(localgit.New, t)
}

func TestModifiedBazelFilesV2(t *testing.T) {
testModifiedBazelFiles(localgit.NewV2, t)
}

func testModifiedBazelFiles(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
Expand Down
40 changes: 36 additions & 4 deletions prow/plugins/golint/golint_test.go
Expand Up @@ -137,7 +137,15 @@ func TestMinConfidence(t *testing.T) {
}

func TestLint(t *testing.T) {
lg, c, err := localgit.New()
testLint(localgit.New, t)
}

func TestLintV2(t *testing.T) {
testLint(localgit.NewV2, t)
}

func testLint(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
Expand Down Expand Up @@ -218,7 +226,14 @@ func TestLint(t *testing.T) {
}

func TestLintCodeSuggestion(t *testing.T) {
testLintCodeSuggestion(localgit.New, t)
}

func TestLintCodeSuggestionV2(t *testing.T) {
testLintCodeSuggestion(localgit.NewV2, t)
}

func testLintCodeSuggestion(clients localgit.Clients, t *testing.T) {
var testcases = []struct {
name string
codeChange string
Expand Down Expand Up @@ -371,7 +386,7 @@ func TestLintCodeSuggestion(t *testing.T) {
},
}

lg, c, err := localgit.New()
lg, c, err := clients()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
Expand Down Expand Up @@ -426,6 +441,15 @@ func TestLintCodeSuggestion(t *testing.T) {
}

func TestLintError(t *testing.T) {
testLintError(localgit.New, t)
}

func TestLintErrorV2(t *testing.T) {
testLintError(localgit.NewV2, t)
}

func testLintError(clients localgit.Clients, t *testing.T) {

var testcases = []struct {
name string
codeChange string
Expand Down Expand Up @@ -515,7 +539,7 @@ func TestLintError(t *testing.T) {
},
}

lg, c, err := localgit.New()
lg, c, err := clients()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
Expand Down Expand Up @@ -643,7 +667,15 @@ func TestAddedLines(t *testing.T) {
}

func TestModifiedGoFiles(t *testing.T) {
lg, c, err := localgit.New()
testModifiedGoFiles(localgit.New, t)
}

func TestModifiedGoFilesV2(t *testing.T) {
testModifiedGoFiles(localgit.NewV2, t)
}

func testModifiedGoFiles(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
Expand Down
10 changes: 9 additions & 1 deletion prow/plugins/mergecommitblocker/mergecommitblocker_test.go
Expand Up @@ -83,7 +83,15 @@ func (f *fakeGHClient) ListIssueComments(org, repo string, number int) ([]github
}

func TestHandle(t *testing.T) {
lg, c, err := localgit.New()
testHandle(localgit.New, t)
}

func TestHandleV2(t *testing.T) {
testHandle(localgit.NewV2, t)
}

func testHandle(clients localgit.Clients, t *testing.T) {
lg, c, err := clients()
if err != nil {
t.Fatalf("Making localgit: %v", err)
}
Expand Down
14 changes: 11 additions & 3 deletions prow/plugins/updateconfig/updateconfig_test.go
Expand Up @@ -93,8 +93,8 @@ var remoteFiles = map[string]map[string]string{
},
}

func setupLocalGitRepo(t *testing.T, org, repo string) git.ClientFactory {
lg, c, err := localgit.New()
func setupLocalGitRepo(clients localgit.Clients, t *testing.T, org, repo string) git.ClientFactory {
lg, c, err := clients()
if err != nil {
t.Fatalf("Making local git repo: %v", err)
}
Expand Down Expand Up @@ -129,6 +129,14 @@ func setupLocalGitRepo(t *testing.T, org, repo string) git.ClientFactory {
}

func TestUpdateConfig(t *testing.T) {
testUpdateConfig(localgit.New, t)
}

func TestUpdateConfigV2(t *testing.T) {
testUpdateConfig(localgit.NewV2, t)
}

func testUpdateConfig(clients localgit.Clients, t *testing.T) {
basicPR := github.PullRequest{
Number: 1,
Base: github.PullRequestBranch{
Expand Down Expand Up @@ -1132,7 +1140,7 @@ func TestUpdateConfig(t *testing.T) {

org := event.PullRequest.Base.Repo.Owner.Login
repo := event.PullRequest.Base.Repo.Name
c := setupLocalGitRepo(t, org, repo)
c := setupLocalGitRepo(clients, t, org, repo)

if err := handle(fgc, c, fkc.CoreV1(), nil, defaultNamespace, log, event, *m, nil); err != nil {
t.Errorf("%s: unexpected error handling: %s", tc.name, err)
Expand Down

0 comments on commit 1aac531

Please sign in to comment.