Skip to content

Commit

Permalink
chore: docstring test writer
Browse files Browse the repository at this point in the history
Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
  • Loading branch information
Benehiko committed Feb 27, 2024
1 parent 135075d commit 4062944
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/test/writer.go
Expand Up @@ -4,19 +4,24 @@ import (
"io"
)

// WriterWithHook is an io.Writer that calls a hook function after every write.
// This is useful for testing.
type WriterWithHook struct {
actualWriter io.Writer
hook func()
}

// Write implements io.Writer.
// Write writes p to the actual writer and then calls the hook function.
func (w *WriterWithHook) Write(p []byte) (n int, err error) {
defer w.hook()
return w.actualWriter.Write(p)
}

var _ io.Writer = (*WriterWithHook)(nil)

// NewWriterWithHook returns a new WriterWithHook that still writes to the actualWriter
// but also calls the hook function after every write.
// The hook function is useful for testing, or waiting for a write to complete.
func NewWriterWithHook(actualWriter io.Writer, hook func()) *WriterWithHook {
return &WriterWithHook{actualWriter: actualWriter, hook: hook}
}

0 comments on commit 4062944

Please sign in to comment.