Skip to content

Commit

Permalink
test(watch): test error return scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaschain committed Dec 3, 2023
1 parent 930e235 commit 36fef86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
18 changes: 14 additions & 4 deletions infrastructure/file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/lucaschain/beholder/core/event_types"
)

func loop(watcher *fsnotify.Watcher, callback core.ChangeCallback) {
func loop(watcher *fsnotify.Watcher, callback core.ChangeCallback) *error {
for {
select {
case event, ok := <-watcher.Events:
Expand All @@ -20,7 +20,10 @@ func loop(watcher *fsnotify.Watcher, callback core.ChangeCallback) {
Type: event_types.FromString(event.Op.String()),
FileName: event.Name,
}
callback(&changeEvent, nil)
err := callback(&changeEvent, nil)
if err != nil {
return err
}
case err, _ := <-watcher.Errors:
if err != nil {
callback(nil, &err)
Expand All @@ -29,15 +32,19 @@ func loop(watcher *fsnotify.Watcher, callback core.ChangeCallback) {
}
}

func FileWatcher(paths []string, callback core.ChangeCallback, ctx context.Context) {
func FileWatcher(paths []string, callback core.ChangeCallback, ctx context.Context) *error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
callback(nil, &err)
}
defer watcher.Close()

go func() {
loop(watcher, callback)
commandError := loop(watcher, callback)

if commandError != nil {
ctx.Done()
}
}()

for _, path := range paths {
Expand All @@ -48,4 +55,7 @@ func FileWatcher(paths []string, callback core.ChangeCallback, ctx context.Conte
}

<-ctx.Done()

err = ctx.Err()
return &err
}
9 changes: 5 additions & 4 deletions use_case/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type WatchConfig struct {
AllowFailing bool
}

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

func onFileChange(c WatchConfig, commandRunner CommandRunner) core.ChangeCallback {
Expand All @@ -28,7 +28,7 @@ func onFileChange(c WatchConfig, commandRunner CommandRunner) core.ChangeCallbac
command := core.CommandTokens(c.Command, event)
commandError := commandRunner(command)

if commandError != nil && !c.AllowFailing {
if commandError != nil && c.AllowFailing {
return &commandError
}
}
Expand All @@ -40,8 +40,9 @@ func Watch(
c WatchConfig,
fileWatcher FileWatcher,
commandRunner CommandRunner,
) {
) *error {
fmt.Printf("Watching path: %s and running command: '%s'\n", c.Paths, c.Command)
callback := onFileChange(c, commandRunner)
fileWatcher(c.Paths, callback, context.Background())

return fileWatcher(c.Paths, callback, context.Background())
}
16 changes: 8 additions & 8 deletions use_case/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ func TestWatch(t *testing.T) {

var returnedError error
var fakeErr error
fakeWatcher := func(paths []string, callback core.ChangeCallback, ctx context.Context) {
fakeWatcher := func(paths []string, callback core.ChangeCallback, ctx context.Context) *error {
fakeErr = errors.New("error")
returnedError = *callback(nil, &fakeErr)
return nil
}

use_case.Watch(config, fakeWatcher, func(command []string) error { return nil })
Expand All @@ -84,23 +85,22 @@ func TestWatch(t *testing.T) {
config.Command = []string{"echo", "{type}", "{file}"}
config.AllowFailing = true

var returnedError error
commandError := errors.New("error")
fakeRunner := func(command []string) error {
return commandError
}

fakeWatcher := buildFakeFileWatcher("test.txt", event_types.Create)
use_case.Watch(config, fakeWatcher, fakeRunner)
err := use_case.Watch(config, fakeWatcher, fakeRunner)

assert.Equal(t, commandError, returnedError)
assert.Equal(t, commandError, *err)
})
}

func buildFakeErrorFileWatcher() use_case.FileWatcher {
return func(paths []string, callback core.ChangeCallback, ctx context.Context) {
return func(paths []string, callback core.ChangeCallback, ctx context.Context) *error {
error := errors.New("error")
callback(nil, &error)
return callback(nil, &error)
}
}

Expand All @@ -113,8 +113,8 @@ func buildFakeFileWatcher(filename string, eventType event_types.EventType) use_
Type: event_types.Create,
FileName: filename,
}
return func(paths []string, callback core.ChangeCallback, ctx context.Context) {
callback(event, nil)
return func(paths []string, callback core.ChangeCallback, ctx context.Context) *error {
return callback(event, nil)
}
}

Expand Down

0 comments on commit 36fef86

Please sign in to comment.