Skip to content

feat: add TimeProvider #22

@yktakaha4

Description

@yktakaha4

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.
yktakaha4@2fcb18e

// 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions