blob: d42eba2234463f7f4114d3fa3b194485327f8e7b [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001package matchers
2
3import (
4 "fmt"
5 "os"
6
7 "github.com/onsi/gomega/format"
8)
9
10type BeAnExistingFileMatcher struct {
11 expected interface{}
12}
13
14func (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
32func (matcher *BeAnExistingFileMatcher) FailureMessage(actual interface{}) (message string) {
33 return format.Message(actual, fmt.Sprintf("to exist"))
34}
35
36func (matcher *BeAnExistingFileMatcher) NegatedFailureMessage(actual interface{}) (message string) {
37 return format.Message(actual, fmt.Sprintf("not to exist"))
38}