...
1
2
3 package matchers
4
5 import (
6 "fmt"
7 "os"
8
9 "github.com/onsi/gomega/format"
10 )
11
12 type notARegularFileError struct {
13 os.FileInfo
14 }
15
16 func (t notARegularFileError) Error() string {
17 fileInfo := os.FileInfo(t)
18 switch {
19 case fileInfo.IsDir():
20 return "file is a directory"
21 default:
22 return fmt.Sprintf("file mode is: %s", fileInfo.Mode().String())
23 }
24 }
25
26 type BeARegularFileMatcher struct {
27 expected interface{}
28 err error
29 }
30
31 func (matcher *BeARegularFileMatcher) Match(actual interface{}) (success bool, err error) {
32 actualFilename, ok := actual.(string)
33 if !ok {
34 return false, fmt.Errorf("BeARegularFileMatcher matcher expects a file path")
35 }
36
37 fileInfo, err := os.Stat(actualFilename)
38 if err != nil {
39 matcher.err = err
40 return false, nil
41 }
42
43 if !fileInfo.Mode().IsRegular() {
44 matcher.err = notARegularFileError{fileInfo}
45 return false, nil
46 }
47 return true, nil
48 }
49
50 func (matcher *BeARegularFileMatcher) FailureMessage(actual interface{}) (message string) {
51 return format.Message(actual, fmt.Sprintf("to be a regular file: %s", matcher.err))
52 }
53
54 func (matcher *BeARegularFileMatcher) NegatedFailureMessage(actual interface{}) (message string) {
55 return format.Message(actual, "not be a regular file")
56 }
57
View as plain text