github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/internal/unionreader/union_reader_test.go (about)

     1  package unionreader
     2  
     3  import (
     4  	"io"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/anchore/syft/syft/file"
    12  )
    13  
    14  func Test_getUnionReader_notUnionReader(t *testing.T) {
    15  	expectedContents := "this is a test"
    16  	reader := io.NopCloser(strings.NewReader(expectedContents))
    17  
    18  	// make certain that the test fixture does not implement the union reader
    19  	_, ok := reader.(UnionReader)
    20  	require.False(t, ok)
    21  
    22  	actual, err := GetUnionReader(reader)
    23  	require.NoError(t, err)
    24  
    25  	_, ok = actual.(UnionReader)
    26  	require.True(t, ok)
    27  
    28  	b, err := io.ReadAll(actual)
    29  	require.NoError(t, err)
    30  
    31  	assert.Equal(t, expectedContents, string(b))
    32  }
    33  
    34  type panickingUnionReader struct{}
    35  
    36  func (p2 *panickingUnionReader) ReadAt(p []byte, off int64) (n int, err error) {
    37  	panic("don't call this in your unit test!")
    38  }
    39  
    40  func (p2 *panickingUnionReader) Seek(offset int64, whence int) (int64, error) {
    41  	panic("don't call this in your unit test!")
    42  }
    43  
    44  func (p2 *panickingUnionReader) Read(p []byte) (n int, err error) {
    45  	panic("don't call this in your unit test!")
    46  }
    47  
    48  func (p2 *panickingUnionReader) Close() error {
    49  	panic("don't call this in your unit test!")
    50  }
    51  
    52  var _ UnionReader = (*panickingUnionReader)(nil)
    53  
    54  func Test_getUnionReader_fileLocationReadCloser(t *testing.T) {
    55  	// panickingUnionReader is a UnionReader
    56  	p := &panickingUnionReader{}
    57  	embedsUnionReader := file.NewLocationReadCloser(file.Location{}, p)
    58  
    59  	// embedded union reader is returned without "ReadAll" invocation
    60  	ur, err := GetUnionReader(embedsUnionReader)
    61  	require.NoError(t, err)
    62  	require.Equal(t, p, ur)
    63  }