github.com/yandex/pandora@v0.5.32/core/coretest/source.go (about)

     1  package coretest
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/spf13/afero"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/yandex/pandora/core"
    13  	"github.com/yandex/pandora/lib/testutil"
    14  )
    15  
    16  func AssertSourceEqualStdStream(t *testing.T, expectedPtr **os.File, getSource func() core.DataSource) {
    17  	temp, err := ioutil.TempFile("", "")
    18  	require.NoError(t, err)
    19  
    20  	backup := *expectedPtr
    21  	defer func() {
    22  		*expectedPtr = backup
    23  	}()
    24  	*expectedPtr = temp
    25  	const testdata = "abcd"
    26  	_, err = io.WriteString(temp, testdata)
    27  	require.NoError(t, err)
    28  
    29  	rc, err := getSource().OpenSource()
    30  	require.NoError(t, err)
    31  
    32  	err = rc.Close()
    33  	require.NoError(t, err, "std stream should not be closed")
    34  
    35  	_, _ = temp.Seek(0, io.SeekStart)
    36  	data, _ := ioutil.ReadAll(temp)
    37  	assert.Equal(t, testdata, string(data))
    38  }
    39  
    40  func AssertSourceEqualFile(t *testing.T, fs afero.Fs, filename string, source core.DataSource) {
    41  	const testdata = "abcd"
    42  	_ = afero.WriteFile(fs, filename, []byte(testdata), 0644)
    43  
    44  	rc, err := source.OpenSource()
    45  	require.NoError(t, err)
    46  
    47  	data := testutil.ReadString(t, rc)
    48  	err = rc.Close()
    49  	require.NoError(t, err)
    50  
    51  	assert.Equal(t, testdata, data)
    52  }