mirror-to-gitea/vendor/github.com/onsi/gomega/matchers/be_a_regular_file.go
vendor/github.com/onsi/gomega/matchers/be_a_regular_file.goRaw
1// untested sections: 5
2
3package matchers
4
5import (
6 "fmt"
7 "os"
8
9 "github.com/onsi/gomega/format"
10)
11
12type notARegularFileError struct {
13 os.FileInfo
14}
15
16func (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
26type BeARegularFileMatcher struct {
27 expected interface{}
28 err error
29}
30
31func (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
50func (matcher *BeARegularFileMatcher) FailureMessage(actual interface{}) (message string) {
51 return format.Message(actual, fmt.Sprintf("to be a regular file: %s", matcher.err))
52}
53
54func (matcher *BeARegularFileMatcher) NegatedFailureMessage(actual interface{}) (message string) {
55 return format.Message(actual, fmt.Sprintf("not be a regular file"))
56}