cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.50.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/reporter/bitbucket_api_test.go

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