cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/reporter/github_test.go
167lines · modecode
| 1 | package reporter_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/rs/zerolog" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | |
| 13 | "github.com/cloudflare/pint/internal/checks" |
| 14 | "github.com/cloudflare/pint/internal/git" |
| 15 | "github.com/cloudflare/pint/internal/parser" |
| 16 | "github.com/cloudflare/pint/internal/reporter" |
| 17 | ) |
| 18 | |
| 19 | func TestGithubReporter(t *testing.T) { |
| 20 | zerolog.SetGlobalLevel(zerolog.FatalLevel) |
| 21 | |
| 22 | type testCaseT struct { |
| 23 | description string |
| 24 | reports []reporter.Report |
| 25 | httpHandler http.Handler |
| 26 | error string |
| 27 | gitCmd git.CommandRunner |
| 28 | |
| 29 | owner string |
| 30 | repo string |
| 31 | token string |
| 32 | prNum int |
| 33 | timeout time.Duration |
| 34 | } |
| 35 | |
| 36 | p := parser.NewParser() |
| 37 | mockRules, _ := p.Parse([]byte(` |
| 38 | - record: target is down |
| 39 | expr: up == 0 |
| 40 | - record: sum errors |
| 41 | expr: sum(errors) by (job) |
| 42 | `)) |
| 43 | |
| 44 | blameLine := func(sha string, line int, filename, content string) string { |
| 45 | return fmt.Sprintf(`%s %d %d 1 |
| 46 | filename %s |
| 47 | %s |
| 48 | `, sha, line, line, filename, content) |
| 49 | } |
| 50 | |
| 51 | for _, tc := range []testCaseT{ |
| 52 | { |
| 53 | description: "timeout errors out", |
| 54 | owner: "foo", |
| 55 | repo: "bar", |
| 56 | token: "something", |
| 57 | prNum: 123, |
| 58 | httpHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 59 | time.Sleep(1 * time.Second) |
| 60 | _, _ = w.Write([]byte("OK")) |
| 61 | }), |
| 62 | timeout: 100 * time.Millisecond, |
| 63 | gitCmd: func(args ...string) ([]byte, error) { |
| 64 | if args[0] == "rev-parse" { |
| 65 | return []byte("fake-commit-id"), nil |
| 66 | } |
| 67 | if args[0] == "blame" { |
| 68 | content := blameLine("fake-commit-id", 2, "foo.txt", "up == 0") |
| 69 | return []byte(content), nil |
| 70 | } |
| 71 | return nil, nil |
| 72 | }, |
| 73 | error: "failed to list pull request reviews: context deadline exceeded", |
| 74 | reports: []reporter.Report{ |
| 75 | { |
| 76 | SourcePath: "foo.txt", |
| 77 | ModifiedLines: []int{2}, |
| 78 | Rule: mockRules[1], |
| 79 | Problem: checks.Problem{ |
| 80 | Fragment: "syntax error", |
| 81 | Lines: []int{2}, |
| 82 | Reporter: "mock", |
| 83 | Text: "syntax error", |
| 84 | Severity: checks.Fatal, |
| 85 | }, |
| 86 | }, |
| 87 | }, |
| 88 | }, |
| 89 | { |
| 90 | description: "happy path", |
| 91 | owner: "foo", |
| 92 | repo: "bar", |
| 93 | token: "something", |
| 94 | prNum: 123, |
| 95 | timeout: 1000 * time.Millisecond, |
| 96 | gitCmd: func(args ...string) ([]byte, error) { |
| 97 | if args[0] == "rev-parse" { |
| 98 | return []byte("fake-commit-id"), nil |
| 99 | } |
| 100 | if args[0] == "blame" { |
| 101 | content := blameLine("fake-commit-id", 2, "foo.txt", "up == 0") |
| 102 | return []byte(content), nil |
| 103 | } |
| 104 | return nil, nil |
| 105 | }, |
| 106 | reports: []reporter.Report{ |
| 107 | { |
| 108 | SourcePath: "foo.txt", |
| 109 | ModifiedLines: []int{2}, |
| 110 | Rule: mockRules[1], |
| 111 | Problem: checks.Problem{ |
| 112 | Fragment: "syntax error", |
| 113 | Lines: []int{2}, |
| 114 | Reporter: "mock", |
| 115 | Text: "syntax error", |
| 116 | Severity: checks.Fatal, |
| 117 | }, |
| 118 | }, |
| 119 | }, |
| 120 | }, |
| 121 | } { |
| 122 | t.Run(tc.description, func(t *testing.T) { |
| 123 | var handler http.Handler |
| 124 | if tc.httpHandler != nil { |
| 125 | handler = tc.httpHandler |
| 126 | } else { |
| 127 | // Handler that checks for token. |
| 128 | handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 129 | auth := r.Header["Authorization"] |
| 130 | if len(auth) == 0 { |
| 131 | w.WriteHeader(500) |
| 132 | _, _ = w.Write([]byte("No token")) |
| 133 | t.Fatal("got a request with no token") |
| 134 | return |
| 135 | } |
| 136 | token := auth[0] |
| 137 | if token != fmt.Sprintf("Bearer %s", tc.token) { |
| 138 | w.WriteHeader(500) |
| 139 | _, _ = w.Write([]byte("Invalid token")) |
| 140 | t.Fatalf("got a request with invalid token (got %s)", token) |
| 141 | } |
| 142 | }) |
| 143 | } |
| 144 | srv := httptest.NewServer(handler) |
| 145 | defer srv.Close() |
| 146 | r, err := reporter.NewGithubReporter( |
| 147 | "v0.999", |
| 148 | srv.URL, |
| 149 | srv.URL, |
| 150 | tc.timeout, |
| 151 | tc.token, |
| 152 | tc.owner, |
| 153 | tc.repo, |
| 154 | tc.prNum, |
| 155 | tc.gitCmd, |
| 156 | ) |
| 157 | require.NoError(t, err) |
| 158 | |
| 159 | err = r.Submit(reporter.NewSummary(tc.reports)) |
| 160 | if tc.error == "" { |
| 161 | require.NoError(t, err) |
| 162 | } else { |
| 163 | require.EqualError(t, err, tc.error) |
| 164 | } |
| 165 | }) |
| 166 | } |
| 167 | } |
| 168 | |