github.com/creativeprojects/go-selfupdate@v1.2.0/reader_test.go (about)

     1  package selfupdate
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  )
     7  
     8  var (
     9  	errTestRead = errors.New("read error")
    10  )
    11  
    12  // errorReader is a reader that will throw an error after reading n characters
    13  type errorReader struct {
    14  	r         io.Reader
    15  	failAfter int
    16  }
    17  
    18  // newErrorReader creates a new reader that will thrown an error after reading n characters
    19  func newErrorReader(r io.Reader, failAfterBytes int) *errorReader {
    20  	return &errorReader{
    21  		r:         r,
    22  		failAfter: failAfterBytes,
    23  	}
    24  }
    25  
    26  // Read will throw an error after reading n characters
    27  func (r *errorReader) Read(p []byte) (int, error) {
    28  	if len(p) <= r.failAfter {
    29  		return r.Read(p)
    30  	}
    31  	read, _ := r.r.Read(p[0 : r.failAfter-1])
    32  	return read, errTestRead
    33  }
    34  
    35  // Verify interface
    36  var _ io.Reader = &errorReader{}