/
path_matchers.go
65 lines (56 loc) · 1.8 KB
/
path_matchers.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
59
60
61
62
63
64
65
package assert
import (
"fmt"
"path/filepath"
"runtime"
)
const windows = runtime.GOOS == "windows"
// MatchPath is a GomegaMatcher for filepaths, on Unix systems paths are
// compared unmodified. On Windows, Unix absolute paths (leading '/') are
// converted to Windows absolute paths using the current working directory
// for the volume name.
type MatchPath string
func isSlash(c byte) bool { return c == '\\' || c == '/' }
func (m MatchPath) isAbs(path string) bool {
return filepath.IsAbs(path) || (windows && len(path) != 0 && isSlash(path[0]))
}
func (m MatchPath) cleanPath(s string) string {
if !windows || !m.isAbs(s) {
return s
}
a, err := filepath.Abs(s)
if err != nil {
return s
}
return a
}
func (m MatchPath) Match(actual interface{}) (bool, error) {
path, ok := actual.(string)
if !ok {
return false, fmt.Errorf("MatchPath: expects a string got: %T", actual)
}
if path == string(m) || (filepath.Clean(path) == filepath.Clean(string(m))) {
return true, nil
}
return m.cleanPath(path) == m.cleanPath(string(m)), nil
}
func (m MatchPath) FailureMessage(actual interface{}) string {
if windows {
// show both the provided and cleaned paths
if s, ok := actual.(string); ok {
return fmt.Sprintf("Expected\n\t%v\n\t%v (clean)\nto match file\n\t%v\n\t%v (clean)",
actual, m.cleanPath(s), m, m.cleanPath(string(m)))
}
}
return fmt.Sprintf("Expected\n\t%v\nto match file\n\t%v", actual, m)
}
func (m MatchPath) NegatedFailureMessage(actual interface{}) string {
if windows {
// show both the provided and cleaned paths
if s, ok := actual.(string); ok {
return fmt.Sprintf("Expected\n\t%v\n\t%v (clean)\nnot to match file\n\t%v\n\t%v (clean)",
actual, m.cleanPath(s), m, m.cleanPath(string(m)))
}
}
return fmt.Sprintf("Expected\n\t%v\nnot to match file\n\t%v", actual, m)
}