From 16b0a31dd4c180f544b9adfcea86cabe581b3510 Mon Sep 17 00:00:00 2001 From: Urko Date: Sun, 26 Feb 2023 16:59:20 +0100 Subject: [PATCH] feat: complete tests --- internal/watcher/watcher.go | 14 ++++++++-- internal/watcher/watcher_test.go | 46 +++++++++++++++++++++++++------- main.go | 8 ++++-- pkg/watcher/watcher.go | 6 +++++ pkg/watcher/watcher_test.go | 3 ++- 5 files changed, 62 insertions(+), 15 deletions(-) diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 33f5cf0..ecdb244 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -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 ¬ifier{} +} + 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 diff --git a/internal/watcher/watcher_test.go b/internal/watcher/watcher_test.go index 084b0b3..721e4c6 100644 --- a/internal/watcher/watcher_test.go +++ b/internal/watcher/watcher_test.go @@ -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 = ¬ifier{} +) + 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 { diff --git a/main.go b/main.go index 35b7795..1b0b120 100644 --- a/main.go +++ b/main.go @@ -11,13 +11,17 @@ import ( pkgwatcher "gitea.urkob.com/urko/git-webhook-ci/pkg/watcher" ) -var watcherIface pkgwatcher.WatcherIface +var ( + watcherIface pkgwatcher.WatcherIface + notifierIface pkgwatcher.NotifyIface +) func main() { isProd := os.Getenv("ENV") == "prod" config := cfg.NewConfig(isProd) - watcherIface = watcher.NewWatcher(pkgwatcher.Deploy) + notifierIface = watcher.NewNotifier() + watcherIface = watcher.NewWatcher(notifierIface, pkgwatcher.Deploy) defer func() { if err := watcherIface.Close(); err != nil { diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go index 276d633..64f4698 100644 --- a/pkg/watcher/watcher.go +++ b/pkg/watcher/watcher.go @@ -5,8 +5,14 @@ import ( "log" "os" "os/exec" + + "github.com/fsnotify/fsnotify" ) +type NotifyIface interface { + NewWatcher() (*fsnotify.Watcher, error) +} + type WatcherIface interface { Monitor(path string) error Listen(binaryPath, scriptPath string, outputErr chan<- error) diff --git a/pkg/watcher/watcher_test.go b/pkg/watcher/watcher_test.go index 6bbb85d..238aa18 100644 --- a/pkg/watcher/watcher_test.go +++ b/pkg/watcher/watcher_test.go @@ -3,12 +3,13 @@ package watcher import ( "testing" + "gitea.urkob.com/urko/git-webhook-ci/cfg" "github.com/stretchr/testify/require" ) var ( binaryPath = "/bin/bash" - scriptPath = "./test-script.sh" + scriptPath = cfg.RootDir() + "/test-script.sh" ) func TestDeploy(t *testing.T) {