From e283dcf1bc0043751713e8d7b54bf9ed4c59c3b9 Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Mon, 16 Mar 2015 12:38:17 +1100 Subject: [PATCH] Added an NlinkIs matcher. --- fusetesting/stat.go | 22 ++++++++++++++++++++++ fusetesting/stat_darwin.go | 6 ++++++ 2 files changed, 28 insertions(+) diff --git a/fusetesting/stat.go b/fusetesting/stat.go index 8077624..c084303 100644 --- a/fusetesting/stat.go +++ b/fusetesting/stat.go @@ -83,3 +83,25 @@ func birthtimeIs(c interface{}, expected time.Time) error { return nil } + +// Match os.FileInfo values that specify a number of links equal to the given +// number. On platforms where there is no nlink field available, match all +// os.FileInfo values. +func NlinkIs(expected uint64) oglematchers.Matcher { + return oglematchers.NewMatcher( + func(c interface{}) error { return nlinkIs(c, expected) }, + fmt.Sprintf("nlink is %v", expected)) +} + +func nlinkIs(c interface{}, expected uint64) error { + fi, ok := c.(os.FileInfo) + if !ok { + return fmt.Errorf("which is of type %v", reflect.TypeOf(c)) + } + + if actual, ok := extractNlink(fi.Sys()); ok && actual != expected { + return fmt.Errorf("which has nlink == %v", actual) + } + + return nil +} diff --git a/fusetesting/stat_darwin.go b/fusetesting/stat_darwin.go index 2b84caf..9b4ebf5 100644 --- a/fusetesting/stat_darwin.go +++ b/fusetesting/stat_darwin.go @@ -30,3 +30,9 @@ func extractBirthtime(sys interface{}) (birthtime time.Time, ok bool) { ok = true return } + +func extractNlink(sys interface{}) (nlink uint64, ok bool) { + nlink = uint64(sys.(*syscall.Stat_t).Nlink) + ok = true + return +}