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