From 6f48be8da333ecec65e1535dbe59650b527df2db Mon Sep 17 00:00:00 2001 From: Marcel Wiederer <14212817+m-rcl@users.noreply.github.com> Date: Thu, 8 Sep 2022 16:40:19 +0200 Subject: [PATCH] feat: customizable directory refresher (#45) This PR is intended to allow users to define the filesystem operations they want to be notified about (for example deletion). --- loader/directory_refresher.go | 24 +++++++++++++++----- loader/loader_test.go | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/loader/directory_refresher.go b/loader/directory_refresher.go index 7e17935..d5c45d5 100644 --- a/loader/directory_refresher.go +++ b/loader/directory_refresher.go @@ -3,7 +3,14 @@ package loader import "path/filepath" type DirectoryRefresher struct { - currDir string + currDir string + watchOps map[FileSystemOp]bool +} + +var defaultFileSystemOps = map[FileSystemOp]bool{ + Write: true, + Create: true, + Chmod: true, } func (d *DirectoryRefresher) WatchDirectory(runtimePath string, appDirPath string) string { @@ -11,10 +18,17 @@ func (d *DirectoryRefresher) WatchDirectory(runtimePath string, appDirPath strin return d.currDir } +func (d *DirectoryRefresher) WatchFileSystemOps(fsops ...FileSystemOp) { + d.watchOps = map[FileSystemOp]bool{} + for _, op := range fsops { + d.watchOps[op] = true + } +} + func (d *DirectoryRefresher) ShouldRefresh(path string, op FileSystemOp) bool { - if filepath.Dir(path) == d.currDir && - (op == Write || op == Create || op == Chmod) { - return true + watchOps := d.watchOps + if watchOps == nil { + watchOps = defaultFileSystemOps } - return false + return filepath.Dir(path) == d.currDir && watchOps[op] } diff --git a/loader/loader_test.go b/loader/loader_test.go index afd365b..dab7460 100644 --- a/loader/loader_test.go +++ b/loader/loader_test.go @@ -334,6 +334,47 @@ func TestOnRuntimeChanged(t *testing.T) { }) } +func TestShouldRefreshDefault(t *testing.T) { + assert := require.New(t) + + refresher := DirectoryRefresher{currDir: "/tmp"} + + assert.True(refresher.ShouldRefresh("/tmp/foo", Write)) + + assert.False(refresher.ShouldRefresh("/tmp/foo", Remove)) + assert.False(refresher.ShouldRefresh("/bar/foo", Write)) + assert.False(refresher.ShouldRefresh("/bar/foo", Remove)) +} + +func TestShouldRefreshRemove(t *testing.T) { + assert := require.New(t) + + refresher := DirectoryRefresher{currDir: "/tmp", watchOps: map[FileSystemOp]bool{ + Remove: true, + Chmod: true, + }} + + assert.True(refresher.ShouldRefresh("/tmp/foo", Remove)) + assert.True(refresher.ShouldRefresh("/tmp/foo", Chmod)) + + assert.False(refresher.ShouldRefresh("/bar/foo", Write)) +} + +func TestWatchFileSystemOps(t *testing.T) { + assert := require.New(t) + + refresher := DirectoryRefresher{currDir: "/tmp"} + + refresher.WatchFileSystemOps() + assert.False(refresher.ShouldRefresh("/tmp/foo", Write)) + + refresher.WatchFileSystemOps(Remove) + assert.True(refresher.ShouldRefresh("/tmp/foo", Remove)) + + refresher.WatchFileSystemOps(Chmod, Write) + assert.True(refresher.ShouldRefresh("/tmp/foo", Write)) +} + func BenchmarkSnapshot(b *testing.B) { var ll Loader for i := 0; i < b.N; i++ {