From 113d54f2d6f361d73576ac6252cafecb8f20d7ed Mon Sep 17 00:00:00 2001 From: Marcio Vieira Date: Wed, 3 Dec 2025 11:26:45 -0300 Subject: [PATCH 1/8] including test files, adjustind begin and end lines to work properly on gitlab report --- internal/gitlab/codequality.go | 59 +++++- internal/gitlab/codequality_test.go | 193 ++++++++++++++++++++ testdata/gitlab/changed_line/formatted.yaml | 18 ++ testdata/gitlab/changed_line/original.yaml | 18 ++ 4 files changed, 287 insertions(+), 1 deletion(-) create mode 100644 testdata/gitlab/changed_line/formatted.yaml create mode 100644 testdata/gitlab/changed_line/original.yaml diff --git a/internal/gitlab/codequality.go b/internal/gitlab/codequality.go index 6643322..cbeecf0 100644 --- a/internal/gitlab/codequality.go +++ b/internal/gitlab/codequality.go @@ -18,6 +18,7 @@ package gitlab import ( "crypto/sha256" "fmt" + "strings" "github.com/google/yamlfmt" ) @@ -35,7 +36,14 @@ type CodeQuality struct { // Location is the location of a Code Quality finding. type Location struct { - Path string `json:"path,omitempty"` + Path string `json:"path,omitempty"` + Lines *Lines `json:"lines,omitempty"` +} + +// Lines follows the GitLab Code Quality schema. +type Lines struct { + Begin *int `json:"begin,omitempty"` + End *int `json:"end,omitempty"` } // NewCodeQuality creates a new CodeQuality object from a yamlfmt.FileDiff. @@ -46,6 +54,8 @@ func NewCodeQuality(diff yamlfmt.FileDiff) (CodeQuality, bool) { return CodeQuality{}, false } + begin, end := detectChangedLines(&diff) + return CodeQuality{ Description: "Not formatted correctly, run yamlfmt to resolve.", Name: "yamlfmt", @@ -53,10 +63,57 @@ func NewCodeQuality(diff yamlfmt.FileDiff) (CodeQuality, bool) { Severity: Major, Location: Location{ Path: diff.Path, + Lines: &Lines{ + Begin: &begin, + End: &end, + }, }, }, true } +// detectChangedLines finds the first and last lines that differ between original and formatted content. +func detectChangedLines(diff *yamlfmt.FileDiff) (begin int, end int) { + original := strings.Split(diff.Diff.Original, "\n") + formatted := strings.Split(diff.Diff.Formatted, "\n") + + max := len(original) + if len(formatted) > max { + max = len(formatted) + } + + begin = -1 + end = -1 + + for i := 0; i < max; i++ { + origLine := "" + fmtLine := "" + + if i < len(original) { + origLine = original[i] + } + if i < len(formatted) { + fmtLine = formatted[i] + } + + if origLine != fmtLine { + if begin == -1 { + begin = i + 1 + } + end = i + 1 + } + } + + // fallback (should not happen because diff.Changed() was true) + if begin == -1 { + begin = 1 + } + if end == -1 { + end = 1 + } + + return begin, end +} + // fingerprint returns a 256-bit SHA256 hash of the original unformatted file. // This is used to uniquely identify a code quality finding. func fingerprint(diff yamlfmt.FileDiff) string { diff --git a/internal/gitlab/codequality_test.go b/internal/gitlab/codequality_test.go index 52c8a1c..8310585 100644 --- a/internal/gitlab/codequality_test.go +++ b/internal/gitlab/codequality_test.go @@ -16,6 +16,8 @@ package gitlab_test import ( "encoding/json" + "os" + "path/filepath" "testing" "github.com/google/go-cmp/cmp" @@ -93,3 +95,194 @@ func TestCodeQuality(t *testing.T) { }) } } + +func TestCodeQuality_DetectChangedLine(t *testing.T) { + t.Parallel() + + testdataDir := "../../testdata/gitlab/changed_line" + print(testdataDir) + originalPath := filepath.Join(testdataDir, "original.yaml") + formattedPath := filepath.Join(testdataDir, "formatted.yaml") + + original, err := os.ReadFile(originalPath) + if err != nil { + t.Fatalf("failed to read original file: %v", err) + } + + formatted, err := os.ReadFile(formattedPath) + if err != nil { + t.Fatalf("failed to read formatted file: %v", err) + } + + diff := yamlfmt.FileDiff{ + Path: "testdata/original.yaml", + Diff: &yamlfmt.FormatDiff{ + Original: string(original), + Formatted: string(formatted), + }, + } + + cq, ok := gitlab.NewCodeQuality(diff) + if !ok { + t.Fatal("NewCodeQuality() returned false, expected true") + } + + if cq.Location.Lines == nil { + t.Fatal("Location.Lines is nil") + } + + if cq.Location.Lines.Begin == nil { + t.Fatal("Location.Lines.Begin is nil") + } + + if cq.Location.Lines.End == nil { + t.Fatal("Location.Lines.End is nil") + } + + wantBeginLine := 6 + gotBeginLine := *cq.Location.Lines.Begin + + if gotBeginLine != wantBeginLine { + t.Errorf("Location.Lines.Begin = %d, want %d", gotBeginLine, wantBeginLine) + } + + wantEndLine := 8 + gotEndLine := *cq.Location.Lines.End + + if gotEndLine != wantEndLine { + t.Errorf("Location.Lines.End = %d, want %d", gotEndLine, wantEndLine) + } + + if cq.Location.Path != diff.Path { + t.Errorf("Location.Path = %q, want %q", cq.Location.Path, diff.Path) + } + + if cq.Description == "" { + t.Error("Description is empty") + } + + if cq.Name == "" { + t.Error("Name is empty") + } + + if cq.Fingerprint == "" { + t.Error("Fingerprint is empty") + } + + if cq.Severity == "" { + t.Error("Severity is empty") + } +} + +func TestCodeQuality_DetectChangedLines_MultipleCases(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + original string + formatted string + wantBegin int + wantEnd int + }{ + { + name: "single line change", + original: "a: b", + formatted: "a: b", + wantBegin: 1, + wantEnd: 1, + }, + { + name: "multiple consecutive lines", + original: `line1 +line2: value +line3: value +line4`, + formatted: `line1 +line2: value +line3: value +line4`, + wantBegin: 2, + wantEnd: 3, + }, + { + name: "non-consecutive changes", + original: `line1 +line2: value +line3 +line4: value +line5`, + formatted: `line1 +line2: value +line3 +line4: value +line5`, + wantBegin: 2, + wantEnd: 4, + }, + { + name: "change at beginning", + original: `key: value +line2 +line3`, + formatted: `key: value +line2 +line3`, + wantBegin: 1, + wantEnd: 1, + }, + { + name: "change at end", + original: `line1 +line2 +key: value`, + formatted: `line1 +line2 +key: value`, + wantBegin: 3, + wantEnd: 3, + }, + } + + for _, tc := range cases { + tc := tc + + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + diff := yamlfmt.FileDiff{ + Path: "test.yaml", + Diff: &yamlfmt.FormatDiff{ + Original: tc.original, + Formatted: tc.formatted, + }, + } + + cq, ok := gitlab.NewCodeQuality(diff) + if !ok { + t.Fatal("NewCodeQuality() returned false, expected true") + } + + if cq.Location.Lines == nil { + t.Fatal("Location.Lines is nil") + } + + if cq.Location.Lines.Begin == nil { + t.Fatal("Location.Lines.Begin is nil") + } + + if cq.Location.Lines.End == nil { + t.Fatal("Location.Lines.End is nil") + } + + gotBegin := *cq.Location.Lines.Begin + if gotBegin != tc.wantBegin { + t.Errorf("Location.Lines.Begin = %d, want %d", gotBegin, tc.wantBegin) + } + + gotEnd := *cq.Location.Lines.End + if gotEnd != tc.wantEnd { + t.Errorf("Location.Lines.End = %d, want %d", gotEnd, tc.wantEnd) + } + }) + } +} diff --git a/testdata/gitlab/changed_line/formatted.yaml b/testdata/gitlab/changed_line/formatted.yaml new file mode 100644 index 0000000..413d266 --- /dev/null +++ b/testdata/gitlab/changed_line/formatted.yaml @@ -0,0 +1,18 @@ +# Example configuration file +version: 1.0 + +services: + web: + image: nginx:latest + ports: + - 80:80 + - 443:443 + environment: + - ENV=production + - DEBUG=false + + database: + image: postgres:14 + environment: + - POSTGRES_DB=myapp + - POSTGRES_USER=admin \ No newline at end of file diff --git a/testdata/gitlab/changed_line/original.yaml b/testdata/gitlab/changed_line/original.yaml new file mode 100644 index 0000000..8c237a5 --- /dev/null +++ b/testdata/gitlab/changed_line/original.yaml @@ -0,0 +1,18 @@ +# Example configuration file +version: 1.0 + +services: + web: + image: nginx:latest + ports: + - 80:80 + - 443:443 + environment: + - ENV=production + - DEBUG=false + + database: + image: postgres:14 + environment: + - POSTGRES_DB=myapp + - POSTGRES_USER=admin \ No newline at end of file From 34c362ecaf9437b7adb43a91baf029b6ff71c3f6 Mon Sep 17 00:00:00 2001 From: Marcio Vieira Date: Wed, 3 Dec 2025 14:35:53 -0300 Subject: [PATCH 2/8] prop begin now is int, not pointer anymore and tests adjusted --- internal/gitlab/codequality.go | 4 ++-- internal/gitlab/codequality_test.go | 26 +++++++++----------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/internal/gitlab/codequality.go b/internal/gitlab/codequality.go index cbeecf0..d76306b 100644 --- a/internal/gitlab/codequality.go +++ b/internal/gitlab/codequality.go @@ -42,7 +42,7 @@ type Location struct { // Lines follows the GitLab Code Quality schema. type Lines struct { - Begin *int `json:"begin,omitempty"` + Begin int `json:"begin"` End *int `json:"end,omitempty"` } @@ -64,7 +64,7 @@ func NewCodeQuality(diff yamlfmt.FileDiff) (CodeQuality, bool) { Location: Location{ Path: diff.Path, Lines: &Lines{ - Begin: &begin, + Begin: begin, End: &end, }, }, diff --git a/internal/gitlab/codequality_test.go b/internal/gitlab/codequality_test.go index 8310585..e91d5be 100644 --- a/internal/gitlab/codequality_test.go +++ b/internal/gitlab/codequality_test.go @@ -131,21 +131,17 @@ func TestCodeQuality_DetectChangedLine(t *testing.T) { t.Fatal("Location.Lines is nil") } - if cq.Location.Lines.Begin == nil { - t.Fatal("Location.Lines.Begin is nil") - } - - if cq.Location.Lines.End == nil { - t.Fatal("Location.Lines.End is nil") - } - wantBeginLine := 6 - gotBeginLine := *cq.Location.Lines.Begin + gotBeginLine := cq.Location.Lines.Begin if gotBeginLine != wantBeginLine { t.Errorf("Location.Lines.Begin = %d, want %d", gotBeginLine, wantBeginLine) } + if cq.Location.Lines.End == nil { + t.Fatal("Location.Lines.End is nil") + } + wantEndLine := 8 gotEndLine := *cq.Location.Lines.End @@ -266,17 +262,13 @@ key: value`, t.Fatal("Location.Lines is nil") } - if cq.Location.Lines.Begin == nil { - t.Fatal("Location.Lines.Begin is nil") + gotBegin := cq.Location.Lines.Begin + if gotBegin != tc.wantBegin { + t.Errorf("Location.Lines.Begin = %d, want %d", gotBegin, tc.wantBegin) } if cq.Location.Lines.End == nil { - t.Fatal("Location.Lines.End is nil") - } - - gotBegin := *cq.Location.Lines.Begin - if gotBegin != tc.wantBegin { - t.Errorf("Location.Lines.Begin = %d, want %d", gotBegin, tc.wantBegin) + t.Fatalf("Location.Lines.End is nil") } gotEnd := *cq.Location.Lines.End From 6207cff5feb9f24c305f816a4a66720762af5dff Mon Sep 17 00:00:00 2001 From: Marcio Vieira Date: Thu, 11 Dec 2025 11:29:58 -0300 Subject: [PATCH 3/8] update stdout.txt to integration tests work properly --- .../command/testdata/gitlab_output/stdout/stdout.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/integrationtest/command/testdata/gitlab_output/stdout/stdout.txt b/integrationtest/command/testdata/gitlab_output/stdout/stdout.txt index d675074..1fba7d1 100644 --- a/integrationtest/command/testdata/gitlab_output/stdout/stdout.txt +++ b/integrationtest/command/testdata/gitlab_output/stdout/stdout.txt @@ -5,7 +5,11 @@ "fingerprint": "e9b14e45ca01a9a72fda9b8356a9ddbbaf7fe8c47116790a51cd699ae1679353", "severity": "major", "location": { - "path": "needs_format.yaml" + "path": "needs_format.yaml", + "lines": { + "begin": 2, + "end": 4 + } } } ] From 2a20f803a92b5ad484baa05936e7fcd75d7afaee Mon Sep 17 00:00:00 2001 From: Marcio Vieira Date: Thu, 11 Dec 2025 12:07:30 -0300 Subject: [PATCH 4/8] adjust end line to ommit prop when end is nil --- internal/gitlab/codequality.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/internal/gitlab/codequality.go b/internal/gitlab/codequality.go index d76306b..c2aafbe 100644 --- a/internal/gitlab/codequality.go +++ b/internal/gitlab/codequality.go @@ -65,14 +65,14 @@ func NewCodeQuality(diff yamlfmt.FileDiff) (CodeQuality, bool) { Path: diff.Path, Lines: &Lines{ Begin: begin, - End: &end, + End: end, }, }, }, true } // detectChangedLines finds the first and last lines that differ between original and formatted content. -func detectChangedLines(diff *yamlfmt.FileDiff) (begin int, end int) { +func detectChangedLines(diff *yamlfmt.FileDiff) (begin int, end *int) { original := strings.Split(diff.Diff.Original, "\n") formatted := strings.Split(diff.Diff.Formatted, "\n") @@ -82,7 +82,6 @@ func detectChangedLines(diff *yamlfmt.FileDiff) (begin int, end int) { } begin = -1 - end = -1 for i := 0; i < max; i++ { origLine := "" @@ -99,7 +98,8 @@ func detectChangedLines(diff *yamlfmt.FileDiff) (begin int, end int) { if begin == -1 { begin = i + 1 } - end = i + 1 + lineNum := i + 1 + end = &lineNum } } @@ -107,9 +107,6 @@ func detectChangedLines(diff *yamlfmt.FileDiff) (begin int, end int) { if begin == -1 { begin = 1 } - if end == -1 { - end = 1 - } return begin, end } From 32ee3ef6467e437f32595ceedaf19010140df9af Mon Sep 17 00:00:00 2001 From: Marcio Vieira Date: Thu, 11 Dec 2025 13:13:18 -0300 Subject: [PATCH 5/8] adjusting string slipt on codequality.go --- internal/gitlab/codequality.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/gitlab/codequality.go b/internal/gitlab/codequality.go index c2aafbe..b78aaa3 100644 --- a/internal/gitlab/codequality.go +++ b/internal/gitlab/codequality.go @@ -73,8 +73,8 @@ func NewCodeQuality(diff yamlfmt.FileDiff) (CodeQuality, bool) { // detectChangedLines finds the first and last lines that differ between original and formatted content. func detectChangedLines(diff *yamlfmt.FileDiff) (begin int, end *int) { - original := strings.Split(diff.Diff.Original, "\n") - formatted := strings.Split(diff.Diff.Formatted, "\n") + original := strings.Split(string(diff.Diff.Original), "\n") + formatted := strings.Split(string(diff.Diff.Formatted), "\n") max := len(original) if len(formatted) > max { From 11ef31e7b7644eea963d0032b6ba16509e861b0e Mon Sep 17 00:00:00 2001 From: Marcio Vieira Date: Thu, 18 Dec 2025 15:13:52 -0300 Subject: [PATCH 6/8] adjusting codequality to use max func to calculate lines and adjust tests --- internal/gitlab/codequality.go | 7 +- internal/gitlab/codequality_test.go | 188 +++++++++++------- .../all_lines_changed/formatted.yaml | 2 + .../all_lines_changed/original.yaml | 2 + .../append_last_line/formatted.yaml | 3 + .../append_last_line/original.yaml | 2 + .../testdata}/changed_line/formatted.yaml | 0 .../last_line_changed/formatted.yaml | 3 + .../last_line_changed/original.yaml | 3 + .../no_lines_changed/formatted.yaml | 2 + .../no_lines_changed/original.yaml | 2 + .../testdata}/changed_line/original.yaml | 0 .../single_line_changed/formatted.yaml | 3 + .../single_line_changed/original.yaml | 3 + 14 files changed, 143 insertions(+), 77 deletions(-) create mode 100644 internal/gitlab/testdata/changed_line/all_lines_changed/formatted.yaml create mode 100644 internal/gitlab/testdata/changed_line/all_lines_changed/original.yaml create mode 100644 internal/gitlab/testdata/changed_line/append_last_line/formatted.yaml create mode 100644 internal/gitlab/testdata/changed_line/append_last_line/original.yaml rename {testdata/gitlab => internal/gitlab/testdata}/changed_line/formatted.yaml (100%) create mode 100644 internal/gitlab/testdata/changed_line/last_line_changed/formatted.yaml create mode 100644 internal/gitlab/testdata/changed_line/last_line_changed/original.yaml create mode 100644 internal/gitlab/testdata/changed_line/no_lines_changed/formatted.yaml create mode 100644 internal/gitlab/testdata/changed_line/no_lines_changed/original.yaml rename {testdata/gitlab => internal/gitlab/testdata}/changed_line/original.yaml (100%) create mode 100644 internal/gitlab/testdata/changed_line/single_line_changed/formatted.yaml create mode 100644 internal/gitlab/testdata/changed_line/single_line_changed/original.yaml diff --git a/internal/gitlab/codequality.go b/internal/gitlab/codequality.go index b78aaa3..abcdb20 100644 --- a/internal/gitlab/codequality.go +++ b/internal/gitlab/codequality.go @@ -76,14 +76,11 @@ func detectChangedLines(diff *yamlfmt.FileDiff) (begin int, end *int) { original := strings.Split(string(diff.Diff.Original), "\n") formatted := strings.Split(string(diff.Diff.Formatted), "\n") - max := len(original) - if len(formatted) > max { - max = len(formatted) - } + maxLines := max(len(original), len(formatted)) begin = -1 - for i := 0; i < max; i++ { + for i := 0; i < maxLines; i++ { origLine := "" fmtLine := "" diff --git a/internal/gitlab/codequality_test.go b/internal/gitlab/codequality_test.go index e91d5be..6a02d96 100644 --- a/internal/gitlab/codequality_test.go +++ b/internal/gitlab/codequality_test.go @@ -22,6 +22,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/yamlfmt" + "github.com/google/yamlfmt/internal/assert" "github.com/google/yamlfmt/internal/gitlab" ) @@ -68,29 +69,24 @@ func TestCodeQuality(t *testing.T) { t.Parallel() got, gotOK := gitlab.NewCodeQuality(tc.diff) - if gotOK != tc.wantOK { - t.Fatalf("NewCodeQuality() = (%#v, %v), want (*, %v)", got, gotOK, tc.wantOK) - } + assert.Equal(t, tc.wantOK, gotOK) if !gotOK { return } - if tc.wantFingerprint != "" && tc.wantFingerprint != got.Fingerprint { - t.Fatalf("NewCodeQuality().Fingerprint = %q, want %q", got.Fingerprint, tc.wantFingerprint) + if tc.wantFingerprint != "" { + assert.Equal(t, tc.wantFingerprint, got.Fingerprint) } data, err := json.Marshal(got) - if err != nil { - t.Fatal(err) - } + assert.NilErr(t, err) var gotUnmarshal gitlab.CodeQuality - if err := json.Unmarshal(data, &gotUnmarshal); err != nil { - t.Fatal(err) - } + err = json.Unmarshal(data, &gotUnmarshal) + assert.NilErr(t, err) - if diff := cmp.Diff(got, gotUnmarshal); diff != "" { - t.Errorf("json.Marshal() and json.Unmarshal() mismatch (-got +want):\n%s", diff) + if d := cmp.Diff(got, gotUnmarshal); d != "" { + assert.EqualMsg(t, "", d, "json.Marshal() and json.Unmarshal() mismatch (-got +want):\n%s") } }) } @@ -99,74 +95,136 @@ func TestCodeQuality(t *testing.T) { func TestCodeQuality_DetectChangedLine(t *testing.T) { t.Parallel() - testdataDir := "../../testdata/gitlab/changed_line" - print(testdataDir) + testdataDir := "./testdata/changed_line" originalPath := filepath.Join(testdataDir, "original.yaml") formattedPath := filepath.Join(testdataDir, "formatted.yaml") original, err := os.ReadFile(originalPath) - if err != nil { - t.Fatalf("failed to read original file: %v", err) - } + assert.NilErr(t, err) formatted, err := os.ReadFile(formattedPath) - if err != nil { - t.Fatalf("failed to read formatted file: %v", err) - } + assert.NilErr(t, err) diff := yamlfmt.FileDiff{ Path: "testdata/original.yaml", Diff: &yamlfmt.FormatDiff{ - Original: string(original), - Formatted: string(formatted), + Original: original, + Formatted: formatted, }, } cq, ok := gitlab.NewCodeQuality(diff) - if !ok { - t.Fatal("NewCodeQuality() returned false, expected true") - } + assert.Assert(t, ok, "NewCodeQuality() returned false, expected true") - if cq.Location.Lines == nil { - t.Fatal("Location.Lines is nil") - } + assert.Assert(t, cq.Location.Lines != nil, "Location.Lines is nil") wantBeginLine := 6 gotBeginLine := cq.Location.Lines.Begin + assert.Equal(t, wantBeginLine, gotBeginLine) - if gotBeginLine != wantBeginLine { - t.Errorf("Location.Lines.Begin = %d, want %d", gotBeginLine, wantBeginLine) - } - - if cq.Location.Lines.End == nil { - t.Fatal("Location.Lines.End is nil") - } + assert.Assert(t, cq.Location.Lines.End != nil, "Location.Lines.End is nil") wantEndLine := 8 gotEndLine := *cq.Location.Lines.End + assert.Equal(t, wantEndLine, gotEndLine) - if gotEndLine != wantEndLine { - t.Errorf("Location.Lines.End = %d, want %d", gotEndLine, wantEndLine) - } + assert.Equal(t, diff.Path, cq.Location.Path) - if cq.Location.Path != diff.Path { - t.Errorf("Location.Path = %q, want %q", cq.Location.Path, diff.Path) - } + assert.Assert(t, cq.Description != "", "Description is empty") + assert.Assert(t, cq.Name != "", "Name is empty") + assert.Assert(t, cq.Fingerprint != "", "Fingerprint is empty") + assert.Assert(t, cq.Severity != "", "Severity is empty") +} - if cq.Description == "" { - t.Error("Description is empty") - } +func TestCodeQuality_DetectChangedLines_FromTestdata(t *testing.T) { + t.Parallel() - if cq.Name == "" { - t.Error("Name is empty") + type tc struct { + name string + dir string + wantOK bool + wantBegin int + wantEnd int } - if cq.Fingerprint == "" { - t.Error("Fingerprint is empty") + cases := []tc{ + { + name: "no lines changed", + dir: "no_lines_changed", + wantOK: false, + }, + { + name: "all lines changed", + dir: "all_lines_changed", + wantOK: true, + wantBegin: 1, + wantEnd: 2, + }, + { + name: "single line changed", + dir: "single_line_changed", + wantOK: true, + wantBegin: 2, + wantEnd: 2, + }, + { + name: "only the last line changed", + dir: "last_line_changed", + wantOK: true, + wantBegin: 3, + wantEnd: 3, + }, + { + name: "only change is appending a last line", + dir: "append_last_line", + wantOK: true, + wantBegin: 3, + wantEnd: 3, + }, } - if cq.Severity == "" { - t.Error("Severity is empty") + for _, c := range cases { + c := c + + t.Run(c.name, func(t *testing.T) { + t.Parallel() + + testdataDir := filepath.Join("./testdata/changed_line", c.dir) + originalPath := filepath.Join(testdataDir, "original.yaml") + formattedPath := filepath.Join(testdataDir, "formatted.yaml") + + original, err := os.ReadFile(originalPath) + assert.NilErr(t, err) + + formatted, err := os.ReadFile(formattedPath) + assert.NilErr(t, err) + + diff := yamlfmt.FileDiff{ + Path: filepath.ToSlash(filepath.Join("testdata/changed_line", c.dir, "original.yaml")), + Diff: &yamlfmt.FormatDiff{ + Original: original, + Formatted: formatted, + }, + } + + cq, ok := gitlab.NewCodeQuality(diff) + assert.Equal(t, c.wantOK, ok) + if !ok { + return + } + + assert.Assert(t, cq.Location.Lines != nil, "Location.Lines is nil") + assert.Equal(t, c.wantBegin, cq.Location.Lines.Begin) + + assert.Assert(t, cq.Location.Lines.End != nil, "Location.Lines.End is nil") + assert.Equal(t, c.wantEnd, *cq.Location.Lines.End) + + assert.Equal(t, diff.Path, cq.Location.Path) + assert.Assert(t, cq.Description != "", "Description is empty") + assert.Assert(t, cq.Name != "", "Name is empty") + assert.Assert(t, cq.Fingerprint != "", "Fingerprint is empty") + assert.Assert(t, cq.Severity != "", "Severity is empty") + }) } } @@ -248,33 +306,19 @@ key: value`, diff := yamlfmt.FileDiff{ Path: "test.yaml", Diff: &yamlfmt.FormatDiff{ - Original: tc.original, - Formatted: tc.formatted, + Original: []byte(tc.original), + Formatted: []byte(tc.formatted), }, } cq, ok := gitlab.NewCodeQuality(diff) - if !ok { - t.Fatal("NewCodeQuality() returned false, expected true") - } - - if cq.Location.Lines == nil { - t.Fatal("Location.Lines is nil") - } + assert.Assert(t, ok, "NewCodeQuality() returned false, expected true") - gotBegin := cq.Location.Lines.Begin - if gotBegin != tc.wantBegin { - t.Errorf("Location.Lines.Begin = %d, want %d", gotBegin, tc.wantBegin) - } + assert.Assert(t, cq.Location.Lines != nil, "Location.Lines is nil") + assert.Equal(t, tc.wantBegin, cq.Location.Lines.Begin) - if cq.Location.Lines.End == nil { - t.Fatalf("Location.Lines.End is nil") - } - - gotEnd := *cq.Location.Lines.End - if gotEnd != tc.wantEnd { - t.Errorf("Location.Lines.End = %d, want %d", gotEnd, tc.wantEnd) - } + assert.Assert(t, cq.Location.Lines.End != nil, "Location.Lines.End is nil") + assert.Equal(t, tc.wantEnd, *cq.Location.Lines.End) }) } } diff --git a/internal/gitlab/testdata/changed_line/all_lines_changed/formatted.yaml b/internal/gitlab/testdata/changed_line/all_lines_changed/formatted.yaml new file mode 100644 index 0000000..9d3554d --- /dev/null +++ b/internal/gitlab/testdata/changed_line/all_lines_changed/formatted.yaml @@ -0,0 +1,2 @@ +x: y +z: w \ No newline at end of file diff --git a/internal/gitlab/testdata/changed_line/all_lines_changed/original.yaml b/internal/gitlab/testdata/changed_line/all_lines_changed/original.yaml new file mode 100644 index 0000000..6f22244 --- /dev/null +++ b/internal/gitlab/testdata/changed_line/all_lines_changed/original.yaml @@ -0,0 +1,2 @@ +a: b +c: d \ No newline at end of file diff --git a/internal/gitlab/testdata/changed_line/append_last_line/formatted.yaml b/internal/gitlab/testdata/changed_line/append_last_line/formatted.yaml new file mode 100644 index 0000000..7eee4d9 --- /dev/null +++ b/internal/gitlab/testdata/changed_line/append_last_line/formatted.yaml @@ -0,0 +1,3 @@ +line1: ok +line2: ok +line3: added \ No newline at end of file diff --git a/internal/gitlab/testdata/changed_line/append_last_line/original.yaml b/internal/gitlab/testdata/changed_line/append_last_line/original.yaml new file mode 100644 index 0000000..171822c --- /dev/null +++ b/internal/gitlab/testdata/changed_line/append_last_line/original.yaml @@ -0,0 +1,2 @@ +line1: ok +line2: ok \ No newline at end of file diff --git a/testdata/gitlab/changed_line/formatted.yaml b/internal/gitlab/testdata/changed_line/formatted.yaml similarity index 100% rename from testdata/gitlab/changed_line/formatted.yaml rename to internal/gitlab/testdata/changed_line/formatted.yaml diff --git a/internal/gitlab/testdata/changed_line/last_line_changed/formatted.yaml b/internal/gitlab/testdata/changed_line/last_line_changed/formatted.yaml new file mode 100644 index 0000000..9aa6d9d --- /dev/null +++ b/internal/gitlab/testdata/changed_line/last_line_changed/formatted.yaml @@ -0,0 +1,3 @@ +line1: ok +line2: ok +line3: spaced \ No newline at end of file diff --git a/internal/gitlab/testdata/changed_line/last_line_changed/original.yaml b/internal/gitlab/testdata/changed_line/last_line_changed/original.yaml new file mode 100644 index 0000000..84f7a82 --- /dev/null +++ b/internal/gitlab/testdata/changed_line/last_line_changed/original.yaml @@ -0,0 +1,3 @@ +line1: ok +line2: ok +line3: spaced \ No newline at end of file diff --git a/internal/gitlab/testdata/changed_line/no_lines_changed/formatted.yaml b/internal/gitlab/testdata/changed_line/no_lines_changed/formatted.yaml new file mode 100644 index 0000000..6f22244 --- /dev/null +++ b/internal/gitlab/testdata/changed_line/no_lines_changed/formatted.yaml @@ -0,0 +1,2 @@ +a: b +c: d \ No newline at end of file diff --git a/internal/gitlab/testdata/changed_line/no_lines_changed/original.yaml b/internal/gitlab/testdata/changed_line/no_lines_changed/original.yaml new file mode 100644 index 0000000..6f22244 --- /dev/null +++ b/internal/gitlab/testdata/changed_line/no_lines_changed/original.yaml @@ -0,0 +1,2 @@ +a: b +c: d \ No newline at end of file diff --git a/testdata/gitlab/changed_line/original.yaml b/internal/gitlab/testdata/changed_line/original.yaml similarity index 100% rename from testdata/gitlab/changed_line/original.yaml rename to internal/gitlab/testdata/changed_line/original.yaml diff --git a/internal/gitlab/testdata/changed_line/single_line_changed/formatted.yaml b/internal/gitlab/testdata/changed_line/single_line_changed/formatted.yaml new file mode 100644 index 0000000..a4e1854 --- /dev/null +++ b/internal/gitlab/testdata/changed_line/single_line_changed/formatted.yaml @@ -0,0 +1,3 @@ +line1: ok +line2: spaced +line3: ok \ No newline at end of file diff --git a/internal/gitlab/testdata/changed_line/single_line_changed/original.yaml b/internal/gitlab/testdata/changed_line/single_line_changed/original.yaml new file mode 100644 index 0000000..9d187a0 --- /dev/null +++ b/internal/gitlab/testdata/changed_line/single_line_changed/original.yaml @@ -0,0 +1,3 @@ +line1: ok +line2: spaced +line3: ok \ No newline at end of file From eeefadb6fdab08249b2f1b05ddef0294611e264c Mon Sep 17 00:00:00 2001 From: Marcio Vieira Date: Mon, 22 Dec 2025 13:50:17 -0300 Subject: [PATCH 7/8] including new lines at the end of the test files --- .../testdata/changed_line/all_lines_changed/formatted.yaml | 2 +- .../testdata/changed_line/all_lines_changed/original.yaml | 2 +- .../testdata/changed_line/append_last_line/formatted.yaml | 2 +- .../gitlab/testdata/changed_line/append_last_line/original.yaml | 2 +- .../testdata/changed_line/last_line_changed/formatted.yaml | 2 +- .../testdata/changed_line/last_line_changed/original.yaml | 2 +- .../testdata/changed_line/no_lines_changed/formatted.yaml | 2 +- .../gitlab/testdata/changed_line/no_lines_changed/original.yaml | 2 +- .../testdata/changed_line/single_line_changed/formatted.yaml | 2 +- .../testdata/changed_line/single_line_changed/original.yaml | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/gitlab/testdata/changed_line/all_lines_changed/formatted.yaml b/internal/gitlab/testdata/changed_line/all_lines_changed/formatted.yaml index 9d3554d..2d0039a 100644 --- a/internal/gitlab/testdata/changed_line/all_lines_changed/formatted.yaml +++ b/internal/gitlab/testdata/changed_line/all_lines_changed/formatted.yaml @@ -1,2 +1,2 @@ x: y -z: w \ No newline at end of file +z: w diff --git a/internal/gitlab/testdata/changed_line/all_lines_changed/original.yaml b/internal/gitlab/testdata/changed_line/all_lines_changed/original.yaml index 6f22244..5851568 100644 --- a/internal/gitlab/testdata/changed_line/all_lines_changed/original.yaml +++ b/internal/gitlab/testdata/changed_line/all_lines_changed/original.yaml @@ -1,2 +1,2 @@ a: b -c: d \ No newline at end of file +c: d diff --git a/internal/gitlab/testdata/changed_line/append_last_line/formatted.yaml b/internal/gitlab/testdata/changed_line/append_last_line/formatted.yaml index 7eee4d9..efe5502 100644 --- a/internal/gitlab/testdata/changed_line/append_last_line/formatted.yaml +++ b/internal/gitlab/testdata/changed_line/append_last_line/formatted.yaml @@ -1,3 +1,3 @@ line1: ok line2: ok -line3: added \ No newline at end of file +line3: added diff --git a/internal/gitlab/testdata/changed_line/append_last_line/original.yaml b/internal/gitlab/testdata/changed_line/append_last_line/original.yaml index 171822c..cf9b973 100644 --- a/internal/gitlab/testdata/changed_line/append_last_line/original.yaml +++ b/internal/gitlab/testdata/changed_line/append_last_line/original.yaml @@ -1,2 +1,2 @@ line1: ok -line2: ok \ No newline at end of file +line2: ok diff --git a/internal/gitlab/testdata/changed_line/last_line_changed/formatted.yaml b/internal/gitlab/testdata/changed_line/last_line_changed/formatted.yaml index 9aa6d9d..89e9d23 100644 --- a/internal/gitlab/testdata/changed_line/last_line_changed/formatted.yaml +++ b/internal/gitlab/testdata/changed_line/last_line_changed/formatted.yaml @@ -1,3 +1,3 @@ line1: ok line2: ok -line3: spaced \ No newline at end of file +line3: spaced diff --git a/internal/gitlab/testdata/changed_line/last_line_changed/original.yaml b/internal/gitlab/testdata/changed_line/last_line_changed/original.yaml index 84f7a82..15ed2cc 100644 --- a/internal/gitlab/testdata/changed_line/last_line_changed/original.yaml +++ b/internal/gitlab/testdata/changed_line/last_line_changed/original.yaml @@ -1,3 +1,3 @@ line1: ok line2: ok -line3: spaced \ No newline at end of file +line3: spaced diff --git a/internal/gitlab/testdata/changed_line/no_lines_changed/formatted.yaml b/internal/gitlab/testdata/changed_line/no_lines_changed/formatted.yaml index 6f22244..5851568 100644 --- a/internal/gitlab/testdata/changed_line/no_lines_changed/formatted.yaml +++ b/internal/gitlab/testdata/changed_line/no_lines_changed/formatted.yaml @@ -1,2 +1,2 @@ a: b -c: d \ No newline at end of file +c: d diff --git a/internal/gitlab/testdata/changed_line/no_lines_changed/original.yaml b/internal/gitlab/testdata/changed_line/no_lines_changed/original.yaml index 6f22244..5851568 100644 --- a/internal/gitlab/testdata/changed_line/no_lines_changed/original.yaml +++ b/internal/gitlab/testdata/changed_line/no_lines_changed/original.yaml @@ -1,2 +1,2 @@ a: b -c: d \ No newline at end of file +c: d diff --git a/internal/gitlab/testdata/changed_line/single_line_changed/formatted.yaml b/internal/gitlab/testdata/changed_line/single_line_changed/formatted.yaml index a4e1854..6420006 100644 --- a/internal/gitlab/testdata/changed_line/single_line_changed/formatted.yaml +++ b/internal/gitlab/testdata/changed_line/single_line_changed/formatted.yaml @@ -1,3 +1,3 @@ line1: ok line2: spaced -line3: ok \ No newline at end of file +line3: ok diff --git a/internal/gitlab/testdata/changed_line/single_line_changed/original.yaml b/internal/gitlab/testdata/changed_line/single_line_changed/original.yaml index 9d187a0..ed56226 100644 --- a/internal/gitlab/testdata/changed_line/single_line_changed/original.yaml +++ b/internal/gitlab/testdata/changed_line/single_line_changed/original.yaml @@ -1,3 +1,3 @@ line1: ok line2: spaced -line3: ok \ No newline at end of file +line3: ok From 217a5dbc6387b8d83d03729b3a0dd784cabf9e31 Mon Sep 17 00:00:00 2001 From: Marcio Vieira Date: Mon, 22 Dec 2025 14:07:01 -0300 Subject: [PATCH 8/8] returning just int instead pointer --- internal/gitlab/codequality.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/gitlab/codequality.go b/internal/gitlab/codequality.go index abcdb20..28492fd 100644 --- a/internal/gitlab/codequality.go +++ b/internal/gitlab/codequality.go @@ -65,20 +65,21 @@ func NewCodeQuality(diff yamlfmt.FileDiff) (CodeQuality, bool) { Path: diff.Path, Lines: &Lines{ Begin: begin, - End: end, + End: &end, }, }, }, true } // detectChangedLines finds the first and last lines that differ between original and formatted content. -func detectChangedLines(diff *yamlfmt.FileDiff) (begin int, end *int) { +func detectChangedLines(diff *yamlfmt.FileDiff) (begin, end int) { original := strings.Split(string(diff.Diff.Original), "\n") formatted := strings.Split(string(diff.Diff.Formatted), "\n") maxLines := max(len(original), len(formatted)) begin = -1 + end = -1 for i := 0; i < maxLines; i++ { origLine := "" @@ -95,14 +96,13 @@ func detectChangedLines(diff *yamlfmt.FileDiff) (begin int, end *int) { if begin == -1 { begin = i + 1 } - lineNum := i + 1 - end = &lineNum + end = i + 1 } } - // fallback (should not happen because diff.Changed() was true) if begin == -1 { begin = 1 + end = 1 } return begin, end