posix: test isDirEmpty, change error conditional (#4743)

This commit adds a new test for isDirEmpty (for code coverage) and
changes around the error conditional. Previously, there was a `return
nil` statement that would only be triggered under a race condition and
would trip up our test coverage for no real reason. With this new error
conditional, there's no awkward 'else'-esque condition, which means test
coverage will not change between runs for no reason in this specific
test. It's also a cleaner read.
master
Brendan Ashworth 7 years ago committed by Dee Koder
parent fcc61fa46a
commit 28bc5899fd
  1. 20
      cmd/posix.go
  2. 36
      cmd/posix_test.go

@ -74,24 +74,20 @@ func checkPathLength(pathName string) error {
func isDirEmpty(dirname string) bool { func isDirEmpty(dirname string) bool {
f, err := os.Open(preparePath(dirname)) f, err := os.Open(preparePath(dirname))
if err != nil { if err != nil {
errorIf(func() error { if !os.IsNotExist(err) {
if !os.IsNotExist(err) { errorIf(err, "Unable to access directory")
return err }
}
return nil
}(), "Unable to access directory.")
return false return false
} }
defer f.Close() defer f.Close()
// List one entry. // List one entry.
_, err = f.Readdirnames(1) _, err = f.Readdirnames(1)
if err != io.EOF { if err != io.EOF {
errorIf(func() error { if !os.IsNotExist(err) {
if !os.IsNotExist(err) { errorIf(err, "Unable to list directory")
return err }
}
return nil
}(), "Unable to list directory.")
return false return false
} }
// Returns true if we have reached EOF, directory is indeed empty. // Returns true if we have reached EOF, directory is indeed empty.

@ -73,6 +73,42 @@ func TestPosixGetDiskInfo(t *testing.T) {
} }
} }
func TestPosixIsDirEmpty(t *testing.T) {
tmp, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatal(err)
}
defer removeAll(tmp)
// Should give false on non-existent directory.
dir1 := slashpath.Join(tmp, "non-existent-directory")
if isDirEmpty(dir1) != false {
t.Error("expected false for non-existent directory, got true")
}
// Should give false for not-a-directory.
dir2 := slashpath.Join(tmp, "file")
err = ioutil.WriteFile(dir2, []byte("hello"), 0777)
if err != nil {
t.Fatal(err)
}
if isDirEmpty(dir2) != false {
t.Error("expected false for a file, got true")
}
// Should give true for a real empty directory.
dir3 := slashpath.Join(tmp, "empty")
err = os.Mkdir(dir3, 0777)
if err != nil {
t.Fatal(err)
}
if isDirEmpty(dir3) != true {
t.Error("expected true for empty dir, got false")
}
}
// TestPosixReadAll - TestPosixs the functionality implemented by posix ReadAll storage API. // TestPosixReadAll - TestPosixs the functionality implemented by posix ReadAll storage API.
func TestPosixReadAll(t *testing.T) { func TestPosixReadAll(t *testing.T) {
// create posix test setup // create posix test setup

Loading…
Cancel
Save