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