github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/test/helpers.go (about)

     1  package test
     2  
     3  import (
     4  	"bytes"
     5  	"regexp"
     6  )
     7  
     8  // TODO copypasta from command package
     9  type CmdOut struct {
    10  	OutBuf     *bytes.Buffer
    11  	ErrBuf     *bytes.Buffer
    12  	BrowsedURL string
    13  }
    14  
    15  func (c CmdOut) String() string {
    16  	return c.OutBuf.String()
    17  }
    18  
    19  func (c CmdOut) Stderr() string {
    20  	return c.ErrBuf.String()
    21  }
    22  
    23  // OutputStub implements a simple utils.Runnable
    24  type OutputStub struct {
    25  	Out   []byte
    26  	Error error
    27  }
    28  
    29  func (s OutputStub) Output() ([]byte, error) {
    30  	if s.Error != nil {
    31  		return s.Out, s.Error
    32  	}
    33  	return s.Out, nil
    34  }
    35  
    36  func (s OutputStub) Run() error {
    37  	if s.Error != nil {
    38  		return s.Error
    39  	}
    40  	return nil
    41  }
    42  
    43  type T interface {
    44  	Helper()
    45  	Errorf(string, ...interface{})
    46  }
    47  
    48  // Deprecated: prefer exact matches for command output
    49  func ExpectLines(t T, output string, lines ...string) {
    50  	t.Helper()
    51  	var r *regexp.Regexp
    52  	for _, l := range lines {
    53  		r = regexp.MustCompile(l)
    54  		if !r.MatchString(output) {
    55  			t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
    56  			return
    57  		}
    58  	}
    59  }