Permalink
Browse files

github: add test for GitHub Review API

  • Loading branch information...
1 parent b016b36 commit c1092ea855e43cb34738a3236c02ef6262e85291 @haya14busa committed Dec 16, 2016
Showing with 70 additions and 1 deletion.
  1. +3 −1 github.go
  2. +67 −0 github_test.go
View
@@ -81,14 +81,16 @@ func commentBody(c *Comment) string {
return tool + bodyPrefix + "\n" + c.Body
}
+var githubAPIHost = "api.github.com"
+
// Flash posts comments which has not been posted yet.
func (g *GitHubPullRequest) Flash() error {
if err := g.setPostedComment(); err != nil {
return err
}
// TODO(haya14busa,#58): remove host check when GitHub Enterprise supports
// Pull Request API.
- if g.cli.BaseURL.Host == "api.github.com" {
+ if g.cli.BaseURL.Host == githubAPIHost {
return g.postAsReviewComment()
}
return g.postCommentsForEach()
View
@@ -12,6 +12,22 @@ import (
"golang.org/x/oauth2"
)
+func setupGithub() (*http.ServeMux, *httptest.Server, *github.Client) {
+ mux := http.NewServeMux()
+ server := httptest.NewServer(mux)
+ client := github.NewClient(nil)
+ url, _ := url.Parse(server.URL)
+ client.BaseURL = url
+ client.UploadURL = url
+ return mux, server, client
+}
+
+func testMethod(t *testing.T, r *http.Request, want string) {
+ if got := r.Method; got != want {
+ t.Errorf("Request method: %v, want %v", got, want)
+ }
+}
+
const notokenSkipTestMes = "skipping test (requires actual Personal access tokens. export REVIEWDOG_TEST_GITHUB_API_TOKEN=<GitHub Personal Access Token>)"
func setupGitHubClient() *github.Client {
@@ -208,3 +224,54 @@ func TestGitHubPullRequest_Post_Flash_mock(t *testing.T) {
t.Errorf("API should be called 2 times, but %v times", apiCalled)
}
}
+
+func TestGitHubPullRequest_Post_Flash_mock_review_api(t *testing.T) {
+ mux, server, client := setupGithub()
+ defer server.Close()
+ defer func(u string) { githubAPIHost = u }(githubAPIHost)
+ u, _ := url.Parse(server.URL)
+ githubAPIHost = u.Host
+
+ mux.HandleFunc("/repos/o/r/pulls/1/comments", func(w http.ResponseWriter, r *http.Request) {
+ testMethod(t, r, "GET")
+ })
+ mux.HandleFunc("/repos/o/r/pulls/1/reviews", func(w http.ResponseWriter, r *http.Request) {
+ testMethod(t, r, "POST")
+
+ v := new(Review)
+ json.NewDecoder(r.Body).Decode(v)
+
+ if v.Body != nil {
+ t.Errorf("Review body = %v, want nil", *v.Body)
+ }
+ if want := "COMMENT"; *v.Event != want {
+ t.Errorf("Review event = %v, want %v", *v.Event, want)
+ }
+ c := v.Comments[0]
+
+ if want := bodyPrefix + "\n[reviewdog] test"; *c.Body != want {
+ t.Errorf("Review comment body = %v, want %v", *c.Body, want)
+ }
+ if want := "reviewdog.go"; *c.Path != want {
+ t.Errorf("Review comment position = %v, want %v", *c.Path, want)
+ }
+ if want := 17; *c.Position != want {
+ t.Errorf("Review comment position = %v, want %v", *c.Position, want)
+ }
+ })
+
+ g := NewGitHubPullReqest(client, "o", "r", 1, "SHA1")
+ comment := &Comment{
+ CheckResult: &CheckResult{
+ Path: "reviewdog.go",
+ },
+ LnumDiff: 17,
+ Body: "[reviewdog] test",
+ }
+ if err := g.Post(comment); err != nil {
+ t.Error(err)
+ }
+ if err := g.Flash(); err != nil {
+ t.Error(err)
+ }
+}

0 comments on commit c1092ea

Please sign in to comment.