Exemplo n.º 1
0
func TestForEachPRDo(t *testing.T) {
	prlinks := github.PullRequestLinks{}
	user := github.User{Login: stringp("bob")}
	tests := []struct {
		Issues   [][]github.Issue
		PRs      map[int]github.PullRequest
		Pages    []int
		ValidPRs int
	}{
		{
			Issues: [][]github.Issue{
				{
					{
						PullRequestLinks: &prlinks,
						Number:           intp(1),
						User:             &user,
					},
				},
			},
			PRs: map[int]github.PullRequest{
				1: PR(1, false),
			},
			Pages:    []int{0},
			ValidPRs: 1,
		},
		{
			Issues: [][]github.Issue{
				{
					{
						Number: intp(1),
						User:   &user,
					},
				},
				{
					{
						PullRequestLinks: &prlinks,
						Number:           intp(2),
						User:             &user,
					},
				},
				{
					{
						PullRequestLinks: &prlinks,
						Number:           intp(3),
						User:             &user,
					},
				},
				{
					{
						PullRequestLinks: &prlinks,
						Number:           intp(4),
						User:             &user,
					},
				},
			},
			PRs: map[int]github.PullRequest{
				2: PR(2, false),
				3: PR(3, true),
				4: PR(4, false),
			},
			Pages:    []int{4, 4, 4, 0},
			ValidPRs: 2,
		},
		{
			Issues: [][]github.Issue{
				{
					{
						PullRequestLinks: &prlinks,
						Number:           intp(1),
						User:             &user,
					},
				},
				{
					{
						PullRequestLinks: &prlinks,
						Number:           intp(2),
						User:             &user,
					},
				},
				{
					{
						PullRequestLinks: &prlinks,
						Number:           intp(3),
						User:             &user,
					},
					{
						PullRequestLinks: &prlinks,
						Number:           intp(4),
						User:             &user,
					},
					{
						PullRequestLinks: &prlinks,
						Number:           intp(5),
						User:             &user,
					},
				},
			},
			PRs: map[int]github.PullRequest{
				1: PR(1, false),
				2: PR(2, false),
				3: PR(3, false),
				4: PR(4, true),
				5: PR(5, false),
			},
			Pages:    []int{3, 3, 0},
			ValidPRs: 4,
		},
	}

	for _, test := range tests {
		client, server, mux := github_test.InitTest()
		config := &GithubConfig{
			client:      client,
			Org:         "foo",
			Project:     "bar",
			MaxPRNumber: 32768,
		}
		mux.HandleFunc("/repos/foo/bar/pulls/", func(w http.ResponseWriter, r *http.Request) {
			if r.Method != "GET" {
				t.Errorf("Unexpected method: %s", r.Method)
			}
			prNumS := strings.TrimPrefix(r.URL.Path, "/repos/foo/bar/pulls/")
			prNum, _ := strconv.Atoi(prNumS)
			data, err := json.Marshal(test.PRs[prNum])
			if err != nil {
				t.Errorf("Unexpected error: %v", err)
			}

			w.Write(data)
		})
		count := 0
		mux.HandleFunc("/repos/foo/bar/issues", func(w http.ResponseWriter, r *http.Request) {
			if r.Method != "GET" {
				t.Errorf("Unexpected method: %s", r.Method)
			}
			// this means page 0, return page 1
			page := r.URL.Query().Get("page")
			if page == "" {
				t.Errorf("Should not get page 0, start with page 1")
			}
			if page != strconv.Itoa(count+1) {
				t.Errorf("Unexpected page: %s", r.URL.Query().Get("page"))
			}
			if r.URL.Query().Get("sort") != "created" {
				t.Errorf("Unexpected sort: %s", r.URL.Query().Get("sort"))
			}
			if r.URL.Query().Get("per_page") != "20" {
				t.Errorf("Unexpected per_page: %s", r.URL.Query().Get("per_page"))
			}
			w.Header().Add("Link",
				fmt.Sprintf("<https://api.github.com/?page=%d>; rel=\"last\"", test.Pages[count]))
			w.WriteHeader(http.StatusOK)
			data, err := json.Marshal(test.Issues[count])
			if err != nil {
				t.Errorf("Unexpected error: %v", err)
			}

			w.Write(data)
			count++
		})
		prs := []*github.PullRequest{}
		handle := func(pr *github.PullRequest, issue *github.Issue) error {
			prs = append(prs, pr)
			return nil
		}
		err := config.ForEachPRDo([]string{}, handle)
		if err != nil {
			t.Errorf("unexpected error: %v", err)
		}
		if len(prs) != test.ValidPRs {
			t.Errorf("unexpected output %d vs %d", len(prs), test.ValidPRs)
		}

		if count != len(test.Issues) {
			t.Errorf("unexpected number of fetches: %d", count)
		}
		server.Close()
	}
}
Exemplo n.º 2
0
func TestGetLastModified(t *testing.T) {
	tests := []struct {
		commits      []github.RepositoryCommit
		expectedTime *time.Time
	}{
		{
			commits: []github.RepositoryCommit{
				{
					Commit: &github.Commit{
						Committer: &github.CommitAuthor{
							Date: timePtr(time.Unix(10, 0)),
						},
					},
				},
			},
			expectedTime: timePtr(time.Unix(10, 0)),
		},
		{
			commits: []github.RepositoryCommit{
				{
					Commit: &github.Commit{
						Committer: &github.CommitAuthor{
							Date: timePtr(time.Unix(10, 0)),
						},
					},
				},
				{
					Commit: &github.Commit{
						Committer: &github.CommitAuthor{
							Date: timePtr(time.Unix(11, 0)),
						},
					},
				},
				{
					Commit: &github.Commit{
						Committer: &github.CommitAuthor{
							Date: timePtr(time.Unix(12, 0)),
						},
					},
				},
			},
			expectedTime: timePtr(time.Unix(12, 0)),
		},
		{
			commits: []github.RepositoryCommit{
				{
					Commit: &github.Commit{
						Committer: &github.CommitAuthor{
							Date: timePtr(time.Unix(10, 0)),
						},
					},
				},
				{
					Commit: &github.Commit{
						Committer: &github.CommitAuthor{
							Date: timePtr(time.Unix(9, 0)),
						},
					},
				},
				{
					Commit: &github.Commit{
						Committer: &github.CommitAuthor{
							Date: timePtr(time.Unix(8, 0)),
						},
					},
				},
			},
			expectedTime: timePtr(time.Unix(10, 0)),
		},
		{
			commits: []github.RepositoryCommit{
				{
					Commit: &github.Commit{
						Committer: &github.CommitAuthor{
							Date: timePtr(time.Unix(9, 0)),
						},
					},
				},
				{
					Commit: &github.Commit{
						Committer: &github.CommitAuthor{
							Date: timePtr(time.Unix(10, 0)),
						},
					},
				},
				{
					Commit: &github.Commit{
						Committer: &github.CommitAuthor{
							Date: timePtr(time.Unix(9, 0)),
						},
					},
				},
			},
			expectedTime: timePtr(time.Unix(10, 0)),
		},
	}
	for _, test := range tests {
		client, server, mux := github_test.InitTest()
		config := &GithubConfig{
			client:  client,
			Org:     "o",
			Project: "r",
		}
		mux.HandleFunc(fmt.Sprintf("/repos/o/r/pulls/1/commits"), func(w http.ResponseWriter, r *http.Request) {
			if r.Method != "GET" {
				t.Errorf("Unexpected method: %s", r.Method)
			}
			w.WriteHeader(http.StatusOK)
			data, err := json.Marshal(test.commits)
			if err != nil {
				t.Errorf("Unexpected error: %v", err)
			}
			w.Write(data)
			ts, err := config.LastModifiedTime(1)
			if err != nil {
				t.Errorf("unexpected error: %v", err)
			}
			if !ts.Equal(*test.expectedTime) {
				t.Errorf("expected: %v, saw: %v", test.expectedTime, ts)
			}
		})
		server.Close()
	}
}
Exemplo n.º 3
0
func TestValidateLGTMAfterPush(t *testing.T) {
	tests := []struct {
		issueEvents  []github.IssueEvent
		shouldPass   bool
		lastModified time.Time
	}{
		{
			issueEvents: []github.IssueEvent{
				{
					Event: stringPtr("labeled"),
					Label: &github.Label{
						Name: stringPtr("lgtm"),
					},
					CreatedAt: timePtr(time.Unix(10, 0)),
				},
			},
			lastModified: time.Unix(9, 0),
			shouldPass:   true,
		},
		{
			issueEvents: []github.IssueEvent{
				{
					Event: stringPtr("labeled"),
					Label: &github.Label{
						Name: stringPtr("lgtm"),
					},
					CreatedAt: timePtr(time.Unix(10, 0)),
				},
			},
			lastModified: time.Unix(11, 0),
			shouldPass:   false,
		},
		{
			issueEvents: []github.IssueEvent{
				{
					Event: stringPtr("labeled"),
					Label: &github.Label{
						Name: stringPtr("lgtm"),
					},
					CreatedAt: timePtr(time.Unix(12, 0)),
				},
				{
					Event: stringPtr("labeled"),
					Label: &github.Label{
						Name: stringPtr("lgtm"),
					},
					CreatedAt: timePtr(time.Unix(11, 0)),
				},
				{
					Event: stringPtr("labeled"),
					Label: &github.Label{
						Name: stringPtr("lgtm"),
					},
					CreatedAt: timePtr(time.Unix(10, 0)),
				},
			},
			lastModified: time.Unix(11, 0),
			shouldPass:   true,
		},
		{
			issueEvents: []github.IssueEvent{
				{
					Event: stringPtr("labeled"),
					Label: &github.Label{
						Name: stringPtr("lgtm"),
					},
					CreatedAt: timePtr(time.Unix(10, 0)),
				},
				{
					Event: stringPtr("labeled"),
					Label: &github.Label{
						Name: stringPtr("lgtm"),
					},
					CreatedAt: timePtr(time.Unix(11, 0)),
				},
				{
					Event: stringPtr("labeled"),
					Label: &github.Label{
						Name: stringPtr("lgtm"),
					},
					CreatedAt: timePtr(time.Unix(12, 0)),
				},
			},
			lastModified: time.Unix(11, 0),
			shouldPass:   true,
		},
	}
	for _, test := range tests {
		client, server, mux := github_test.InitTest()
		config := &SubmitQueueConfig{}
		config.Org = "o"
		config.Project = "r"
		config.SetClient(client)

		mux.HandleFunc(fmt.Sprintf("/repos/o/r/issues/1/events"), func(w http.ResponseWriter, r *http.Request) {
			if r.Method != "GET" {
				t.Errorf("Unexpected method: %s", r.Method)
			}
			w.WriteHeader(http.StatusOK)
			data, err := json.Marshal(test.issueEvents)
			if err != nil {
				t.Errorf("Unexpected error: %v", err)
			}
			w.Write(data)
			ok, err := config.validateLGTMAfterPush(&github.PullRequest{Number: intPtr(1)}, &test.lastModified)
			if err != nil {
				t.Errorf("unexpected error: %v", err)
			}
			if ok != test.shouldPass {
				t.Errorf("expected: %v, saw: %v", test.shouldPass, ok)
			}
		})
		server.Close()
	}
}