cloudflare/pint

Public

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

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