-
Notifications
You must be signed in to change notification settings - Fork 928
/
match_path.go
58 lines (45 loc) · 1.45 KB
/
match_path.go
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
package matchers
import (
"fmt"
"github.com/onsi/gomega"
"path/filepath"
"regexp"
"strings"
)
type PathMatcher struct {
actualPath string
expectedPath string
}
func MatchPath(expectedPath string) gomega.OmegaMatcher {
return &PathMatcher{expectedPath: expectedPath}
}
func (matcher *PathMatcher) Match(actual interface{}) (success bool, err error) {
actualPath, ok := actual.(string)
if !ok {
return false, fmt.Errorf("MatchPath: Actual must be a string, got %T", actual)
}
resolvedActualPath, err := resolveSymLinks(actualPath)
if err != nil {
return false, err
}
resolvedExpectedPath, err := resolveSymLinks(matcher.expectedPath)
if err != nil {
return false, err
}
matcher.actualPath = resolvedActualPath
matcher.expectedPath = resolvedExpectedPath
return strings.EqualFold(regexp.QuoteMeta(matcher.expectedPath), regexp.QuoteMeta(matcher.actualPath)), nil
}
func (matcher *PathMatcher) FailureMessage(actual interface{}) string {
return fmt.Sprintf("Expected:\n %v\n to match actual:\n%v\n", matcher.expectedPath, matcher.actualPath)
}
func (matcher *PathMatcher) NegatedFailureMessage(actual interface{}) string {
return fmt.Sprintf("Expected:\n %v\n not to match actual:\n%v\n", matcher.expectedPath, matcher.actualPath)
}
func resolveSymLinks(path string) (string, error) {
theRealDir, err := filepath.EvalSymlinks(filepath.Dir(path))
if err != nil {
return "", err
}
return filepath.Join(theRealDir, filepath.Base(path)), nil
}