Fixed several mtime assertions.
parent
a6dd3d2147
commit
d6e247cc46
|
@ -28,28 +28,32 @@ import (
|
|||
// also that it matches.
|
||||
func MtimeIs(expected time.Time) oglematchers.Matcher {
|
||||
return oglematchers.NewMatcher(
|
||||
func(c interface{}) error { return mtimeIs(c, expected) },
|
||||
func(c interface{}) error { return mtimeIsWithin(c, expected, 0) },
|
||||
fmt.Sprintf("mtime is %v", expected))
|
||||
}
|
||||
|
||||
func mtimeIs(c interface{}, expected time.Time) error {
|
||||
// Like MtimeIs, but allows for a tolerance.
|
||||
func MtimeIsWithin(expected time.Time, d time.Duration) oglematchers.Matcher {
|
||||
return oglematchers.NewMatcher(
|
||||
func(c interface{}) error { return mtimeIsWithin(c, expected, d) },
|
||||
fmt.Sprintf("mtime is within %v of %v", d, expected))
|
||||
}
|
||||
|
||||
func mtimeIsWithin(c interface{}, expected time.Time, d time.Duration) error {
|
||||
fi, ok := c.(os.FileInfo)
|
||||
if !ok {
|
||||
return fmt.Errorf("which is of type %v", reflect.TypeOf(c))
|
||||
}
|
||||
|
||||
// Check ModTime().
|
||||
if fi.ModTime() != expected {
|
||||
d := fi.ModTime().Sub(expected)
|
||||
return fmt.Errorf("which has mtime %v, off by %v", fi.ModTime(), d)
|
||||
diff := fi.ModTime().Sub(expected)
|
||||
absDiff := diff
|
||||
if absDiff < 0 {
|
||||
absDiff = -absDiff
|
||||
}
|
||||
|
||||
// Check Sys().
|
||||
if sysMtime, ok := extractMtime(fi.Sys()); ok {
|
||||
if sysMtime != expected {
|
||||
d := sysMtime.Sub(expected)
|
||||
return fmt.Errorf("which has Sys() mtime %v, off by %v", sysMtime, d)
|
||||
}
|
||||
if !(absDiff < d) {
|
||||
return fmt.Errorf("which has mtime %v, off by %v", fi.ModTime(), diff)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -35,6 +35,9 @@ import (
|
|||
|
||||
func TestMemFS(t *testing.T) { RunTests(t) }
|
||||
|
||||
// TODO(jacobsa): Comments.
|
||||
const timeSlop = 5 * time.Millisecond
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Helpers
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
@ -125,7 +128,7 @@ func (t *MemFSTest) Mkdir_OneLevel() {
|
|||
ExpectEq("dir", fi.Name())
|
||||
ExpectEq(0, fi.Size())
|
||||
ExpectEq(os.ModeDir|applyUmask(0754), fi.Mode())
|
||||
ExpectThat(fi, fusetesting.MtimeIs(createTime))
|
||||
ExpectThat(fi, fusetesting.MtimeIsWithin(createTime, timeSlop))
|
||||
ExpectThat(fi, fusetesting.BirthtimeIs(createTime))
|
||||
ExpectTrue(fi.IsDir())
|
||||
|
||||
|
@ -181,7 +184,7 @@ func (t *MemFSTest) Mkdir_TwoLevels() {
|
|||
ExpectEq("dir", fi.Name())
|
||||
ExpectEq(0, fi.Size())
|
||||
ExpectEq(os.ModeDir|applyUmask(0754), fi.Mode())
|
||||
ExpectThat(fi, fusetesting.MtimeIs(createTime))
|
||||
ExpectThat(fi, fusetesting.MtimeIsWithin(createTime, timeSlop))
|
||||
ExpectThat(fi, fusetesting.BirthtimeIs(createTime))
|
||||
ExpectTrue(fi.IsDir())
|
||||
|
||||
|
@ -290,7 +293,7 @@ func (t *MemFSTest) CreateNewFile_InRoot() {
|
|||
ExpectEq("foo", fi.Name())
|
||||
ExpectEq(len(contents), fi.Size())
|
||||
ExpectEq(applyUmask(0400), fi.Mode())
|
||||
ExpectThat(fi, fusetesting.MtimeIs(createTime))
|
||||
ExpectThat(fi, fusetesting.MtimeIsWithin(createTime, timeSlop))
|
||||
ExpectThat(fi, fusetesting.BirthtimeIs(createTime))
|
||||
ExpectFalse(fi.IsDir())
|
||||
|
||||
|
@ -332,7 +335,7 @@ func (t *MemFSTest) CreateNewFile_InSubDir() {
|
|||
ExpectEq("foo", fi.Name())
|
||||
ExpectEq(len(contents), fi.Size())
|
||||
ExpectEq(applyUmask(0400), fi.Mode())
|
||||
ExpectThat(fi, fusetesting.MtimeIs(createTime))
|
||||
ExpectThat(fi, fusetesting.MtimeIsWithin(createTime, timeSlop))
|
||||
ExpectThat(fi, fusetesting.BirthtimeIs(createTime))
|
||||
ExpectFalse(fi.IsDir())
|
||||
|
||||
|
@ -379,7 +382,7 @@ func (t *MemFSTest) ModifyExistingFile_InRoot() {
|
|||
ExpectEq("foo", fi.Name())
|
||||
ExpectEq(len("Hello, world!"), fi.Size())
|
||||
ExpectEq(applyUmask(0600), fi.Mode())
|
||||
ExpectThat(fi, fusetesting.MtimeIs(modifyTime))
|
||||
ExpectThat(fi, fusetesting.MtimeIsWithin(modifyTime, timeSlop))
|
||||
ExpectThat(fi, fusetesting.BirthtimeIs(createTime))
|
||||
ExpectFalse(fi.IsDir())
|
||||
|
||||
|
@ -431,7 +434,7 @@ func (t *MemFSTest) ModifyExistingFile_InSubDir() {
|
|||
ExpectEq("foo", fi.Name())
|
||||
ExpectEq(len("Hello, world!"), fi.Size())
|
||||
ExpectEq(applyUmask(0600), fi.Mode())
|
||||
ExpectThat(fi, fusetesting.MtimeIs(modifyTime))
|
||||
ExpectThat(fi, fusetesting.MtimeIsWithin(modifyTime, timeSlop))
|
||||
ExpectThat(fi, fusetesting.BirthtimeIs(createTime))
|
||||
ExpectFalse(fi.IsDir())
|
||||
|
||||
|
|
Loading…
Reference in New Issue