mirror of
https://github.com/trufflesecurity/trufflehog.git
synced 2024-11-10 07:04:24 +00:00
[bug] - Refactor newDiff constructor to avoid double initialization of contentWriter (#2742)
* only create the contentWriter once * update test * correclty use mock * remove deprecated pkg
This commit is contained in:
parent
11452e8a57
commit
8ceeb5d5a1
2 changed files with 61 additions and 1 deletions
|
@ -77,11 +77,15 @@ func withCustomContentWriter(cr contentWriter) diffOption {
|
|||
// The contentWriter is used to manage the diff's content, allowing for flexible handling of diff data.
|
||||
// By default, a buffer is used as the contentWriter, but this can be overridden with a custom contentWriter.
|
||||
func newDiff(ctx context.Context, commit *Commit, opts ...diffOption) *Diff {
|
||||
diff := &Diff{Commit: commit, contentWriter: bufferwriter.New(ctx)}
|
||||
diff := &Diff{Commit: commit}
|
||||
for _, opt := range opts {
|
||||
opt(diff)
|
||||
}
|
||||
|
||||
if diff.contentWriter == nil {
|
||||
diff.contentWriter = bufferwriter.New(ctx)
|
||||
}
|
||||
|
||||
return diff
|
||||
}
|
||||
|
||||
|
|
|
@ -2,10 +2,13 @@ package gitparse
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
||||
bufferwriter "github.com/trufflesecurity/trufflehog/v3/pkg/writers/buffer_writer"
|
||||
bufferedfilewriter "github.com/trufflesecurity/trufflehog/v3/pkg/writers/buffered_file_writer"
|
||||
|
@ -2346,3 +2349,56 @@ index 2ee133b..12b4843 100644
|
|||
+output = json
|
||||
+region = us-east-2
|
||||
`
|
||||
|
||||
type mockContentWriter struct{ counter int }
|
||||
|
||||
func newMockContentWriter() *mockContentWriter { return &mockContentWriter{counter: 1} }
|
||||
|
||||
func (m *mockContentWriter) ReadCloser() (io.ReadCloser, error) {
|
||||
return io.NopCloser(bytes.NewReader([]byte{})), nil
|
||||
}
|
||||
|
||||
func (m *mockContentWriter) CloseForWriting() error { return nil }
|
||||
|
||||
func (m *mockContentWriter) Len() int { return 0 }
|
||||
|
||||
func (m *mockContentWriter) String() (string, error) { return "", nil }
|
||||
|
||||
func (m *mockContentWriter) Write(p []byte) (n int, err error) { return len(p), nil }
|
||||
|
||||
func TestNewDiffContentWriterCreation(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
opts []diffOption
|
||||
expectedCount int
|
||||
}{
|
||||
{
|
||||
name: "Without custom contentWriter",
|
||||
expectedCount: 1,
|
||||
},
|
||||
{
|
||||
name: "With custom contentWriter",
|
||||
opts: []diffOption{withCustomContentWriter(newMockContentWriter())},
|
||||
expectedCount: 1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
commit := new(Commit)
|
||||
|
||||
mockWriter := newMockContentWriter()
|
||||
assert.NotNil(t, mockWriter, "Failed to create mockWriter")
|
||||
|
||||
diff := newDiff(ctx, commit, tc.opts...)
|
||||
assert.NotNil(t, diff, "Failed to create diff")
|
||||
assert.NotNil(t, diff.contentWriter, "Failed to create contentWriter")
|
||||
|
||||
assert.Equal(t, tc.expectedCount, mockWriter.counter, "Unexpected number of contentWriter creations")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue