cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.39.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/reporter/github_test.go

164lines · modecode

1package reporter_test
2
3import (
4 "fmt"
5 "net/http"
6 "net/http/httptest"
7 "testing"
8 "time"
9
10 "github.com/cloudflare/pint/internal/checks"
11 "github.com/cloudflare/pint/internal/git"
12 "github.com/cloudflare/pint/internal/parser"
13 "github.com/cloudflare/pint/internal/reporter"
14)
15
16func TestGithubReporter(t *testing.T) {
17 type errorCheck func(t *testing.T, err error) error
18
19 type testCaseT struct {
20 description string
21 reports []reporter.Report
22 httpHandler http.Handler
23 errorHandler errorCheck
24 gitCmd git.CommandRunner
25
26 owner string
27 repo string
28 token string
29 prNum int
30 timeout time.Duration
31 }
32
33 p := parser.NewParser()
34 mockRules, _ := p.Parse([]byte(`
35- record: target is down
36 expr: up == 0
37- record: sum errors
38 expr: sum(errors) by (job)
39`))
40
41 for _, tc := range []testCaseT{
42 {
43 description: "timeout errors out",
44 owner: "foo",
45 repo: "bar",
46 token: "something",
47 prNum: 123,
48 httpHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
49 time.Sleep(1 * time.Second)
50 _, _ = w.Write([]byte("OK"))
51 }),
52 timeout: 100 * time.Millisecond,
53 gitCmd: func(args ...string) ([]byte, error) {
54 if args[0] == "rev-parse" {
55 return []byte("fake-commit-id"), nil
56 }
57 if args[0] == "blame" {
58 content := blameLine("fake-commit-id", 2, "foo.txt", "up == 0")
59 return []byte(content), nil
60 }
61 return nil, nil
62 },
63 errorHandler: func(t *testing.T, err error) error {
64 if err == nil {
65 return fmt.Errorf("expected an error")
66 }
67 if err.Error() != "failed to create a new check run: context deadline exceeded" {
68 return fmt.Errorf("unexpected error")
69 }
70 return nil
71 },
72 reports: []reporter.Report{
73 {
74 SourcePath: "foo.txt",
75 ModifiedLines: []int{2},
76 Rule: mockRules[1],
77 Problem: checks.Problem{
78 Fragment: "syntax error",
79 Lines: []int{2},
80 Reporter: "mock",
81 Text: "syntax error",
82 Severity: checks.Fatal,
83 },
84 },
85 },
86 },
87 {
88 description: "happy path",
89 owner: "foo",
90 repo: "bar",
91 token: "something",
92 prNum: 123,
93 timeout: 1000 * time.Millisecond,
94 errorHandler: func(t *testing.T, err error) error {
95 return err
96 },
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 := reporter.NewGithubReporter(
148 srv.URL,
149 srv.URL,
150 tc.timeout,
151 tc.token,
152 tc.owner,
153 tc.repo,
154 tc.gitCmd,
155 )
156
157 err := r.Submit(reporter.NewSummary(tc.reports))
158 if e := tc.errorHandler(t, err); e != nil {
159 t.Errorf("error check failure: %s", e)
160 return
161 }
162 })
163 }
164}
165