Skip to content

Commit c2cb012

Browse files
committed
Fix test coverage
1 parent 5260056 commit c2cb012

6 files changed

Lines changed: 67 additions & 14 deletions

File tree

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/cloudflare/pint
33
go 1.26.0
44

55
require (
6-
github.com/akedrou/textdiff v0.1.0
76
github.com/cespare/xxhash/v2 v2.3.0
87
github.com/gkampitakis/go-snaps v0.5.21
98
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0 h1:XRzhVemXdgv
1414
github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0/go.mod h1:HKpQxkWaGLJ+D/5H8QRpyQXA1eKjxkFlOMwck5+33Jk=
1515
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
1616
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
17-
github.com/akedrou/textdiff v0.1.0 h1:K7nbOVQju7/coCXnJRJ2fsltTwbSvC+M4hKBUJRBRGY=
18-
github.com/akedrou/textdiff v0.1.0/go.mod h1:a9CCC49AKtFTmVDNFHDlCg7V/M7C7QExDAhb2SkL6DQ=
1917
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b h1:mimo19zliBX/vSQ6PWWSL9lK8qwHozUj03+zLoEB8O0=
2018
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b/go.mod h1:fvzegU4vN3H1qMT+8wDmzjAcDONcgo2/SZ/TyfdUOFs=
2119
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=

internal/reporter/github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (gr GithubReporter) commentPosition(p PendingComment) (side string, line in
5353
if nearest := p.changedLines.NearestBefore(oldLine); nearest > 0 {
5454
return "LEFT", nearest
5555
}
56-
return "LEFT", oldLine
56+
// No deleted lines in the diff, fall through to RIGHT side.
5757
}
5858
if p.changedLines.HasAfter(p.line) {
5959
return "RIGHT", p.line

internal/reporter/github_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,61 @@ func TestGitHubReporter(t *testing.T) {
886886
},
887887
}),
888888
},
889+
{
890+
description: "anchor before with only added lines in diff",
891+
owner: "foo",
892+
repo: "bar",
893+
token: "something",
894+
prNum: 123,
895+
maxComments: 50,
896+
timeout: time.Second,
897+
httpHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
898+
if r.Method == http.MethodGet && r.URL.Path == "/api/v3/repos/foo/bar/pulls/123/reviews" {
899+
_, _ = w.Write([]byte(`[]`))
900+
return
901+
}
902+
if r.Method == http.MethodGet && r.URL.Path == "/api/v3/repos/foo/bar/pulls/123/comments" {
903+
_, _ = w.Write([]byte(`[]`))
904+
return
905+
}
906+
if r.Method == http.MethodPost && r.URL.Path == "/api/v3/repos/foo/bar/pulls/123/comments" {
907+
body, _ := io.ReadAll(r.Body)
908+
b := strings.TrimSpace(strings.TrimRight(string(body), "\n\t\r"))
909+
// Diff only has added lines, no deleted lines to attach to.
910+
// Falls through to RIGHT side, moved to nearest added line: 5.
911+
if b != `{"body":":stop_sign: **Fatal** reported by [pint](https://cloudflare.github.io/pint/) **mock** check.\n\n------\n\nsyntax error\n\n<details>\n<summary>More information</summary>\nsyntax details\n</details>\n\n------\n\n:information_source: To see documentation covering this check and instructions on how to resolve it [click here](https://cloudflare.github.io/pint/checks/mock.html).\n","path":"foo.txt","line":5,"side":"RIGHT","commit_id":"HEAD"}` {
912+
t.Errorf("Unexpected comment: %s", b)
913+
t.FailNow()
914+
}
915+
}
916+
_, _ = w.Write([]byte(""))
917+
}),
918+
summary: reporter.NewSummary([]reporter.Report{
919+
{
920+
Path: discovery.Path{
921+
Name: "foo.txt",
922+
SymlinkTarget: "foo.txt",
923+
},
924+
925+
Changes: discovery.Changes{
926+
OldPath: "",
927+
Lines: git.LineNumbers{{Before: 0, After: 5}},
928+
},
929+
Rule: mockFile.Groups[0].Rules[0],
930+
Problem: checks.Problem{
931+
Lines: diags.LineRange{
932+
First: 3,
933+
Last: 3,
934+
},
935+
Reporter: "mock",
936+
Summary: "syntax error",
937+
Details: "syntax details",
938+
Severity: checks.Fatal,
939+
Anchor: checks.AnchorBefore,
940+
},
941+
},
942+
}),
943+
},
889944
{
890945
description: "review comment",
891946
owner: "foo",

internal/reporter/gitlab.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,17 @@ func reportToGitLabDiscussion(pending PendingComment, ver *gitlab.MergeRequestDi
434434

435435
// GitLab requires comments on lines present in the diff.
436436
// If the target line is not in the diff, move it to the nearest changed line.
437+
var useBefore bool
437438
if pending.anchor == checks.AnchorBefore {
438-
if !pending.changedLines.HasBefore(line) {
439-
if nearest := pending.changedLines.NearestBefore(line); nearest > 0 {
440-
line = nearest
441-
}
439+
if pending.changedLines.HasBefore(line) {
440+
useBefore = true
441+
} else if nearest := pending.changedLines.NearestBefore(line); nearest > 0 {
442+
line = nearest
443+
useBefore = true
442444
}
443-
} else {
445+
// No deleted lines in the diff, fall through to RIGHT side.
446+
}
447+
if !useBefore {
444448
if !pending.changedLines.HasAfter(line) {
445449
if nearest := pending.changedLines.NearestAfter(line); nearest > 0 {
446450
line = nearest
@@ -450,14 +454,11 @@ func reportToGitLabDiscussion(pending PendingComment, ver *gitlab.MergeRequestDi
450454
}
451455

452456
switch {
453-
// Deleted line: only old_line, no new_line.
454-
case pending.anchor == checks.AnchorBefore:
457+
case useBefore:
455458
d.Position.OldLine = new(int64(line))
456459
d.Position.NewLine = nil
457-
// Added line (oldLine==0): only new_line, no old_line.
458460
case oldLine == 0:
459461
d.Position.NewLine = new(int64(line))
460-
// Context line with known old->new mapping: both old_line and new_line.
461462
case oldLine > 0:
462463
d.Position.NewLine = new(int64(line))
463464
d.Position.OldLine = new(int64(oldLine))

internal/reporter/gitlab_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ Below is the list of checks that were disabled for each Prometheus server define
10261026
OldPath: new(mockPath),
10271027
NewPath: new(mockPath),
10281028
PositionType: new("text"),
1029-
OldLine: new(int64(3)),
1029+
NewLine: new(int64(3)),
10301030
}),
10311031
}).ReturnJSON(gitlab.Response{})
10321032
}),

0 commit comments

Comments
 (0)