Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | package matchers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | |
| 7 | "github.com/onsi/gomega/format" |
| 8 | ) |
| 9 | |
| 10 | type BeAnExistingFileMatcher struct { |
| 11 | expected interface{} |
| 12 | } |
| 13 | |
| 14 | func (matcher *BeAnExistingFileMatcher) Match(actual interface{}) (success bool, err error) { |
| 15 | actualFilename, ok := actual.(string) |
| 16 | if !ok { |
| 17 | return false, fmt.Errorf("BeAnExistingFileMatcher matcher expects a file path") |
| 18 | } |
| 19 | |
| 20 | if _, err = os.Stat(actualFilename); err != nil { |
| 21 | switch { |
| 22 | case os.IsNotExist(err): |
| 23 | return false, nil |
| 24 | default: |
| 25 | return false, err |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | return true, nil |
| 30 | } |
| 31 | |
| 32 | func (matcher *BeAnExistingFileMatcher) FailureMessage(actual interface{}) (message string) { |
| 33 | return format.Message(actual, fmt.Sprintf("to exist")) |
| 34 | } |
| 35 | |
| 36 | func (matcher *BeAnExistingFileMatcher) NegatedFailureMessage(actual interface{}) (message string) { |
| 37 | return format.Message(actual, fmt.Sprintf("not to exist")) |
| 38 | } |