trufflehog/pkg/writers/buffer_writer/bufferwriter_test.go
ahrav 4a5fbf8417
[refactor] - Update Write method signature in contentWriter interface (#2721)
* Update write method in contentWriter interface

* fix lint
2024-04-23 08:47:53 -07:00

155 lines
3.2 KiB
Go

package bufferwriter
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
)
func TestBufferWriterWrite(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []byte
initialState state
expectedError bool
expectedSize int
}{
{
name: "Write in writeOnly state with empty input",
input: []byte(""),
initialState: writeOnly,
},
{
name: "Write in writeOnly state with non-empty input",
input: []byte("hello"),
initialState: writeOnly,
expectedError: false,
expectedSize: 5,
},
{
name: "Attempt to write in readOnly state",
input: []byte("hello"),
initialState: readOnly,
expectedError: true,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
writer := New(context.Background())
writer.state = tc.initialState
_, err := writer.Write(tc.input)
if tc.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedSize, writer.Len())
}
})
}
}
func TestBufferWriterReadCloser(t *testing.T) {
t.Parallel()
tests := []struct {
name string
initialState state
expectedError bool
}{
{
name: "Get ReadCloser in writeOnly state",
initialState: writeOnly,
expectedError: true,
},
{
name: "Get ReadCloser in readOnly state",
initialState: readOnly,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
writer := New(context.Background())
writer.state = tc.initialState
rc, err := writer.ReadCloser()
if tc.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotNil(t, rc)
// Test that the ReadCloser can be closed.
err = rc.Close()
assert.NoError(t, err)
}
})
}
}
func TestBufferWriterCloseForWriting(t *testing.T) {
writer := New(context.Background())
err := writer.CloseForWriting()
assert.NoError(t, err)
assert.Equal(t, readOnly, writer.state)
}
func TestBufferWriterString(t *testing.T) {
t.Parallel()
tests := []struct {
name string
prepareBuffer func(*BufferWriter) // Function to prepare the buffer with data or state
expectedStr string
expectedError bool
}{
{
name: "String with no data",
prepareBuffer: func(_ *BufferWriter) {
// No preparation needed, buffer is empty by default
},
expectedStr: "",
expectedError: false,
},
{
name: "String with data",
prepareBuffer: func(bw *BufferWriter) {
_, _ = bw.Write([]byte("test data"))
},
expectedStr: "test data",
expectedError: false,
},
{
name: "Buffer is nil",
prepareBuffer: func(bw *BufferWriter) {
bw.buf = nil
},
expectedStr: "",
expectedError: true,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
writer := New(context.Background())
tc.prepareBuffer(writer)
result, err := writer.String()
if tc.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedStr, result)
}
})
}
}