cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.71.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/diags/problems_test.go

162lines · modecode

1package diags
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8
9 "github.com/cloudflare/pint/internal/output"
10)
11
12func TestInjectDiagnostics(t *testing.T) {
13 type testCaseT struct {
14 input string
15 output string
16 diags []Diagnostic
17 firstLine, lastLine int
18 }
19
20 testCases := []testCaseT{
21 {
22 input: "expr: foo(bar) by()",
23 firstLine: 1,
24 lastLine: 1,
25 diags: []Diagnostic{
26 {FirstColumn: 1, LastColumn: 13, Message: "this is bad"},
27 },
28 output: `1 | expr: foo(bar) by()
29 ^^^^^^^^^^^^^ this is bad
30`,
31 },
32 {
33 input: "expr: foo(bar) on()",
34 firstLine: 1,
35 lastLine: 1,
36 diags: []Diagnostic{
37 {FirstColumn: 10, LastColumn: 11, Message: "oops"},
38 },
39 output: `1 | expr: foo(bar) on()
40 ^^ oops
41`,
42 },
43 {
44 input: `
45expr: sum(foo{job="bar"})
46 / on(a,b)
47 sum(foo)
48`,
49 firstLine: 2,
50 lastLine: 4,
51 diags: []Diagnostic{
52 {FirstColumn: 23, LastColumn: 29, Message: "abc"},
53 {FirstColumn: 26, LastColumn: 28, Message: "efg"},
54 },
55 output: `2 | expr: sum(foo{job="bar"})
563 | / on(a,b)
57 ^^^^^^^ abc
58 ^^^ efg
594 | sum(foo)
60`,
61 },
62 {
63 input: `
64expr: |
65 sum(bar{job="foo"})
66 / on(c,d)
67 sum(bar)
68`,
69 firstLine: 2,
70 lastLine: 5,
71 diags: []Diagnostic{
72 {FirstColumn: 23, LastColumn: 24, Message: "123"},
73 {FirstColumn: 31, LastColumn: 33, Message: "456"},
74 },
75 output: `2 | expr: |
763 | sum(bar{job="foo"})
774 | / on(c,d)
78 ^^ 123
795 | sum(bar)
80 ^^^ 456
81`,
82 },
83 {
84 input: `
85expr:
86 sum(bar{job="foo"})
87 / on(c,d)
88 sum(bar)
89`,
90 firstLine: 2,
91 lastLine: 5,
92 diags: []Diagnostic{
93 {FirstColumn: 23, LastColumn: 29, Message: "abc"},
94 {FirstColumn: 23, LastColumn: 29, Message: "efg"},
95 },
96 output: `2 | expr:
973 | sum(bar{job="foo"})
984 | / on(c,d)
99 ^^^^^^^ abc
100 efg
1015 | sum(bar)
102`,
103 },
104 {
105 input: `
106### BEGIN ###
107expr: >-
108 sum(bar{job="foo"})
109 / on(c,d)
110 sum(bar)
111### END ###
112`,
113 firstLine: 3,
114 lastLine: 6,
115 diags: []Diagnostic{
116 {FirstColumn: 23, LastColumn: 29, Message: "abc"},
117 {FirstColumn: 23, LastColumn: 29, Message: "efg"},
118 },
119 output: `3 | expr: >-
1204 | sum(bar{job="foo"})
1215 | / on(c,d)
122 ^^^^^^^ abc
123 efg
1246 | sum(bar)
125`,
126 },
127 {
128 input: "expr: cnt(bar) by()",
129 firstLine: 1,
130 lastLine: 1,
131 diags: []Diagnostic{
132 {FirstColumn: 14, LastColumn: 14, Message: "this is bad"},
133 },
134 output: `1 | expr: cnt(bar) by()
135 ^ this is bad
136`,
137 },
138 }
139
140 for _, tc := range testCases {
141 t.Run(tc.input, func(t *testing.T) {
142 key, val := parseYaml(tc.input)
143 require.NotNil(t, key)
144 require.NotNil(t, val)
145 pos := NewPositionRange(strings.Split(tc.input, "\n"), val, key.Column+2)
146 require.NotEmpty(t, pos)
147
148 diags := make([]Diagnostic, 0, len(tc.diags))
149 for _, diag := range tc.diags {
150 diags = append(diags, Diagnostic{
151 Message: diag.Message,
152 Pos: pos,
153 FirstColumn: diag.FirstColumn,
154 LastColumn: diag.LastColumn,
155 })
156 }
157
158 out := InjectDiagnostics(tc.input, diags, output.None, tc.firstLine, tc.lastLine)
159 require.Equal(t, tc.output, out)
160 })
161 }
162}
163