feat(parser/diff): limit max diff output in the end
This commit is contained in:
parent
997e7dde20
commit
43bfbba327
4
go.mod
4
go.mod
|
@ -12,6 +12,7 @@ require (
|
||||||
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7
|
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7
|
||||||
github.com/mcuadros/go-defaults v1.2.0
|
github.com/mcuadros/go-defaults v1.2.0
|
||||||
github.com/mitchellh/mapstructure v1.5.0
|
github.com/mitchellh/mapstructure v1.5.0
|
||||||
|
github.com/stretchr/testify v1.10.0
|
||||||
google.golang.org/grpc v1.73.0
|
google.golang.org/grpc v1.73.0
|
||||||
google.golang.org/protobuf v1.36.6
|
google.golang.org/protobuf v1.36.6
|
||||||
)
|
)
|
||||||
|
@ -24,6 +25,7 @@ require (
|
||||||
github.com/cloudflare/circl v1.6.1 // indirect
|
github.com/cloudflare/circl v1.6.1 // indirect
|
||||||
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
|
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
|
||||||
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
|
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/emirpasic/gods v1.18.1 // indirect
|
github.com/emirpasic/gods v1.18.1 // indirect
|
||||||
github.com/fatih/camelcase v1.0.0 // indirect
|
github.com/fatih/camelcase v1.0.0 // indirect
|
||||||
github.com/fatih/structs v1.1.0 // indirect
|
github.com/fatih/structs v1.1.0 // indirect
|
||||||
|
@ -33,6 +35,7 @@ require (
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
||||||
github.com/kevinburke/ssh_config v1.2.0 // indirect
|
github.com/kevinburke/ssh_config v1.2.0 // indirect
|
||||||
github.com/pjbgf/sha1cd v0.3.2 // indirect
|
github.com/pjbgf/sha1cd v0.3.2 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/sergi/go-diff v1.4.0 // indirect
|
github.com/sergi/go-diff v1.4.0 // indirect
|
||||||
github.com/skeema/knownhosts v1.3.1 // indirect
|
github.com/skeema/knownhosts v1.3.1 // indirect
|
||||||
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
||||||
|
@ -43,4 +46,5 @@ require (
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250603155806-513f23925822 // indirect
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20250603155806-513f23925822 // indirect
|
||||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
@ -124,16 +124,7 @@ func (d *Diff) generateDiffComment(answerStr, resultStr string, output Output) s
|
||||||
if output.MaxDiffLines == 0 { // real default value
|
if output.MaxDiffLines == 0 { // real default value
|
||||||
output.MaxDiffLines = 50
|
output.MaxDiffLines = 50
|
||||||
}
|
}
|
||||||
|
|
||||||
truncated := false
|
truncated := false
|
||||||
if len(answerStr) > output.MaxDiffLength {
|
|
||||||
answerStr = answerStr[:output.MaxDiffLength]
|
|
||||||
truncated = true
|
|
||||||
}
|
|
||||||
if len(resultStr) > output.MaxDiffLength {
|
|
||||||
resultStr = resultStr[:output.MaxDiffLength]
|
|
||||||
truncated = true
|
|
||||||
}
|
|
||||||
|
|
||||||
answerLines := strings.Split(answerStr, "\n")
|
answerLines := strings.Split(answerStr, "\n")
|
||||||
resultLines := strings.Split(resultStr, "\n")
|
resultLines := strings.Split(resultStr, "\n")
|
||||||
|
@ -150,8 +141,8 @@ func (d *Diff) generateDiffComment(answerStr, resultStr string, output Output) s
|
||||||
); n += 1 {
|
); n += 1 {
|
||||||
}
|
}
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
answerLines = answerLines[n-1:]
|
answerLines = answerLines[n:]
|
||||||
resultLines = resultLines[n-1:]
|
resultLines = resultLines[n:]
|
||||||
commonPrefixLineCount = n
|
commonPrefixLineCount = n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -173,7 +164,10 @@ func (d *Diff) generateDiffComment(answerStr, resultStr string, output Output) s
|
||||||
})
|
})
|
||||||
diffOutput := diffText(diffs)
|
diffOutput := diffText(diffs)
|
||||||
diffOutput = strings.TrimSuffix(diffOutput, "\n ")
|
diffOutput = strings.TrimSuffix(diffOutput, "\n ")
|
||||||
|
if len(diffOutput) > output.MaxDiffLength {
|
||||||
|
diffOutput = diffOutput[:output.MaxDiffLength]
|
||||||
|
truncated = true
|
||||||
|
}
|
||||||
if truncated {
|
if truncated {
|
||||||
diffOutput += "\n\n(truncated)"
|
diffOutput += "\n\n(truncated)"
|
||||||
}
|
}
|
||||||
|
|
67
internal/parser/diff/parser_test.go
Normal file
67
internal/parser/diff/parser_test.go
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
package diff
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGenerateDiffComment(t *testing.T) {
|
||||||
|
d := &Diff{}
|
||||||
|
|
||||||
|
t.Run("HideCommonPrefix with common prefix", func(t *testing.T) {
|
||||||
|
answerStr := "line1\nline2\nline3\nline4"
|
||||||
|
resultStr := "line1\nline2\nlineA\nlineB"
|
||||||
|
output := Output{
|
||||||
|
HideCommonPrefix: true,
|
||||||
|
}
|
||||||
|
comment := d.generateDiffComment(answerStr, resultStr, output)
|
||||||
|
expected := "```diff\n(2 line(s) of common prefix hidden)\n\n- line3\n- line4\n+ lineA\n+ lineB\n```\n"
|
||||||
|
assert.Equal(t, expected, comment)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("HideCommonPrefix with no common prefix", func(t *testing.T) {
|
||||||
|
answerStr := "line1\nline2"
|
||||||
|
resultStr := "lineA\nlineB"
|
||||||
|
output := Output{
|
||||||
|
HideCommonPrefix: true,
|
||||||
|
}
|
||||||
|
comment := d.generateDiffComment(answerStr, resultStr, output)
|
||||||
|
expected := "```diff\n- line1\n- line2\n+ lineA\n+ lineB\n```\n"
|
||||||
|
assert.Equal(t, expected, comment)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("HideCommonPrefix with identical content", func(t *testing.T) {
|
||||||
|
answerStr := "line1\nline2"
|
||||||
|
resultStr := "line1\nline2"
|
||||||
|
output := Output{
|
||||||
|
HideCommonPrefix: true,
|
||||||
|
}
|
||||||
|
comment := d.generateDiffComment(answerStr, resultStr, output)
|
||||||
|
expected := "```diff\n(2 line(s) of common prefix hidden)\n\n\n```\n"
|
||||||
|
assert.Equal(t, expected, comment)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("HideCommonPrefix with only common prefix", func(t *testing.T) {
|
||||||
|
answerStr := "line1\nline2"
|
||||||
|
resultStr := "line1\nline2\nlineA"
|
||||||
|
output := Output{
|
||||||
|
HideCommonPrefix: true,
|
||||||
|
}
|
||||||
|
comment := d.generateDiffComment(answerStr, resultStr, output)
|
||||||
|
expected := "```diff\n(2 line(s) of common prefix hidden)\n\n+ lineA\n```\n"
|
||||||
|
assert.Equal(t, expected, comment)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("MaxDiffLines truncation", func(t *testing.T) {
|
||||||
|
answerStr := "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8"
|
||||||
|
resultStr := "line1\nline2\nline3\nlineA\nlineB\nlineC\nlineD\nlineE"
|
||||||
|
output := Output{
|
||||||
|
MaxDiffLines: 3,
|
||||||
|
HideCommonPrefix: true,
|
||||||
|
}
|
||||||
|
comment := d.generateDiffComment(answerStr, resultStr, output)
|
||||||
|
expected := "```diff\n(3 line(s) of common prefix hidden)\n\n- line4\n- line5\n- line6\n+ lineA\n+ lineB\n+ lineC\n\n(truncated)\n```\n"
|
||||||
|
assert.Equal(t, expected, comment)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user