github.com/yandex/pandora@v0.5.32/core/coretest/sink.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 ) 14 15 func AssertSinkEqualStdStream(t *testing.T, expectedPtr **os.File, getSink func() core.DataSink) { 16 temp, err := ioutil.TempFile("", "") 17 require.NoError(t, err) 18 19 backup := *expectedPtr 20 defer func() { 21 *expectedPtr = backup 22 }() 23 *expectedPtr = temp 24 const testdata = "abcd" 25 26 wc, err := getSink().OpenSink() 27 require.NoError(t, err) 28 29 _, err = io.WriteString(wc, testdata) 30 require.NoError(t, err) 31 32 err = wc.Close() 33 require.NoError(t, err) 34 35 _, _ = temp.Seek(0, io.SeekStart) 36 data, _ := ioutil.ReadAll(temp) 37 assert.Equal(t, testdata, string(data)) 38 } 39 40 func AssertSinkEqualFile(t *testing.T, fs afero.Fs, filename string, sink core.DataSink) { 41 _ = afero.WriteFile(fs, filename, []byte("should be truncated"), 0644) 42 43 wc, err := sink.OpenSink() 44 require.NoError(t, err) 45 46 const testdata = "abcd" 47 48 _, err = io.WriteString(wc, testdata) 49 require.NoError(t, err) 50 51 err = wc.Close() 52 require.NoError(t, err) 53 54 data, err := afero.ReadFile(fs, filename) 55 require.NoError(t, err) 56 57 assert.Equal(t, testdata, string(data)) 58 }