Skip to content

Commit

Permalink
test file watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaschain committed Dec 3, 2023
1 parent f9e0477 commit c57852a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
5 changes: 3 additions & 2 deletions infrastructure/file_watcher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package infrastructure

import (
"context"
"log"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -28,7 +29,7 @@ func loop(watcher *fsnotify.Watcher, callback core.ChangeCallback) {
}
}

func FileWatcher(paths []string, callback core.ChangeCallback) {
func FileWatcher(paths []string, callback core.ChangeCallback, ctx context.Context) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
callback(nil, &err)
Expand All @@ -46,5 +47,5 @@ func FileWatcher(paths []string, callback core.ChangeCallback) {
}
}

<-make(chan struct{})
<-ctx.Done()
}
37 changes: 37 additions & 0 deletions infrastructure/file_watcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package infrastructure_test

import (
"context"
"os"
"testing"

"github.com/lucaschain/beholder/core"
"github.com/lucaschain/beholder/infrastructure"
"github.com/stretchr/testify/assert"
)

func TestFileWatcher(t *testing.T) {
t.Run("should call callback when file is created", func(t *testing.T) {
testFilePath := "/tmp/beholder"
testFileName := "test.txt"
testFile := testFilePath + "/" + testFileName
ctx, cancel := context.WithCancel(context.Background())
var fileName string
callback := func(event *core.ChangeEvent, err *error) {
fileName = event.FileName
cancel()
}
os.MkdirAll(testFilePath, 0755)
os.Create(testFilePath + "/" + testFileName)
defer os.RemoveAll(testFilePath)
paths := []string{testFilePath}

go func() {
os.WriteFile(testFile, []byte("test"), 0644)
}()

infrastructure.FileWatcher(paths, callback, ctx)

assert.Equal(t, testFile, fileName)
})
}
5 changes: 3 additions & 2 deletions use_case/watch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package use_case

import (
"context"
"fmt"
"log"

Expand All @@ -15,7 +16,7 @@ type WatchConfig struct {
AllowFailing bool
}

type FileWatcher func(paths []string, callback core.ChangeCallback)
type FileWatcher func(paths []string, callback core.ChangeCallback, ctx context.Context)
type CommandRunner func(command []string) error

func onFileChange(c WatchConfig, commandRunner CommandRunner) core.ChangeCallback {
Expand All @@ -42,5 +43,5 @@ func Watch(
) {
fmt.Printf("Watching path: %s and running command: '%s'\n", c.Paths, c.Command)
callback := onFileChange(c, commandRunner)
fileWatcher(c.Paths, callback)
fileWatcher(c.Paths, callback, context.Background())
}
3 changes: 2 additions & 1 deletion use_case/watch_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package use_case_test

import (
"context"
"testing"

"github.com/lucaschain/beholder/core"
Expand Down Expand Up @@ -69,7 +70,7 @@ func buildFakeFileWatcher(filename string, eventType event_types.EventType) use_
Type: event_types.Create,
FileName: filename,
}
return func(paths []string, callback core.ChangeCallback) {
return func(paths []string, callback core.ChangeCallback, ctx context.Context) {
callback(event, nil)
}
}
Expand Down

0 comments on commit c57852a

Please sign in to comment.