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