Skip to content

Commit

Permalink
refactor(watch): return err instead of log fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaschain committed Dec 3, 2023
1 parent 65922e5 commit 930e235
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/change_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ type ChangeEvent struct {
FileName string
}

type ChangeCallback func(*ChangeEvent, *error)
type ChangeCallback func(*ChangeEvent, *error) *error
3 changes: 2 additions & 1 deletion infrastructure/file_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ func TestFileWatcher(t *testing.T) {
testFile := testFilePath + "/" + testFileName
ctx, cancel := context.WithCancel(context.Background())
var fileName string
callback := func(event *core.ChangeEvent, err *error) {
callback := func(event *core.ChangeEvent, err *error) *error {
fileName = event.FileName
cancel()
return nil
}
os.MkdirAll(testFilePath, 0755)
os.Create(testFilePath + "/" + testFileName)
Expand Down
8 changes: 4 additions & 4 deletions use_case/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package use_case
import (
"context"
"fmt"
"log"

"github.com/lucaschain/beholder/core"
"github.com/lucaschain/beholder/core/event_types"
Expand All @@ -20,19 +19,20 @@ type FileWatcher func(paths []string, callback core.ChangeCallback, ctx context.
type CommandRunner func(command []string) error

func onFileChange(c WatchConfig, commandRunner CommandRunner) core.ChangeCallback {
return func(event *core.ChangeEvent, err *error) {
return func(event *core.ChangeEvent, err *error) *error {
if err != nil {
log.Fatal(err)
return err
}

if event_types.Filter(event.Type, c.AllowedTypes) {
command := core.CommandTokens(c.Command, event)
commandError := commandRunner(command)

if commandError != nil && !c.AllowFailing {
log.Fatal(commandError)
return &commandError
}
}
return nil
}
}

Expand Down
43 changes: 43 additions & 0 deletions use_case/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package use_case_test

import (
"context"
"errors"
"testing"

"github.com/lucaschain/beholder/core"
Expand Down Expand Up @@ -59,6 +60,48 @@ func TestWatch(t *testing.T) {

assert.Equal(t, []string{"echo", "CREATE", "test.txt"}, command)
})

t.Run("should return watcher errors without treating anything", func(t *testing.T) {
config := buildWatchConfig()
config.AllowedTypes = []event_types.EventType{event_types.Create}
config.Command = []string{"echo", "{type}", "{file}"}

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

use_case.Watch(config, fakeWatcher, func(command []string) error { return nil })

assert.Equal(t, fakeErr, returnedError)
})

t.Run("should return command errors if allowing errors", func(t *testing.T) {
config := buildWatchConfig()
config.AllowedTypes = []event_types.EventType{event_types.Create}
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)

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

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

func buildFakeFileWatcher(filename string, eventType event_types.EventType) use_case.FileWatcher {
Expand Down

0 comments on commit 930e235

Please sign in to comment.