Skip to content

Commit

Permalink
Merge pull request #814 from cloudflare/tests
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
prymitive authored Dec 13, 2023
2 parents 11cee0d + 46138bd commit 19b966b
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 9 deletions.
6 changes: 3 additions & 3 deletions internal/reporter/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down
190 changes: 184 additions & 6 deletions internal/reporter/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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))
}
})
}
Expand Down

0 comments on commit 19b966b

Please sign in to comment.