feat: complete tests

This commit is contained in:
2023-02-26 16:59:20 +01:00
parent 49a72dc34b
commit 16b0a31dd4
5 changed files with 62 additions and 15 deletions

View File

@@ -13,13 +13,23 @@ type watcher struct {
deploy pkgwatcher.DeployFunc
}
type notifier struct{}
func (n *notifier) NewWatcher() (*fsnotify.Watcher, error) {
return fsnotify.NewWatcher()
}
func NewNotifier() *notifier {
return &notifier{}
}
var (
errEventsClosedChan = errors.New("events is closed")
errErrorsClosedChan = errors.New("errors is closed")
)
func NewWatcher(deploy pkgwatcher.DeployFunc) *watcher {
wt, err := fsnotify.NewWatcher()
func NewWatcher(notifier pkgwatcher.NotifyIface, deploy pkgwatcher.DeployFunc) *watcher {
wt, err := notifier.NewWatcher()
if err != nil {
log.Printf("fsnotify.NewWatcher: %s\n", err)
return nil

View File

@@ -12,6 +12,19 @@ import (
"github.com/stretchr/testify/require"
)
type testErrorNotifier struct {
*fsnotify.Watcher
}
func (n *testErrorNotifier) NewWatcher() (*fsnotify.Watcher, error) {
return nil, errIntentional
}
var (
errNotifier = &testErrorNotifier{}
okNotifier = &notifier{}
)
var (
mockDeploy pkgwatcher.DeployFunc
mockErrorDeploy pkgwatcher.DeployFunc
@@ -50,33 +63,46 @@ func sendTestEvents(w *watcher) {
}
}
func getNewWatcher() *watcher {
return NewWatcher(mockDeploy)
func newWatcher() *watcher {
return NewWatcher(okNotifier, mockDeploy)
}
func getNewWatcherWithError() *watcher {
return NewWatcher(mockErrorDeploy)
func newWatcherWithDeployError() *watcher {
return NewWatcher(okNotifier, mockErrorDeploy)
}
func newWatcherWithCtorError() *watcher {
return NewWatcher(errNotifier, mockDeploy)
}
func Test_NewNotifier(t *testing.T) {
require.NotNil(t, NewNotifier())
}
func Test_NewWatcher(t *testing.T) {
w := getNewWatcher()
w := newWatcher()
require.NotNil(t, w)
}
func Test_ErrorNewWatcher(t *testing.T) {
w := newWatcherWithCtorError()
require.Nil(t, w)
}
func Test_Close(t *testing.T) {
w := getNewWatcher()
w := newWatcher()
err := w.Close()
require.NoError(t, err)
}
func Test_Monitor(t *testing.T) {
w := getNewWatcher()
w := newWatcher()
err := w.Monitor(config.TestFileToWatchPath)
require.NoError(t, err)
}
func Test_ListenSuccess(t *testing.T) {
w := getNewWatcher()
w := newWatcher()
ctx, errors := listenWithSendEvents(w)
for {
@@ -91,7 +117,7 @@ func Test_ListenSuccess(t *testing.T) {
}
func Test_ListenError(t *testing.T) {
w := getNewWatcherWithError()
w := newWatcherWithDeployError()
ctx, errors := listenWithSendEvents(w)
for {
@@ -107,7 +133,7 @@ func Test_ListenError(t *testing.T) {
}
func Test_ListenErrorChanClose(t *testing.T) {
w := getNewWatcher()
w := newWatcher()
ctx, errors := listenWithSendEvents(w)
close(w.fswatcher.Events)
for {