cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/diags/problems_test.go
272lines · modecode
| 1 | package diags |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/require" |
| 8 | |
| 9 | "github.com/cloudflare/pint/internal/output" |
| 10 | ) |
| 11 | |
| 12 | func TestInjectDiagnostics(t *testing.T) { |
| 13 | type testCaseT struct { |
| 14 | input string |
| 15 | output string |
| 16 | diags []Diagnostic |
| 17 | } |
| 18 | |
| 19 | testCases := []testCaseT{ |
| 20 | { |
| 21 | input: "expr: foo(bar) by()", |
| 22 | diags: []Diagnostic{ |
| 23 | {FirstColumn: 1, LastColumn: 13, Message: "this is bad"}, |
| 24 | }, |
| 25 | output: `1 | expr: foo(bar) by() |
| 26 | ^^^^^^^^^^^^^ this is bad |
| 27 | `, |
| 28 | }, |
| 29 | { |
| 30 | input: "expr: foo(bar) on()", |
| 31 | diags: []Diagnostic{ |
| 32 | {FirstColumn: 10, LastColumn: 11, Message: "oops"}, |
| 33 | }, |
| 34 | output: `1 | expr: foo(bar) on() |
| 35 | ^^ oops |
| 36 | `, |
| 37 | }, |
| 38 | { |
| 39 | input: ` |
| 40 | expr: sum(foo{job="bar"}) |
| 41 | / on(a,b) |
| 42 | sum(foo) |
| 43 | `, |
| 44 | diags: []Diagnostic{ |
| 45 | {FirstColumn: 23, LastColumn: 29, Message: "abc"}, |
| 46 | {FirstColumn: 26, LastColumn: 28, Message: "efg"}, |
| 47 | }, |
| 48 | output: `2 | expr: sum(foo{job="bar"}) |
| 49 | 3 | / on(a,b) |
| 50 | ^^^ efg |
| 51 | ^^^^^^^ abc |
| 52 | 4 | sum(foo) |
| 53 | `, |
| 54 | }, |
| 55 | { |
| 56 | input: ` |
| 57 | expr: | |
| 58 | sum(bar{job="foo"}) |
| 59 | / on(c,d) |
| 60 | sum(bar) |
| 61 | `, |
| 62 | diags: []Diagnostic{ |
| 63 | {FirstColumn: 23, LastColumn: 24, Message: "123"}, |
| 64 | {FirstColumn: 31, LastColumn: 33, Message: "456"}, |
| 65 | }, |
| 66 | output: `3 | sum(bar{job="foo"}) |
| 67 | 4 | / on(c,d) |
| 68 | ^^ 123 |
| 69 | 5 | sum(bar) |
| 70 | ^^^ 456 |
| 71 | `, |
| 72 | }, |
| 73 | { |
| 74 | input: ` |
| 75 | expr: |
| 76 | sum(bar{job="foo"}) |
| 77 | / on(c,d) |
| 78 | sum(bar) |
| 79 | `, |
| 80 | diags: []Diagnostic{ |
| 81 | {FirstColumn: 23, LastColumn: 29, Message: "abc"}, |
| 82 | {FirstColumn: 23, LastColumn: 29, Message: "efg"}, |
| 83 | }, |
| 84 | output: `3 | sum(bar{job="foo"}) |
| 85 | 4 | / on(c,d) |
| 86 | ^^^^^^^ abc |
| 87 | efg |
| 88 | 5 | sum(bar) |
| 89 | `, |
| 90 | }, |
| 91 | { |
| 92 | input: ` |
| 93 | ### BEGIN ### |
| 94 | expr: >- |
| 95 | sum(bar{job="foo"}) |
| 96 | / on(c,d) |
| 97 | sum(bar) |
| 98 | ### END ### |
| 99 | `, |
| 100 | diags: []Diagnostic{ |
| 101 | {FirstColumn: 23, LastColumn: 29, Message: "abc"}, |
| 102 | {FirstColumn: 23, LastColumn: 29, Message: "efg"}, |
| 103 | }, |
| 104 | output: `4 | sum(bar{job="foo"}) |
| 105 | 5 | / on(c,d) |
| 106 | ^^^^^^^ abc |
| 107 | efg |
| 108 | 6 | sum(bar) |
| 109 | `, |
| 110 | }, |
| 111 | { |
| 112 | input: "expr: cnt(bar) by()", |
| 113 | diags: []Diagnostic{ |
| 114 | {FirstColumn: 14, LastColumn: 14, Message: "this is bad"}, |
| 115 | }, |
| 116 | output: `1 | expr: cnt(bar) by() |
| 117 | ^ this is bad |
| 118 | `, |
| 119 | }, |
| 120 | { |
| 121 | input: ` |
| 122 | expr: | |
| 123 | foo{ |
| 124 | job="bar" |
| 125 | } |
| 126 | `, |
| 127 | diags: []Diagnostic{ |
| 128 | {FirstColumn: 1, LastColumn: 16, Message: "this is bad"}, |
| 129 | }, |
| 130 | output: `3 | foo{ |
| 131 | 4 | job="bar" |
| 132 | 5 | } |
| 133 | ^ this is bad |
| 134 | `, |
| 135 | }, |
| 136 | } |
| 137 | |
| 138 | for _, tc := range testCases { |
| 139 | t.Run(tc.input, func(t *testing.T) { |
| 140 | key, val := parseYaml(tc.input) |
| 141 | require.NotNil(t, key) |
| 142 | require.NotNil(t, val) |
| 143 | pos := NewPositionRange(strings.Split(tc.input, "\n"), val, key.Column+2) |
| 144 | require.NotEmpty(t, pos) |
| 145 | |
| 146 | diags := make([]Diagnostic, 0, len(tc.diags)) |
| 147 | for _, diag := range tc.diags { |
| 148 | diags = append(diags, Diagnostic{ |
| 149 | Message: diag.Message, |
| 150 | Pos: pos, |
| 151 | FirstColumn: diag.FirstColumn, |
| 152 | LastColumn: diag.LastColumn, |
| 153 | }) |
| 154 | } |
| 155 | |
| 156 | out := InjectDiagnostics(tc.input, diags, output.None) |
| 157 | require.Equal(t, tc.output, out) |
| 158 | }) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | func TestCountDigits(t *testing.T) { |
| 163 | type testCaseT struct { |
| 164 | name string |
| 165 | input int |
| 166 | expected int |
| 167 | } |
| 168 | |
| 169 | testCases := []testCaseT{ |
| 170 | {name: "single digit", input: 1, expected: 1}, |
| 171 | {name: "two digits", input: 10, expected: 2}, |
| 172 | {name: "three digits", input: 100, expected: 3}, |
| 173 | {name: "zero", input: 0, expected: 0}, |
| 174 | {name: "four digits", input: 1000, expected: 4}, |
| 175 | {name: "five digits", input: 99999, expected: 5}, |
| 176 | {name: "six digits", input: 123456, expected: 6}, |
| 177 | } |
| 178 | |
| 179 | for _, tc := range testCases { |
| 180 | t.Run(tc.name, func(t *testing.T) { |
| 181 | require.Equal(t, tc.expected, countDigits(tc.input)) |
| 182 | }) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func TestLineCoverage(t *testing.T) { |
| 187 | type testCaseT struct { |
| 188 | name string |
| 189 | diags []Diagnostic |
| 190 | expected []int |
| 191 | } |
| 192 | |
| 193 | testCases := []testCaseT{ |
| 194 | { |
| 195 | name: "multiple lines", |
| 196 | diags: []Diagnostic{ |
| 197 | {Pos: PositionRanges{{Line: 1}, {Line: 2}}}, |
| 198 | {Pos: PositionRanges{{Line: 2}, {Line: 3}}}, |
| 199 | }, |
| 200 | expected: []int{1, 2, 3}, |
| 201 | }, |
| 202 | { |
| 203 | name: "empty", |
| 204 | diags: []Diagnostic{}, |
| 205 | expected: nil, |
| 206 | }, |
| 207 | { |
| 208 | name: "single line", |
| 209 | diags: []Diagnostic{ |
| 210 | {Pos: PositionRanges{{Line: 5}}}, |
| 211 | }, |
| 212 | expected: []int{5}, |
| 213 | }, |
| 214 | { |
| 215 | name: "duplicates", |
| 216 | diags: []Diagnostic{ |
| 217 | {Pos: PositionRanges{{Line: 3}, {Line: 3}}}, |
| 218 | {Pos: PositionRanges{{Line: 3}, {Line: 5}}}, |
| 219 | }, |
| 220 | expected: []int{3, 5}, |
| 221 | }, |
| 222 | } |
| 223 | |
| 224 | for _, tc := range testCases { |
| 225 | t.Run(tc.name, func(t *testing.T) { |
| 226 | result := lineCoverage(tc.diags) |
| 227 | if tc.expected == nil { |
| 228 | require.Empty(t, result) |
| 229 | } else { |
| 230 | require.Equal(t, tc.expected, result) |
| 231 | } |
| 232 | }) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | func TestInjectDiagnosticsKind(t *testing.T) { |
| 237 | input := "expr: foo(bar) by()" |
| 238 | diags := []Diagnostic{ |
| 239 | {FirstColumn: 1, LastColumn: 13, Message: "this is bad", Kind: Issue}, |
| 240 | {FirstColumn: 1, LastColumn: 13, Message: "this is context", Kind: Context}, |
| 241 | } |
| 242 | key, val := parseYaml(input) |
| 243 | pos := NewPositionRange(strings.Split(input, "\n"), val, key.Column+2) |
| 244 | for i := range diags { |
| 245 | diags[i].Pos = pos |
| 246 | } |
| 247 | out := InjectDiagnostics(input, diags, output.None) |
| 248 | expected := `1 | expr: foo(bar) by() |
| 249 | ^^^^^^^^^^^^^ this is bad |
| 250 | this is context |
| 251 | ` |
| 252 | require.Equal(t, expected, out) |
| 253 | } |
| 254 | |
| 255 | func TestInjectDiagnosticsOrder(t *testing.T) { |
| 256 | input := "expr: foo(bar) by()" |
| 257 | diags := []Diagnostic{ |
| 258 | {FirstColumn: 1, LastColumn: 13, Message: "this is bad", Kind: Issue}, |
| 259 | {FirstColumn: 10, LastColumn: 13, Message: "this is context", Kind: Context}, |
| 260 | } |
| 261 | key, val := parseYaml(input) |
| 262 | pos := NewPositionRange(strings.Split(input, "\n"), val, key.Column+2) |
| 263 | for i := range diags { |
| 264 | diags[i].Pos = pos |
| 265 | } |
| 266 | out := InjectDiagnostics(input, diags, output.None) |
| 267 | expected := `1 | expr: foo(bar) by() |
| 268 | ^^^^ this is context |
| 269 | ^^^^^^^^^^^^^ this is bad |
| 270 | ` |
| 271 | require.Equal(t, expected, out) |
| 272 | } |
| 273 | |