cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.40.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/reporter/github_test.go

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