cloudflare/pint

Public

mirrored fromhttps://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
cf569e66637dd7c62a7f7b6762b4defada974793

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/diags/problems_test.go

160lines · 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 }
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: `
40expr: 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"})
493 | / on(a,b)
50 ^^^^^^^ abc
51 ^^^ efg
524 | sum(foo)
53`,
54 },
55 {
56 input: `
57expr: |
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"})
674 | / on(c,d)
68 ^^ 123
695 | sum(bar)
70 ^^^ 456
71`,
72 },
73 {
74 input: `
75expr:
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"})
854 | / on(c,d)
86 ^^^^^^^ abc
87 efg
885 | sum(bar)
89`,
90 },
91 {
92 input: `
93### BEGIN ###
94expr: >-
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"})
1055 | / on(c,d)
106 ^^^^^^^ abc
107 efg
1086 | 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: `
122expr: |
123 foo{
124 job="bar"
125 }
126`,
127 diags: []Diagnostic{
128 {FirstColumn: 1, LastColumn: 16, Message: "this is bad"},
129 },
130 output: `3 | foo{
1314 | job="bar"
1325 | }
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}