From 46138bd499525a17202b56f98e93a049741f09dd Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Wed, 13 Dec 2023 14:01:41 +0000 Subject: [PATCH] Add more tests --- internal/reporter/github.go | 6 +- internal/reporter/github_test.go | 190 ++++++++++++++++++++++++++++++- 2 files changed, 187 insertions(+), 9 deletions(-) diff --git a/internal/reporter/github.go b/internal/reporter/github.go index 50dca7bf..c4eb0cfd 100644 --- a/internal/reporter/github.go +++ b/internal/reporter/github.go @@ -78,11 +78,11 @@ func (gr GithubReporter) Submit(summary Summary) error { } if review != nil { if err = gr.updateReview(review, summary); err != nil { - return err + return fmt.Errorf("failed to update pull request review: %w", err) } } else { if err = gr.createReview(headCommit, summary); err != nil { - return err + return fmt.Errorf("failed to create pull request review: %w", err) } } @@ -198,7 +198,7 @@ func (gr GithubReporter) createReview(headCommit string, summary Summary) error }, ) if err != nil { - return fmt.Errorf("failed to create review: %w", err) + return err } slog.Info("Pull request review created", slog.String("status", resp.Status)) return nil diff --git a/internal/reporter/github_test.go b/internal/reporter/github_test.go index 27873e1a..35b9cb8f 100644 --- a/internal/reporter/github_test.go +++ b/internal/reporter/github_test.go @@ -22,7 +22,7 @@ func TestGithubReporter(t *testing.T) { description string reports []reporter.Report httpHandler http.Handler - error string + error func(uri string) string gitCmd git.CommandRunner owner string @@ -69,7 +69,9 @@ filename %s } return nil, nil }, - error: "failed to list pull request reviews: context deadline exceeded", + error: func(_ string) string { + return "failed to list pull request reviews: context deadline exceeded" + }, reports: []reporter.Report{ { SourcePath: "foo.txt", @@ -84,13 +86,131 @@ filename %s }, }, }, + { + description: "list reviews error", + owner: "foo", + repo: "bar", + token: "something", + prNum: 123, + timeout: time.Second, + gitCmd: func(args ...string) ([]byte, error) { + if args[0] == "rev-parse" { + return []byte("fake-commit-id"), nil + } + if args[0] == "blame" { + content := blameLine("fake-commit-id", 2, "foo.txt", "up == 0") + return []byte(content), nil + } + return nil, nil + }, + httpHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && r.URL.Path == "/api/v3/repos/foo/bar/pulls/123/reviews" { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte("Error")) + return + } + _, _ = w.Write([]byte("")) + }), + error: func(uri string) string { + return fmt.Sprintf("failed to list pull request reviews: GET %s/api/v3/repos/foo/bar/pulls/123/reviews: 400 []", uri) + }, + reports: []reporter.Report{ + { + SourcePath: "foo.txt", + ModifiedLines: []int{2}, + Rule: mockRules[1], + Problem: checks.Problem{ + Lines: []int{2}, + Reporter: "mock", + Text: "syntax error", + Details: "syntax details", + Severity: checks.Fatal, + }, + }, + }, + }, { description: "happy path", owner: "foo", repo: "bar", token: "something", prNum: 123, - timeout: 1000 * time.Millisecond, + timeout: time.Second, + gitCmd: func(args ...string) ([]byte, error) { + if args[0] == "rev-parse" { + return []byte("fake-commit-id"), nil + } + if args[0] == "blame" { + content := blameLine("fake-commit-id", 2, "foo.txt", "up == 0") + return []byte(content), nil + } + return nil, nil + }, + reports: []reporter.Report{ + { + SourcePath: "foo.txt", + ModifiedLines: []int{2}, + Rule: mockRules[1], + Problem: checks.Problem{ + Lines: []int{2}, + Reporter: "mock", + Text: "syntax error", + Details: "syntax details", + Severity: checks.Fatal, + }, + }, + }, + }, + { + description: "error crating review", + owner: "foo", + repo: "bar", + token: "something", + prNum: 123, + timeout: time.Second, + gitCmd: func(args ...string) ([]byte, error) { + if args[0] == "rev-parse" { + return []byte("fake-commit-id"), nil + } + if args[0] == "blame" { + content := blameLine("fake-commit-id", 2, "foo.txt", "up == 0") + return []byte(content), nil + } + return nil, nil + }, + httpHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost && r.URL.Path == "/api/v3/repos/foo/bar/pulls/123/reviews" { + w.WriteHeader(http.StatusBadGateway) + _, _ = w.Write([]byte("Error")) + return + } + _, _ = w.Write([]byte("")) + }), + error: func(uri string) string { + return fmt.Sprintf("failed to create pull request review: POST %s/api/v3/repos/foo/bar/pulls/123/reviews: 502 []", uri) + }, + reports: []reporter.Report{ + { + SourcePath: "foo.txt", + ModifiedLines: []int{2}, + Rule: mockRules[1], + Problem: checks.Problem{ + Lines: []int{2}, + Reporter: "mock", + Text: "syntax error", + Details: "syntax details", + Severity: checks.Fatal, + }, + }, + }, + }, + { + description: "error updating existing review", + owner: "foo", + repo: "bar", + token: "something", + prNum: 123, + timeout: time.Second, gitCmd: func(args ...string) ([]byte, error) { if args[0] == "rev-parse" { return []byte("fake-commit-id"), nil @@ -101,6 +221,64 @@ filename %s } return nil, nil }, + httpHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && r.URL.Path == "/api/v3/repos/foo/bar/pulls/123/reviews" { + _, _ = w.Write([]byte(`[{"id":1,"body":"### This pull request was validated by [pint](https://github.com/cloudflare/pint).\nxxxx"}]`)) + return + } + if r.Method == http.MethodPut && r.URL.Path == "/api/v3/repos/foo/bar/pulls/123/reviews/1" { + w.WriteHeader(http.StatusBadGateway) + _, _ = w.Write([]byte("Error")) + return + } + _, _ = w.Write([]byte("")) + }), + error: func(uri string) string { + return fmt.Sprintf("failed to update pull request review: PUT %s/api/v3/repos/foo/bar/pulls/123/reviews/1: 502 []", uri) + }, + reports: []reporter.Report{ + { + SourcePath: "foo.txt", + ModifiedLines: []int{2}, + Rule: mockRules[1], + Problem: checks.Problem{ + Lines: []int{2}, + Reporter: "mock", + Text: "syntax error", + Details: "syntax details", + Severity: checks.Fatal, + }, + }, + }, + }, + { + description: "update existing review", + owner: "foo", + repo: "bar", + token: "something", + prNum: 123, + timeout: time.Second, + gitCmd: func(args ...string) ([]byte, error) { + if args[0] == "rev-parse" { + return []byte("fake-commit-id"), nil + } + if args[0] == "blame" { + content := blameLine("fake-commit-id", 2, "foo.txt", "up == 0") + return []byte(content), nil + } + return nil, nil + }, + httpHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && r.URL.Path == "/api/v3/repos/foo/bar/pulls/123/reviews" { + _, _ = w.Write([]byte(`[{"id":1,"body":"### This pull request was validated by [pint](https://github.com/cloudflare/pint).\nxxxx"}]`)) + return + } + if r.Method == http.MethodGet && r.URL.Path == "/api/v3/repos/foo/bar/pulls/123/comments" { + _, _ = w.Write([]byte(`[{"id":1,"commit_id":"fake-commit-id","path":"foo.txt","line":2,"body":":stop_sign: [mock](https://cloudflare.github.io/pint/checks/mock.html): syntax error\n\nsyntax details"}]`)) + return + } + _, _ = w.Write([]byte("")) + }), reports: []reporter.Report{ { SourcePath: "foo.txt", @@ -144,7 +322,7 @@ filename %s srv := httptest.NewServer(handler) defer srv.Close() r, err := reporter.NewGithubReporter( - "v0.999", + "v0.0.0", srv.URL, srv.URL, tc.timeout, @@ -157,10 +335,10 @@ filename %s require.NoError(t, err) err = r.Submit(reporter.NewSummary(tc.reports)) - if tc.error == "" { + if tc.error == nil { require.NoError(t, err) } else { - require.EqualError(t, err, tc.error) + require.EqualError(t, err, tc.error(srv.URL)) } }) }