Skip to content
Snippets Groups Projects
Verified Commit 6a0b826d authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: implement #1

parent b81a2b5f
No related branches found
No related tags found
No related merge requests found
package watch
import (
"fmt"
"github.com/fsnotify/fsnotify"
"path/filepath"
"sync"
......@@ -57,6 +58,11 @@ type Lighthouse interface {
SetOnError(callback EventErrorCallback)
SetDebounce(duration time.Duration)
WatchList() []string
Sync() error
IsInSync() bool
}
// NewLighthouse creates a new lighthouse instance.
......@@ -102,6 +108,67 @@ func (l *lighthouse) checkAndInit() {
}
// WatchList returns a list of all watched paths.
func (l *lighthouse) WatchList() []string {
l.mutex.Lock()
defer l.mutex.Unlock()
var list []string
for k := range l.watchers {
list = append(list, k)
}
return list
}
// IsInSync returns true if all paths are watched by fsnotify.
func (l *lighthouse) IsInSync() bool {
l.mutex.Lock()
defer l.mutex.Unlock()
wl := l.fsnotify.WatchList()
if len(wl) != len(l.watchers) {
return false
}
for _, w := range wl {
if _, ok := l.watchers[w]; !ok {
return false
}
}
return true
}
// Sync Lighthouse and fsnotify. Remove all watchers from fsnotify and add them again.
func (l *lighthouse) Sync() error {
l.mutex.Lock()
defer l.mutex.Unlock()
// remove all watchers from fsnotify
wl := l.fsnotify.WatchList()
for _, w := range wl {
_ = l.fsnotify.Remove(w)
}
// add all watchers to fsnotify
var errReturn error
for k := range l.watchers {
err := l.fsnotify.Add(k)
if err != nil {
if errReturn == nil {
errReturn = err
} else {
errReturn = fmt.Errorf("%w, %v", errReturn, err)
}
}
}
return errReturn
}
// IsRunning returns true if the watcher is active, false otherwise.
func (l *lighthouse) IsRunning() bool {
l.mutex.Lock()
......
......@@ -84,3 +84,82 @@ func TestIsActiveWatched(t *testing.T) {
assert.True(t, l.IsActiveWatched(tempDir))
assert.False(t, l.IsActiveWatched("path/not/watched"))
}
func TestWatchList(t *testing.T) {
tempDir, err := os.MkdirTemp("", "watchtest")
assert.Nil(t, err)
defer func() {
_ = os.RemoveAll(tempDir) // Cleanup
}()
l := NewLighthouse()
w := &Watch{Path: tempDir}
err = l.Add(w)
assert.Nil(t, err)
assert.Equal(t, 1, len(l.WatchList()))
assert.Equal(t, tempDir, l.WatchList()[0])
}
// test isinsync
func TestIsInSync(t *testing.T) {
tempDir, err := os.MkdirTemp("", "watchtest")
assert.Nil(t, err)
tempFile, err := os.CreateTemp(tempDir, "test")
assert.Nil(t, err)
defer func() {
_ = os.RemoveAll(tempDir) // Cleanup
_ = os.RemoveAll(tempFile.Name()) // Cleanup
}()
l := NewLighthouse()
w := &Watch{Path: tempDir}
err = l.Add(w)
assert.Nil(t, err)
w2 := &Watch{Path: tempFile.Name()}
err = l.Add(w2)
assert.Nil(t, err)
err = l.Sync()
assert.Nil(t, err)
assert.True(t, l.IsInSync())
internal := l.(*lighthouse)
internal.fsnotify.Remove(tempFile.Name())
assert.False(t, l.IsInSync())
}
// test Sync
func TestSync(t *testing.T) {
tempDir, err := os.MkdirTemp("", "watchtest")
assert.Nil(t, err)
tempFile, err := os.CreateTemp(tempDir, "test")
assert.Nil(t, err)
defer func() {
_ = os.RemoveAll(tempDir) // Cleanup
_ = os.RemoveAll(tempFile.Name()) // Cleanup
}()
l := NewLighthouse()
w := &Watch{Path: tempDir}
err = l.Add(w)
assert.Nil(t, err)
w2 := &Watch{Path: tempFile.Name()}
err = l.Add(w2)
assert.Nil(t, err)
err = l.Sync()
assert.Nil(t, err)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment