add test for GitHubPullRequest_Post_Flash with GitHub Review API
@@ -9,6 +9,7 @@ import (
"testing"
"github.com/google/go-github/github"
+ "github.com/kylelemons/godebug/pretty"
"golang.org/x/oauth2"
)
@@ -218,3 +219,85 @@ func TestGitHubPullRequest_Post_Flash_mock(t *testing.T) {
t.Errorf("API should be called 2 times, but %v times", apiCalled)
}
}
+
+func TestGitHubPullRequest_Post_Flash_review_api(t *testing.T) {
+ apiCalled := 0
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ apiCalled++
+ switch r.Method {
+ case "GET":
+ if r.URL.String() != "/repos/o/r/pulls/14/comments" {
+ t.Errorf("unexpected access: %v %v", r.Method, r.URL)
+ }
+ cs := []*github.PullRequestComment{
+ {
+ Path: github.String("reviewdog.go"),
+ Position: github.Int(1),
+ Body: github.String(bodyPrefix + "\n[reviewdog] already commented"),
+ },
+ }
+ if err := json.NewEncoder(w).Encode(cs); err != nil {
+ t.Fatal(err)
+ }
+ case "POST":
+ if r.URL.String() != "/repos/o/r/pulls/14/reviews" {
+ t.Errorf("unexpected access: %v %v", r.Method, r.URL)
+ }
+ var req github.PullRequestReviewRequest
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ t.Error(err)
+ }
+ if *req.Event != "COMMENT" {
+ t.Errorf("PullRequestReviewRequest.Event = %v, want COMMENT", *req.Event)
+ }
+ if req.Body != nil {
+ t.Errorf("PullRequestReviewRequest.Body = %v, want empty", *req.Body)
+ }
+ want := []*github.DraftReviewComment{
+ {
+ Path: github.String("reviewdog.go"),
+ Position: github.Int(14),
+ Body: github.String(bodyPrefix + "\n[reviewdog] new comment"),
+ },
+ }
+ if diff := pretty.Compare(want, req.Comments); diff != "" {
+ t.Errorf("req.Comments diff: (-got +want)\n%s", diff)
+ }
+ default:
+ t.Errorf("unexpected access: %v %v", r.Method, r.URL)
+ }
+ }))
+ defer func(h string) { githubAPIHost = h }(githubAPIHost)
+ u, _ := url.Parse(ts.URL)
+ githubAPIHost = u.Host
+ cli := github.NewClient(nil)
+ cli.BaseURL, _ = url.Parse(ts.URL)
+ g := NewGitHubPullReqest(cli, "o", "r", 14, "")
+ comments := []*Comment{
+ {
+ CheckResult: &CheckResult{
+ Path: "reviewdog.go",
+ },
+ LnumDiff: 1,
+ Body: "[reviewdog] already commented",
+ },
+ {
+ CheckResult: &CheckResult{
+ Path: "reviewdog.go",
+ },
+ LnumDiff: 14,
+ Body: "[reviewdog] new comment",
+ },
+ }
+ for _, c := range comments {
+ if err := g.Post(c); err != nil {
+ t.Error(err)
+ }
+ }
+ if err := g.Flash(); err != nil {
+ t.Error(err)
+ }
+ if apiCalled != 2 {
+ t.Errorf("GitHub API should be called once; called %v times", apiCalled)
+ }
+}
