github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/readers/noclose_test.go (about)

     1  package readers
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var errRead = errors.New("read error")
    12  
    13  type readOnly struct{}
    14  
    15  func (readOnly) Read(p []byte) (n int, err error) {
    16  	return 0, io.EOF
    17  }
    18  
    19  type readClose struct{}
    20  
    21  func (readClose) Read(p []byte) (n int, err error) {
    22  	return 0, errRead
    23  }
    24  
    25  func (readClose) Close() (err error) {
    26  	return io.EOF
    27  }
    28  
    29  func TestNoCloser(t *testing.T) {
    30  	assert.Equal(t, nil, NoCloser(nil))
    31  
    32  	ro := readOnly{}
    33  	assert.Equal(t, ro, NoCloser(ro))
    34  
    35  	rc := readClose{}
    36  	nc := NoCloser(rc)
    37  	assert.NotEqual(t, nc, rc)
    38  
    39  	_, hasClose := nc.(io.Closer)
    40  	assert.False(t, hasClose)
    41  
    42  	_, err := nc.Read(nil)
    43  	assert.Equal(t, errRead, err)
    44  }