github.com/onsi/gomega@v1.32.0/matchers/be_a_directory.go (about) 1 // untested sections: 5 2 3 package matchers 4 5 import ( 6 "fmt" 7 "os" 8 9 "github.com/onsi/gomega/format" 10 ) 11 12 type notADirectoryError struct { 13 os.FileInfo 14 } 15 16 func (t notADirectoryError) Error() string { 17 fileInfo := os.FileInfo(t) 18 switch { 19 case fileInfo.Mode().IsRegular(): 20 return "file is a regular file" 21 default: 22 return fmt.Sprintf("file mode is: %s", fileInfo.Mode().String()) 23 } 24 } 25 26 type BeADirectoryMatcher struct { 27 expected interface{} 28 err error 29 } 30 31 func (matcher *BeADirectoryMatcher) Match(actual interface{}) (success bool, err error) { 32 actualFilename, ok := actual.(string) 33 if !ok { 34 return false, fmt.Errorf("BeADirectoryMatcher 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().IsDir() { 44 matcher.err = notADirectoryError{fileInfo} 45 return false, nil 46 } 47 return true, nil 48 } 49 50 func (matcher *BeADirectoryMatcher) FailureMessage(actual interface{}) (message string) { 51 return format.Message(actual, fmt.Sprintf("to be a directory: %s", matcher.err)) 52 } 53 54 func (matcher *BeADirectoryMatcher) NegatedFailureMessage(actual interface{}) (message string) { 55 return format.Message(actual, "not be a directory") 56 }