memoryfs
memoryfs copied to clipboard
feat: add TimeProvider
Thanks for a great library.
In my use case, I needed to inject an any value into ModTime, so I made the following modifications.
The default value is set to time.Now(), so backward compatibility is not broken.
https://github.com/yktakaha4/memoryfs/commit/2fcb18eec857fff8c482fb028826a35135372999
// SetTimeProvider set function to mock generation of current time (default: time.Now())
func (m *FS) SetTimeProvider(provider func() time.Time) {
m.context.provideTime = provider
}
// Usage
mfs := New()
t1 := time.Date(2112, 9, 3, 12, 34, 56, 321, time.UTC)
mfs.SetTimeProvider(func() time.Time {
return t1
})
err = mfs.WriteFile("test.txt", []byte("content"), 0o644)
assert.NoError(t, err)
stat, err = mfs.Stat("test.txt")
assert.NoError(t, err)
assert.Equal(t, t1, stat.ModTime())
Would this change be appropriate for this library? If no problem, I would like to create a pull request.