Skip to content

Commit 10ac794

Browse files
authored
Merge pull request #1418 from cloudflare/tests
Update tests
2 parents 0a60551 + 98f6c5a commit 10ac794

File tree

5 files changed

+97
-7
lines changed

5 files changed

+97
-7
lines changed

cmd/pint/bench/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
REVISION := 31a27fb9e0e778bd8fe6097aa58c8ea598fe9cec
1+
REVISION := 9f5c641bddde827d25c0284134c16a5ac3b94503
22

33
.PHONE: fetch
44
fetch:

internal/checks/query_cost_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func TestCostCheck(t *testing.T) {
238238
description: "6 results with 5 series max",
239239
content: content,
240240
checker: func(prom *promapi.FailoverGroup) checks.RuleChecker {
241-
return checks.NewCostCheck(prom, 5, 0, 0, 0, "", checks.Bug)
241+
return checks.NewCostCheck(prom, 5, 0, 0, 0, "Rule comment", checks.Bug)
242242
},
243243
prometheus: newSimpleProm,
244244
problems: true,

internal/checks/query_cost_test.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
problem:
4444
reporter: query/cost
4545
summary: query is too expensive
46-
details: ""
46+
details: 'Rule comment: Rule comment'
4747
diagnostics:
4848
- message: '`prom` Prometheus server at https://simple.example.com returned 6 result(s), maximum allowed series is 5.'
4949
pos:

internal/reporter/comments.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ func makeComments(summary Summary, showDuplicates bool) (comments []PendingComme
9898
buf.WriteString(diag.Message)
9999
buf.WriteRune('\n')
100100
if report.Problem.Details != "" {
101+
buf.WriteRune('\n')
101102
buf.WriteString(report.Problem.Details)
102-
buf.WriteString("\n")
103+
buf.WriteRune('\n')
103104
}
104-
buf.WriteString("\n")
105+
buf.WriteRune('\n')
105106
}
106107
buf.WriteString("</details>\n\n")
107108
} else {

internal/reporter/gitlab_test.go

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"log/slog"
77
"net/http"
88
"net/http/httptest"
9+
"os"
10+
"path/filepath"
911
"strconv"
1012
"strings"
1113
"testing"
@@ -44,12 +46,16 @@ func TestGitLabReporter(t *testing.T) {
4446
type errorCheck func(err error) error
4547

4648
p := parser.NewParser(false, parser.PrometheusSchema, model.UTF8Validation)
47-
mockFile := p.Parse(strings.NewReader(`
49+
mockRules := `
4850
- record: target is down
4951
expr: up == 0
5052
- record: sum errors
5153
expr: sum(errors) by (job)
52-
`))
54+
`
55+
tmpDir := t.TempDir()
56+
mockPath := filepath.Join(tmpDir, "foo.txt")
57+
require.NoError(t, os.WriteFile(mockPath, []byte(mockRules), 0o644))
58+
mockFile := p.Parse(strings.NewReader(mockRules))
5359

5460
fooReport := reporter.Report{
5561
Path: discovery.Path{
@@ -157,6 +163,26 @@ func TestGitLabReporter(t *testing.T) {
157163
158164
:information_source: To see documentation covering this check and instructions on how to resolve it [click here](https://cloudflare.github.io/pint/checks/%s.html).
159165
`, reporter, summary, details, reporter))
166+
}
167+
discBodyWithDiag := func(reporter, summary, details, yml, diag string) *string {
168+
return gitlab.Ptr(fmt.Sprintf(
169+
`:stop_sign: **Fatal** reported by [pint](https://cloudflare.github.io/pint/) **%s** check.
170+
171+
<details>
172+
<summary>%s</summary>
173+
174+
%s
175+
176+
%s
177+
178+
%s
179+
180+
</details>
181+
182+
------
183+
184+
:information_source: To see documentation covering this check and instructions on how to resolve it [click here](https://cloudflare.github.io/pint/checks/a.html).
185+
`, reporter, summary, yml, diag, details))
160186
}
161187
discPosition := func(path string, line int) *gitlab.PositionOptions {
162188
return gitlab.Ptr(gitlab.PositionOptions{
@@ -221,6 +247,69 @@ func TestGitLabReporter(t *testing.T) {
221247
s.ExpectGet(apiOpenMergeRequests).ReturnJSON([]gitlab.BasicMergeRequest{})
222248
}),
223249
},
250+
{
251+
description: "single MR with diagnostics",
252+
timeout: time.Minute,
253+
maxComments: 1,
254+
summary: reporter.NewSummary([]reporter.Report{
255+
{
256+
Path: discovery.Path{
257+
SymlinkTarget: mockPath,
258+
Name: mockPath,
259+
},
260+
ModifiedLines: []int{2},
261+
Rule: mockFile.Groups[0].Rules[0],
262+
Problem: checks.Problem{
263+
Reporter: "a",
264+
Summary: "foo error1",
265+
Details: "foo details",
266+
Diagnostics: []diags.Diagnostic{
267+
{
268+
Message: "Diagnostic message",
269+
Pos: diags.PositionRanges{
270+
{
271+
Line: 2,
272+
FirstColumn: 3,
273+
LastColumn: 24,
274+
},
275+
{
276+
Line: 3,
277+
FirstColumn: 3,
278+
LastColumn: 15,
279+
},
280+
},
281+
FirstColumn: 1,
282+
LastColumn: 24,
283+
},
284+
},
285+
Lines: diags.LineRange{First: 1, Last: 3},
286+
Severity: checks.Fatal,
287+
Anchor: checks.AnchorAfter,
288+
},
289+
},
290+
}),
291+
mock: httpmock.New(func(s *httpmock.Server) {
292+
s.ExpectGet(apiUser).ReturnJSON(gitlab.User{ID: 123})
293+
s.ExpectGet(apiOpenMergeRequests).ReturnJSON([]gitlab.BasicMergeRequest{
294+
{IID: 1},
295+
})
296+
s.ExpectGet(apiVersions(1)).ReturnJSON([]gitlab.MergeRequestDiffVersion{
297+
{ID: 2, HeadCommitSHA: "head", BaseCommitSHA: "base", StartCommitSHA: "start"},
298+
{ID: 1, HeadCommitSHA: "head", BaseCommitSHA: "base", StartCommitSHA: "start"},
299+
})
300+
s.ExpectGet(apiDiffs(1)).ReturnJSON([]gitlab.MergeRequestDiff{
301+
{OldPath: mockPath, NewPath: mockPath, Diff: fooDiff},
302+
})
303+
s.ExpectGet(apiDiscussions(1, true)).ReturnJSON([]gitlab.Discussion{})
304+
s.ExpectPost(apiDiscussions(1, false)).WithBodyJSON(gitlab.CreateMergeRequestDiscussionOptions{
305+
Body: discBodyWithDiag("a", "foo error1", "foo details", "```yaml\n2 | - record: target is down\n3 | expr: up == 0\n ^^ \n```", "Diagnostic message"),
306+
Position: discPosition(mockPath, 2),
307+
}).ReturnJSON(gitlab.Response{})
308+
}),
309+
errorHandler: func(err error) error {
310+
return err
311+
},
312+
},
224313
{
225314
description: "multiple merge requests",
226315
timeout: time.Minute,

0 commit comments

Comments
 (0)