cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.52.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/reporter/bitbucket_api_test.go

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