1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
package test_helpers
import (
"io"
"sync"
)
type FakeOutputInterceptor struct {
intercepting bool
forwardingWriter io.Writer
interceptedOutput string
lock *sync.Mutex
}
func NewFakeOutputInterceptor() *FakeOutputInterceptor {
return &FakeOutputInterceptor{
lock: &sync.Mutex{},
forwardingWriter: io.Discard,
}
}
func (interceptor *FakeOutputInterceptor) AppendInterceptedOutput(s string) {
interceptor.lock.Lock()
defer interceptor.lock.Unlock()
interceptor.interceptedOutput += s
interceptor.forwardingWriter.Write([]byte(s))
}
func (interceptor *FakeOutputInterceptor) StartInterceptingOutput() {
interceptor.StartInterceptingOutputAndForwardTo(io.Discard)
}
func (interceptor *FakeOutputInterceptor) StartInterceptingOutputAndForwardTo(w io.Writer) {
interceptor.lock.Lock()
defer interceptor.lock.Unlock()
interceptor.forwardingWriter = w
interceptor.intercepting = true
interceptor.interceptedOutput = ""
}
func (interceptor *FakeOutputInterceptor) PauseIntercepting() {
interceptor.lock.Lock()
defer interceptor.lock.Unlock()
interceptor.intercepting = false
}
func (interceptor *FakeOutputInterceptor) ResumeIntercepting() {
interceptor.lock.Lock()
defer interceptor.lock.Unlock()
interceptor.intercepting = true
}
func (interceptor *FakeOutputInterceptor) StopInterceptingAndReturnOutput() string {
interceptor.lock.Lock()
defer interceptor.lock.Unlock()
interceptor.intercepting = false
return interceptor.interceptedOutput
}
func (interceptor *FakeOutputInterceptor) Shutdown() {
}
|