cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.55.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/reporter/bitbucket_api_test.go

296lines · modecode

1package reporter
2
3import (
4 "log/slog"
5 "testing"
6
7 "github.com/neilotoole/slogt"
8 "github.com/stretchr/testify/require"
9
10 "github.com/cloudflare/pint/internal/checks"
11 "github.com/cloudflare/pint/internal/parser"
12)
13
14func TestPendingCommentToBitBucketComment(t *testing.T) {
15 type testCaseT struct {
16 description string
17 input pendingComment
18 output BitBucketPendingComment
19 changes *bitBucketPRChanges
20 }
21
22 testCases := []testCaseT{
23 {
24 description: "nil changes",
25 input: pendingComment{
26 severity: "NORMAL",
27 text: "this is text",
28 path: "foo.yaml",
29 line: 5,
30 },
31 output: BitBucketPendingComment{
32 Text: "this is text",
33 Severity: "NORMAL",
34 Anchor: BitBucketPendingCommentAnchor{
35 Path: "foo.yaml",
36 Line: 5,
37 DiffType: "EFFECTIVE",
38 LineType: "CONTEXT",
39 FileType: "FROM",
40 },
41 },
42 changes: nil,
43 },
44 {
45 description: "path not found in changes",
46 input: pendingComment{
47 severity: "NORMAL",
48 text: "this is text",
49 path: "foo.yaml",
50 line: 5,
51 },
52 output: BitBucketPendingComment{
53 Text: "this is text",
54 Severity: "NORMAL",
55 Anchor: BitBucketPendingCommentAnchor{
56 Path: "foo.yaml",
57 Line: 5,
58 DiffType: "EFFECTIVE",
59 LineType: "CONTEXT",
60 FileType: "FROM",
61 },
62 },
63 changes: &bitBucketPRChanges{
64 pathModifiedLines: map[string][]int{"bar.yaml": {1, 2, 3}},
65 pathLineMapping: map[string]map[int]int{"bar.yaml": {1: 1, 2: 5, 3: 3}},
66 },
67 },
68 {
69 description: "path found in changes",
70 input: pendingComment{
71 severity: "NORMAL",
72 text: "this is text",
73 path: "foo.yaml",
74 line: 5,
75 },
76 output: BitBucketPendingComment{
77 Text: "this is text",
78 Severity: "NORMAL",
79 Anchor: BitBucketPendingCommentAnchor{
80 Path: "foo.yaml",
81 Line: 5,
82 DiffType: "EFFECTIVE",
83 LineType: "ADDED",
84 FileType: "TO",
85 },
86 },
87 changes: &bitBucketPRChanges{
88 pathModifiedLines: map[string][]int{"foo.yaml": {1, 3, 5}},
89 pathLineMapping: map[string]map[int]int{"foo.yaml": {1: 1, 3: 3, 5: 4}},
90 },
91 },
92 }
93
94 for _, tc := range testCases {
95 t.Run(tc.description, func(t *testing.T) {
96 slog.SetDefault(slogt.New(t))
97 out := tc.input.toBitBucketComment(tc.changes)
98 require.Equal(t, tc.output, out, "pendingComment.toBitBucketComment() returned wrong BitBucketPendingComment")
99 })
100 }
101}
102
103func TestReportToAnnotation(t *testing.T) {
104 type testCaseT struct {
105 description string
106 input Report
107 output BitBucketAnnotation
108 }
109
110 testCases := []testCaseT{
111 {
112 description: "fatal report on modified line",
113 input: Report{
114 ReportedPath: "foo.yaml",
115 SourcePath: "foo.yaml",
116 ModifiedLines: []int{4, 5, 6},
117 Problem: checks.Problem{
118 Lines: parser.LineRange{
119 First: 5,
120 Last: 5,
121 },
122 Reporter: "mock",
123 Text: "report text",
124 Details: "mock details",
125 Severity: checks.Fatal,
126 },
127 },
128 output: BitBucketAnnotation{
129 Path: "foo.yaml",
130 Line: 5,
131 Message: "mock: report text",
132 Severity: "HIGH",
133 Type: "BUG",
134 Link: "https://cloudflare.github.io/pint/checks/mock.html",
135 },
136 },
137 {
138 description: "bug report on modified line",
139 input: Report{
140 ReportedPath: "foo.yaml",
141 SourcePath: "foo.yaml",
142 ModifiedLines: []int{4, 5, 6},
143 Problem: checks.Problem{
144 Lines: parser.LineRange{
145 First: 5,
146 Last: 5,
147 },
148 Reporter: "mock",
149 Text: "report text",
150 Severity: checks.Bug,
151 },
152 },
153 output: BitBucketAnnotation{
154 Path: "foo.yaml",
155 Line: 5,
156 Message: "mock: report text",
157 Severity: "MEDIUM",
158 Type: "BUG",
159 Link: "https://cloudflare.github.io/pint/checks/mock.html",
160 },
161 },
162 {
163 description: "warning report on modified line",
164 input: Report{
165 ReportedPath: "foo.yaml",
166 SourcePath: "foo.yaml",
167 ModifiedLines: []int{4, 5, 6},
168 Problem: checks.Problem{
169 Lines: parser.LineRange{
170 First: 5,
171 Last: 5,
172 },
173 Reporter: "mock",
174 Text: "report text",
175 Severity: checks.Warning,
176 },
177 },
178 output: BitBucketAnnotation{
179 Path: "foo.yaml",
180 Line: 5,
181 Message: "mock: report text",
182 Severity: "LOW",
183 Type: "CODE_SMELL",
184 Link: "https://cloudflare.github.io/pint/checks/mock.html",
185 },
186 },
187 {
188 description: "information report on modified line",
189 input: Report{
190 ReportedPath: "foo.yaml",
191 SourcePath: "foo.yaml",
192 ModifiedLines: []int{4, 5, 6},
193 Problem: checks.Problem{
194 Lines: parser.LineRange{
195 First: 5,
196 Last: 5,
197 },
198 Reporter: "mock",
199 Text: "report text",
200 Severity: checks.Information,
201 },
202 },
203 output: BitBucketAnnotation{
204 Path: "foo.yaml",
205 Line: 5,
206 Message: "mock: report text",
207 Severity: "LOW",
208 Type: "CODE_SMELL",
209 Link: "https://cloudflare.github.io/pint/checks/mock.html",
210 },
211 },
212 {
213 description: "fatal report on symlinked file",
214 input: Report{
215 ReportedPath: "foo.yaml",
216 SourcePath: "bar.yaml",
217 ModifiedLines: []int{4, 5, 6},
218 Problem: checks.Problem{
219 Lines: parser.LineRange{
220 First: 5,
221 Last: 5,
222 },
223 Reporter: "mock",
224 Text: "report text",
225 Severity: checks.Fatal,
226 },
227 },
228 output: BitBucketAnnotation{
229 Path: "foo.yaml",
230 Line: 5,
231 Message: "Problem detected on symlinked file bar.yaml: mock: report text",
232 Severity: "HIGH",
233 Type: "BUG",
234 Link: "https://cloudflare.github.io/pint/checks/mock.html",
235 },
236 },
237 {
238 description: "fatal report on symlinked file on unmodified line",
239 input: Report{
240 ReportedPath: "foo.yaml",
241 SourcePath: "bar.yaml",
242 ModifiedLines: []int{4, 5, 6},
243 Problem: checks.Problem{
244 Lines: parser.LineRange{
245 First: 7,
246 Last: 7,
247 },
248 Reporter: "mock",
249 Text: "report text",
250 Severity: checks.Fatal,
251 },
252 },
253 output: BitBucketAnnotation{
254 Path: "foo.yaml",
255 Line: 4,
256 Message: "Problem detected on symlinked file bar.yaml. Problem reported on unmodified line 7, annotation moved here: mock: report text",
257 Severity: "HIGH",
258 Type: "BUG",
259 Link: "https://cloudflare.github.io/pint/checks/mock.html",
260 },
261 },
262 {
263 description: "information report on unmodified line",
264 input: Report{
265 ReportedPath: "foo.yaml",
266 SourcePath: "foo.yaml",
267 ModifiedLines: []int{4, 5, 6},
268 Problem: checks.Problem{
269 Lines: parser.LineRange{
270 First: 1,
271 Last: 1,
272 },
273 Reporter: "mock",
274 Text: "report text",
275 Severity: checks.Information,
276 },
277 },
278 output: BitBucketAnnotation{
279 Path: "foo.yaml",
280 Line: 4,
281 Message: "Problem reported on unmodified line 1, annotation moved here: mock: report text",
282 Severity: "LOW",
283 Type: "CODE_SMELL",
284 Link: "https://cloudflare.github.io/pint/checks/mock.html",
285 },
286 },
287 }
288
289 for _, tc := range testCases {
290 t.Run(tc.description, func(t *testing.T) {
291 slog.SetDefault(slogt.New(t))
292 out := reportToAnnotation(tc.input)
293 require.Equal(t, tc.output, out, "reportToAnnotation() returned wrong BitBucketAnnotation")
294 })
295 }
296}
297