github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/test_helpers/fake_output_interceptor.go (about)

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