directory matcher string fixes
diff --git a/matchers.go b/matchers.go index e594db8..3b39677 100644 --- a/matchers.go +++ b/matchers.go
@@ -337,3 +337,9 @@ func BeARegularFile() types.GomegaMatcher { return &matchers.BeARegularFileMatcher{} } + +//BeADirectory succeeds iff a file exists and is a directory. +//Actual must be a string representing the abs path to the file being checked. +func BeADirectory() types.GomegaMatcher { + return &matchers.BeADirectoryMatcher{} +}
diff --git a/matchers/be_a_directory.go b/matchers/be_a_directory.go new file mode 100644 index 0000000..7b6975e --- /dev/null +++ b/matchers/be_a_directory.go
@@ -0,0 +1,54 @@ +package matchers + +import ( + "fmt" + "os" + + "github.com/onsi/gomega/format" +) + +type notADirectoryError struct { + os.FileInfo +} + +func (t notADirectoryError) Error() string { + fileInfo := os.FileInfo(t) + switch { + case fileInfo.Mode().IsRegular(): + return "file is a regular file" + default: + return fmt.Sprintf("file mode is: %s", fileInfo.Mode().String()) + } +} + +type BeADirectoryMatcher struct { + expected interface{} + err error +} + +func (matcher *BeADirectoryMatcher) Match(actual interface{}) (success bool, err error) { + actualFilename, ok := actual.(string) + if !ok { + return false, fmt.Errorf("BeADirectoryMatcher matcher expects a file path") + } + + fileInfo, err := os.Stat(actualFilename) + if err != nil { + matcher.err = err + return false, nil + } + + if !fileInfo.Mode().IsDir() { + matcher.err = notADirectoryError{fileInfo} + return false, nil + } + return true, nil +} + +func (matcher *BeADirectoryMatcher) FailureMessage(actual interface{}) (message string) { + return format.Message(actual, fmt.Sprintf("to be a directory: %s", matcher.err)) +} + +func (matcher *BeADirectoryMatcher) NegatedFailureMessage(actual interface{}) (message string) { + return format.Message(actual, fmt.Sprintf("not be a directory")) +}
diff --git a/matchers/be_a_directory_test.go b/matchers/be_a_directory_test.go new file mode 100644 index 0000000..e59d769 --- /dev/null +++ b/matchers/be_a_directory_test.go
@@ -0,0 +1,40 @@ +package matchers_test + +import ( + "io/ioutil" + "os" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + . "github.com/onsi/gomega/matchers" +) + +var _ = Describe("BeADirectoryMatcher", func() { + Context("when passed a string", func() { + It("should do the right thing", func() { + Ω("/dne/test").ShouldNot(BeADirectory()) + + tmpFile, err := ioutil.TempFile("", "gomega-test-tempfile") + Ω(err).ShouldNot(HaveOccurred()) + defer os.Remove(tmpFile.Name()) + Ω(tmpFile.Name()).ShouldNot(BeADirectory()) + + tmpDir, err := ioutil.TempDir("", "gomega-test-tempdir") + Ω(err).ShouldNot(HaveOccurred()) + defer os.Remove(tmpDir) + Ω(tmpDir).Should(BeADirectory()) + }) + }) + + Context("when passed something else", func() { + It("should error", func() { + success, err := (&BeADirectoryMatcher{}).Match(nil) + Ω(success).Should(BeFalse()) + Ω(err).Should(HaveOccurred()) + + success, err = (&BeADirectoryMatcher{}).Match(true) + Ω(success).Should(BeFalse()) + Ω(err).Should(HaveOccurred()) + }) + }) +})
diff --git a/matchers/be_a_regular_file.go b/matchers/be_a_regular_file.go index 7a58928..e239131 100644 --- a/matchers/be_a_regular_file.go +++ b/matchers/be_a_regular_file.go
@@ -29,7 +29,7 @@ func (matcher *BeARegularFileMatcher) Match(actual interface{}) (success bool, err error) { actualFilename, ok := actual.(string) if !ok { - return false, fmt.Errorf("FileExistsMatcher matcher expects a file path") + return false, fmt.Errorf("BeARegularFileMatcher matcher expects a file path") } fileInfo, err := os.Stat(actualFilename)
diff --git a/matchers/be_an_existing_file.go b/matchers/be_an_existing_file.go index 30a350e..d42eba2 100644 --- a/matchers/be_an_existing_file.go +++ b/matchers/be_an_existing_file.go
@@ -14,7 +14,7 @@ func (matcher *BeAnExistingFileMatcher) Match(actual interface{}) (success bool, err error) { actualFilename, ok := actual.(string) if !ok { - return false, fmt.Errorf("FileExistsMatcher matcher expects a file path") + return false, fmt.Errorf("BeAnExistingFileMatcher matcher expects a file path") } if _, err = os.Stat(actualFilename); err != nil {