github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/util/testhelpers/matchers/match_path.go (about) 1 package matchers 2 3 import ( 4 "fmt" 5 "github.com/onsi/gomega" 6 "path/filepath" 7 "regexp" 8 "strings" 9 ) 10 11 type PathMatcher struct { 12 actualPath string 13 expectedPath string 14 } 15 16 func MatchPath(expectedPath string) gomega.OmegaMatcher { 17 return &PathMatcher{expectedPath: expectedPath} 18 } 19 20 func (matcher *PathMatcher) Match(actual interface{}) (success bool, err error) { 21 actualPath, ok := actual.(string) 22 if !ok { 23 return false, fmt.Errorf("MatchPath: Actual must be a string, got %T", actual) 24 } 25 26 resolvedActualPath, err := resolveSymLinks(actualPath) 27 if err != nil { 28 return false, err 29 } 30 31 resolvedExpectedPath, err := resolveSymLinks(matcher.expectedPath) 32 if err != nil { 33 return false, err 34 } 35 36 matcher.actualPath = resolvedActualPath 37 matcher.expectedPath = resolvedExpectedPath 38 39 return strings.EqualFold(regexp.QuoteMeta(matcher.expectedPath), regexp.QuoteMeta(matcher.actualPath)), nil 40 } 41 42 func (matcher *PathMatcher) FailureMessage(actual interface{}) string { 43 return fmt.Sprintf("Expected:\n %v\n to match actual:\n%v\n", matcher.expectedPath, matcher.actualPath) 44 } 45 46 func (matcher *PathMatcher) NegatedFailureMessage(actual interface{}) string { 47 return fmt.Sprintf("Expected:\n %v\n not to match actual:\n%v\n", matcher.expectedPath, matcher.actualPath) 48 } 49 50 func resolveSymLinks(path string) (string, error) { 51 theRealDir, err := filepath.EvalSymlinks(filepath.Dir(path)) 52 53 if err != nil { 54 return "", err 55 } 56 57 return filepath.Join(theRealDir, filepath.Base(path)), nil 58 }