cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/read_test.go
332lines · modecode
| 1 | package parser |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/google/go-cmp/cmp" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | |
| 13 | "github.com/cloudflare/pint/internal/comments" |
| 14 | ) |
| 15 | |
| 16 | func TestReadContent(t *testing.T) { |
| 17 | type testCaseT struct { |
| 18 | input []byte |
| 19 | output []byte |
| 20 | comments []comments.Comment |
| 21 | ignored bool |
| 22 | shouldError bool |
| 23 | } |
| 24 | |
| 25 | testCases := []testCaseT{ |
| 26 | { |
| 27 | input: []byte(""), |
| 28 | output: []byte(""), |
| 29 | }, |
| 30 | { |
| 31 | input: []byte("\n"), |
| 32 | output: []byte("\n"), |
| 33 | }, |
| 34 | { |
| 35 | input: []byte("\n \n"), |
| 36 | output: []byte("\n \n"), |
| 37 | }, |
| 38 | { |
| 39 | input: []byte("foo bar"), |
| 40 | output: []byte("foo bar"), |
| 41 | }, |
| 42 | { |
| 43 | input: []byte("foo bar\n"), |
| 44 | output: []byte("foo bar\n"), |
| 45 | }, |
| 46 | { |
| 47 | input: []byte("line1\nline2"), |
| 48 | output: []byte("line1\nline2"), |
| 49 | }, |
| 50 | { |
| 51 | input: []byte("line1\nline2\n"), |
| 52 | output: []byte("line1\nline2\n"), |
| 53 | }, |
| 54 | { |
| 55 | input: []byte("line1\n\nline2\n\n"), |
| 56 | output: []byte("line1\n\nline2\n\n"), |
| 57 | }, |
| 58 | { |
| 59 | input: []byte("# pint ignore/next-line\nfoo\n"), |
| 60 | output: []byte("# pint ignore/next-line\n \n"), |
| 61 | }, |
| 62 | { |
| 63 | input: []byte("# pint ignore/next-line\nfoo"), |
| 64 | output: []byte("# pint ignore/next-line\n "), |
| 65 | }, |
| 66 | { |
| 67 | input: []byte("# pint ignore/next-line\nfoo\n\n"), |
| 68 | output: []byte("# pint ignore/next-line\n \n\n"), |
| 69 | }, |
| 70 | { |
| 71 | input: []byte("# pint ignore/next-line\nfoo\nbar\n"), |
| 72 | output: []byte("# pint ignore/next-line\n \nbar\n"), |
| 73 | }, |
| 74 | { |
| 75 | input: []byte("# pint ignore/next-line \nfoo\n"), |
| 76 | output: []byte("# pint ignore/next-line \n \n"), |
| 77 | }, |
| 78 | { |
| 79 | input: []byte("# pint ignore/next-line \nfoo\n"), |
| 80 | output: []byte("# pint ignore/next-line \n \n"), |
| 81 | }, |
| 82 | { |
| 83 | input: []byte("# pint ignore/next-line \nfoo\n"), |
| 84 | output: []byte("# pint ignore/next-line \n \n"), |
| 85 | }, |
| 86 | { |
| 87 | input: []byte("# pintignore/next-line\nfoo\n"), |
| 88 | output: []byte("# pintignore/next-line\nfoo\n"), |
| 89 | }, |
| 90 | { |
| 91 | input: []byte("# pint ignore/next-linex\nfoo\n"), |
| 92 | output: []byte("# pint ignore/next-linex\nfoo\n"), |
| 93 | }, |
| 94 | { |
| 95 | input: []byte("# pint ignore/begin\nfoo\nbar\n"), |
| 96 | output: []byte("# pint ignore/begin\n \n \n"), |
| 97 | }, |
| 98 | { |
| 99 | input: []byte("prefix # pint ignore/begin\nfoo\nbar\n"), |
| 100 | output: []byte("prefix # pint ignore/begin\n \n \n"), |
| 101 | }, |
| 102 | { |
| 103 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/begin"), |
| 104 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin"), |
| 105 | }, |
| 106 | { |
| 107 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/begin\nfoo\n"), |
| 108 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin\n \n"), |
| 109 | }, |
| 110 | { |
| 111 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/end\nfoo\n"), |
| 112 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/end\nfoo\n"), |
| 113 | }, |
| 114 | { |
| 115 | input: []byte("# pint ignore/begin\nfoo # pint ignore/line\nbar\n# pint ignore/begin"), |
| 116 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin"), |
| 117 | }, |
| 118 | { |
| 119 | input: []byte("line1\nline2 # pint ignore/line\n"), |
| 120 | output: []byte("line1\n # pint ignore/line\n"), |
| 121 | }, |
| 122 | { |
| 123 | input: []byte("line1\nline2 # pint ignore/line\nline3\n"), |
| 124 | output: []byte("line1\n # pint ignore/line\nline3\n"), |
| 125 | }, |
| 126 | { |
| 127 | input: []byte("{#- comment #} # pint ignore/line\n"), |
| 128 | output: []byte(" # pint ignore/line\n"), |
| 129 | }, |
| 130 | { |
| 131 | input: []byte("# pint ignore/file\nfoo\nbar\n# pint ignore/begin\nfoo\n# pint ignore/end\n"), |
| 132 | output: []byte("# pint ignore/file\n \n \n# pint ignore/begin\n \n# pint ignore/end\n"), |
| 133 | ignored: true, |
| 134 | }, |
| 135 | { |
| 136 | input: []byte("foo\n# pint ignore/file\nfoo\nbar\n# pint ignore/begin\nfoo\n# pint ignore/end\n"), |
| 137 | output: []byte("foo\n# pint ignore/file\n \n \n# pint ignore/begin\n \n# pint ignore/end\n"), |
| 138 | ignored: true, |
| 139 | }, |
| 140 | { |
| 141 | input: []byte(" {% raw %} # pint ignore/line\n"), |
| 142 | output: []byte(" # pint ignore/line\n"), |
| 143 | }, |
| 144 | { |
| 145 | input: []byte("{# comment #} # pint ignore/line\n"), |
| 146 | output: []byte(" # pint ignore/line\n"), |
| 147 | }, |
| 148 | { |
| 149 | input: []byte("# pint file/owner bob\n# pint rule/set xxx\n# pint bamboozle xxx\n"), |
| 150 | output: []byte("# pint file/owner bob\n# pint rule/set xxx\n# pint bamboozle xxx\n"), |
| 151 | comments: []comments.Comment{ |
| 152 | { |
| 153 | Type: comments.FileOwnerType, |
| 154 | Value: comments.Owner{Name: "bob", Line: 1}, |
| 155 | }, |
| 156 | }, |
| 157 | }, |
| 158 | { |
| 159 | input: []byte("{#- hide this comment -#} # pint ignore/line\n"), |
| 160 | output: []byte(" # pint ignore/line\n"), |
| 161 | }, |
| 162 | { |
| 163 | input: []byte("# pint ignore/begin\n - alert: Ignored\n # pint rule/set foo\n # pint rule/set bar\n expr: up\n# pint ignore/end\n"), |
| 164 | output: []byte("# pint ignore/begin\n \n \n \n \n# pint ignore/end\n"), |
| 165 | }, |
| 166 | { |
| 167 | input: []byte(` |
| 168 | # pint ignore/begin |
| 169 | {%- set foo = 1 %} |
| 170 | {% set bar = 2 -%} |
| 171 | {# comment #} |
| 172 | {# |
| 173 | comment |
| 174 | #} |
| 175 | # pint ignore/end |
| 176 | |
| 177 | - record: colo_job:up:count |
| 178 | expr: sum(foo) without(job) |
| 179 | |
| 180 | - record: invalid |
| 181 | expr: sum(foo) by ()) |
| 182 | |
| 183 | # pint ignore/begin |
| 184 | - record: colo_job:down:count |
| 185 | expr: up == {{ foo }} |
| 186 | # pint ignore/end |
| 187 | |
| 188 | - record: colo:multiline |
| 189 | expr: | |
| 190 | sum( |
| 191 | multiline |
| 192 | ) without(job, instance) |
| 193 | |
| 194 | - record: colo:multiline:sum |
| 195 | expr: | |
| 196 | sum(sum) without(job) |
| 197 | + |
| 198 | sum(sum) without(job) |
| 199 | |
| 200 | - record: colo:multiline2 |
| 201 | expr: >- |
| 202 | sum( |
| 203 | multiline2 |
| 204 | ) without(job, instance) |
| 205 | |
| 206 | - record: colo_job:up:byinstance |
| 207 | expr: sum(byinstance) by(instance) |
| 208 | |
| 209 | - record: instance_mode:node_cpu:rate4m |
| 210 | expr: sum(rate(node_cpu_seconds_total[4m])) without (cpu) |
| 211 | |
| 212 | - record: instance_mode:node_cpu:rate4m |
| 213 | expr: sum(rate(node_cpu_seconds_total[5m])) without (cpu) |
| 214 | |
| 215 | - record: instance_mode:node_cpu:rate5min |
| 216 | expr: sum(irate(node_cpu_seconds_total[5m])) without (cpu) |
| 217 | |
| 218 | - alert: Instance Is Down |
| 219 | expr: up == 0 |
| 220 | |
| 221 | - alert: Error Rate |
| 222 | expr: sum(rate(errors[5m])) > 0.5 |
| 223 | |
| 224 | - alert: Error Rate |
| 225 | expr: sum(rate(errors[5m])) > 0.5 |
| 226 | annotations: |
| 227 | link: http://docs |
| 228 | summary: 'error rate: {{ $value }}' |
| 229 | `), |
| 230 | output: []byte(` |
| 231 | # pint ignore/begin |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | # pint ignore/end |
| 239 | |
| 240 | - record: colo_job:up:count |
| 241 | expr: sum(foo) without(job) |
| 242 | |
| 243 | - record: invalid |
| 244 | expr: sum(foo) by ()) |
| 245 | |
| 246 | # pint ignore/begin |
| 247 | |
| 248 | |
| 249 | # pint ignore/end |
| 250 | |
| 251 | - record: colo:multiline |
| 252 | expr: | |
| 253 | sum( |
| 254 | multiline |
| 255 | ) without(job, instance) |
| 256 | |
| 257 | - record: colo:multiline:sum |
| 258 | expr: | |
| 259 | sum(sum) without(job) |
| 260 | + |
| 261 | sum(sum) without(job) |
| 262 | |
| 263 | - record: colo:multiline2 |
| 264 | expr: >- |
| 265 | sum( |
| 266 | multiline2 |
| 267 | ) without(job, instance) |
| 268 | |
| 269 | - record: colo_job:up:byinstance |
| 270 | expr: sum(byinstance) by(instance) |
| 271 | |
| 272 | - record: instance_mode:node_cpu:rate4m |
| 273 | expr: sum(rate(node_cpu_seconds_total[4m])) without (cpu) |
| 274 | |
| 275 | - record: instance_mode:node_cpu:rate4m |
| 276 | expr: sum(rate(node_cpu_seconds_total[5m])) without (cpu) |
| 277 | |
| 278 | - record: instance_mode:node_cpu:rate5min |
| 279 | expr: sum(irate(node_cpu_seconds_total[5m])) without (cpu) |
| 280 | |
| 281 | - alert: Instance Is Down |
| 282 | expr: up == 0 |
| 283 | |
| 284 | - alert: Error Rate |
| 285 | expr: sum(rate(errors[5m])) > 0.5 |
| 286 | |
| 287 | - alert: Error Rate |
| 288 | expr: sum(rate(errors[5m])) > 0.5 |
| 289 | annotations: |
| 290 | link: http://docs |
| 291 | summary: 'error rate: {{ $value }}' |
| 292 | `), |
| 293 | }, |
| 294 | { |
| 295 | // Test long string that doesn't fill into a single io.ReadAll read (512 bytes). |
| 296 | input: []byte(strings.Repeat("A", 518)), |
| 297 | output: []byte(strings.Repeat("A", 518)), |
| 298 | }, |
| 299 | } |
| 300 | |
| 301 | cmpErrorText := cmp.Comparer(func(x, y any) bool { |
| 302 | xe := x.(error) |
| 303 | ye := y.(error) |
| 304 | return xe.Error() == ye.Error() |
| 305 | }) |
| 306 | sameErrorText := cmp.FilterValues(func(x, y any) bool { |
| 307 | _, xe := x.(error) |
| 308 | _, ye := y.(error) |
| 309 | return xe && ye |
| 310 | }, cmpErrorText) |
| 311 | |
| 312 | for i, tc := range testCases { |
| 313 | t.Run(strconv.Itoa(i+1), func(t *testing.T) { |
| 314 | r := newContentReader(bytes.NewReader(tc.input)) |
| 315 | output, err := io.ReadAll(r) |
| 316 | |
| 317 | hadError := err != nil |
| 318 | if hadError != tc.shouldError { |
| 319 | t.Errorf("ReadContent() returned err=%v, expected=%v", err, tc.shouldError) |
| 320 | return |
| 321 | } |
| 322 | |
| 323 | if diff := cmp.Diff(tc.comments, r.comments, sameErrorText); diff != "" { |
| 324 | t.Errorf("ReadContent() returned wrong comments (-want +got):\n%s", diff) |
| 325 | return |
| 326 | } |
| 327 | |
| 328 | require.Equal(t, string(tc.output), string(output), "ReadContent() returned wrong output") |
| 329 | require.Equal(t, tc.ignored, r.skipAll, "ReadContent() returned wrong skipAll value") |
| 330 | }) |
| 331 | } |
| 332 | } |
| 333 | |