Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(assert): added AssertDirEmpty and AssertDirNotEmpty. fixes #37 #52

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,3 +708,32 @@ func AssertNoFileExists(t testRunner, file string, msg ...interface{}) {
internal.Fail(t, "A file that !!should not exist!!, does exist.", internal.NewObjectsSingleUnknown(file), msg...)
}
}

// AssertDirEmpty asserts that a directory is empty.
// The test will pass when the directory is empty or does not exist.
//
// Example:
// testza.AssertDirEmpty(t, "FolderName")
func AssertDirEmpty(t testRunner, dir string, msg ...interface{}) {
if test, ok := t.(helper); ok {
test.Helper()
}

if !internal.AssertDirEmptyHelper(t, dir) {
internal.Fail(t, "The directory !!is not!! empty.", internal.NewObjectsSingleNamed("Directory", dir))
}
}

// AssertDirNotEmpty asserts that a directory is not empty
//
// Example:
// testza.AssertDirNotEmpty(t, "FolderName")
func AssertDirNotEmpty(t testRunner, dir string, msg ...interface{}) {
if test, ok := t.(helper); ok {
test.Helper()
}

if internal.AssertDirEmptyHelper(t, dir) {
internal.Fail(t, "The directory !!is!! empty.", internal.NewObjectsSingleNamed("Directory", dir))
}
}
27 changes: 27 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"errors"
"fmt"
"go/types"
"io/ioutil"
"math/rand"
"os"
"reflect"
"regexp"
"testing"
Expand Down Expand Up @@ -1024,3 +1026,28 @@ func TestAssertNoFileExists_fail(t *testing.T) {
})
})
}

func TestAssertDirEmpty(t *testing.T) {
tempdir, _ := ioutil.TempDir("testdata", "temp")
AssertDirEmpty(t, tempdir)
defer os.RemoveAll(tempdir)
}

func TestAssertDirEmpty_fail(t *testing.T) {
AssertTestFails(t, func(t TestingPackageWithFailFunctions) {
AssertDirEmpty(t, "non-existent")
AssertDirEmpty(t, "internal")
})
}

func TestAssertDirNotEmpty(t *testing.T) {
AssertDirNotEmpty(t, "testdata")
}

func TestAssertDirNotEmpty_fail(t *testing.T) {
tempdir, _ := ioutil.TempDir("testdata", "temp")
AssertTestFails(t, func(t TestingPackageWithFailFunctions) {
AssertDirNotEmpty(t, tempdir)
})
defer os.RemoveAll(tempdir)
}
15 changes: 15 additions & 0 deletions internal/assertion_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package internal

import (
"bytes"
"errors"
"fmt"
"io"
"os"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -262,3 +265,15 @@ func AssertRegexpHelper(t testRunner, regex interface{}, txt interface{}, should
}, msg...)
}
}

// AssertDirEmptyHelper checks for io.EOF to determine if directory empty or not
func AssertDirEmptyHelper(t testRunner, dir string) bool {
f, err := os.Open(dir)
if err != nil {
Fail(t, "Error opening directory specified", NewObjectsSingleNamed("dir", dir))
}
defer f.Close()

_, err = f.Readdirnames(1)
return errors.Is(err, io.EOF)
}