cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.32.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/reporter/github_test.go

169lines · modecode

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