github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/cf/util/testhelpers/matchers/have_command_in_category.go (about)

     1  package matchers
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/onsi/gomega/gexec"
    10  	"github.com/onsi/gomega/types"
    11  )
    12  
    13  type CommandInCategoryMatcher struct {
    14  	command     string
    15  	category    string
    16  	description string
    17  
    18  	actualDescription string
    19  	actualCategory    string
    20  }
    21  
    22  func HaveCommandInCategoryWithDescription(command, category, description string) types.GomegaMatcher {
    23  	return &CommandInCategoryMatcher{
    24  		command:     command,
    25  		category:    category,
    26  		description: description,
    27  	}
    28  }
    29  
    30  func getDescriptionAndCategory(out, command string) (string, string) {
    31  	lines := strings.Split(out, "\n")
    32  	description := ""
    33  	category := ""
    34  	index := -1
    35  
    36  	for i, line := range lines {
    37  		r := regexp.MustCompile(fmt.Sprintf(`^\s+%s\s+(.*)$`, command))
    38  		matches := r.FindAllStringSubmatch(line, 1)
    39  		if matches != nil {
    40  			description = matches[0][1]
    41  			index = i
    42  			break
    43  		}
    44  	}
    45  
    46  	for i := index - 1; i >= 0; i-- {
    47  		r := regexp.MustCompile(`^(.+):$`)
    48  		matches := r.FindAllStringSubmatch(lines[i], 1)
    49  		if matches != nil {
    50  			category = matches[0][1]
    51  			break
    52  		}
    53  	}
    54  
    55  	return description, category
    56  }
    57  
    58  func (matcher *CommandInCategoryMatcher) Match(actual interface{}) (success bool, err error) {
    59  	var session *gexec.Session
    60  	var out string
    61  	var ok bool
    62  	if session, ok = actual.(*gexec.Session); !ok {
    63  		return false, errors.New("HaveCommandInCategory: Actual value must be a *gexec.Session.")
    64  	}
    65  	out = string(session.Out.Contents())
    66  
    67  	matcher.actualDescription, matcher.actualCategory = getDescriptionAndCategory(out, matcher.command)
    68  	if matcher.actualDescription == "" && matcher.actualCategory == "" {
    69  		return false, fmt.Errorf("HaveCommandInCategory: output:\n%s\ndoes not contain command `%s`\n", out, matcher.command)
    70  	}
    71  
    72  	return matcher.actualDescription == matcher.description && matcher.actualCategory == matcher.category, nil
    73  }
    74  
    75  func (matcher *CommandInCategoryMatcher) FailureMessage(actual interface{}) string {
    76  	if matcher.actualDescription != matcher.description {
    77  		return fmt.Sprintf("Expected command `%s` to have description:\n %s\nbut found:\n %s\n", matcher.command, matcher.description, matcher.actualDescription)
    78  	}
    79  
    80  	return fmt.Sprintf("Expected command `%s` to be in category `%s` but found it in `%s`\n", matcher.command, matcher.category, matcher.actualCategory)
    81  }
    82  
    83  func (matcher *CommandInCategoryMatcher) NegatedFailureMessage(actual interface{}) string {
    84  	panic("Not implemented. Are you sure you want to negate this test?")
    85  }