github.1git.de/docker/cli@v26.1.3+incompatible/internal/test/writer.go (about)

     1  package test
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  type writerWithHook struct {
     8  	actualWriter io.Writer
     9  	hook         func([]byte)
    10  }
    11  
    12  func (w *writerWithHook) Write(p []byte) (n int, err error) {
    13  	defer w.hook(p)
    14  	return w.actualWriter.Write(p)
    15  }
    16  
    17  var _ io.Writer = (*writerWithHook)(nil)
    18  
    19  // NewWriterWithHook returns a io.Writer that still
    20  // writes to the actualWriter but also calls the hook function
    21  // after every write. It is useful to use this function when
    22  // you need to wait for a writer to complete writing inside a test.
    23  func NewWriterWithHook(actualWriter io.Writer, hook func([]byte)) *writerWithHook {
    24  	return &writerWithHook{actualWriter: actualWriter, hook: hook}
    25  }