cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.48.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/reporter/github_test.go

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