feat: complete tests

This commit is contained in:
Urko 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 deploy pkgwatcher.DeployFunc
} }
type notifier struct{}
func (n *notifier) NewWatcher() (*fsnotify.Watcher, error) {
return fsnotify.NewWatcher()
}
func NewNotifier() *notifier {
return &notifier{}
}
var ( var (
errEventsClosedChan = errors.New("events is closed") errEventsClosedChan = errors.New("events is closed")
errErrorsClosedChan = errors.New("errors is closed") errErrorsClosedChan = errors.New("errors is closed")
) )
func NewWatcher(deploy pkgwatcher.DeployFunc) *watcher { func NewWatcher(notifier pkgwatcher.NotifyIface, deploy pkgwatcher.DeployFunc) *watcher {
wt, err := fsnotify.NewWatcher() wt, err := notifier.NewWatcher()
if err != nil { if err != nil {
log.Printf("fsnotify.NewWatcher: %s\n", err) log.Printf("fsnotify.NewWatcher: %s\n", err)
return nil return nil

View File

@ -12,6 +12,19 @@ import (
"github.com/stretchr/testify/require" "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 ( var (
mockDeploy pkgwatcher.DeployFunc mockDeploy pkgwatcher.DeployFunc
mockErrorDeploy pkgwatcher.DeployFunc mockErrorDeploy pkgwatcher.DeployFunc
@ -50,33 +63,46 @@ func sendTestEvents(w *watcher) {
} }
} }
func getNewWatcher() *watcher { func newWatcher() *watcher {
return NewWatcher(mockDeploy) return NewWatcher(okNotifier, mockDeploy)
} }
func getNewWatcherWithError() *watcher { func newWatcherWithDeployError() *watcher {
return NewWatcher(mockErrorDeploy) 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) { func Test_NewWatcher(t *testing.T) {
w := getNewWatcher() w := newWatcher()
require.NotNil(t, w) require.NotNil(t, w)
} }
func Test_ErrorNewWatcher(t *testing.T) {
w := newWatcherWithCtorError()
require.Nil(t, w)
}
func Test_Close(t *testing.T) { func Test_Close(t *testing.T) {
w := getNewWatcher() w := newWatcher()
err := w.Close() err := w.Close()
require.NoError(t, err) require.NoError(t, err)
} }
func Test_Monitor(t *testing.T) { func Test_Monitor(t *testing.T) {
w := getNewWatcher() w := newWatcher()
err := w.Monitor(config.TestFileToWatchPath) err := w.Monitor(config.TestFileToWatchPath)
require.NoError(t, err) require.NoError(t, err)
} }
func Test_ListenSuccess(t *testing.T) { func Test_ListenSuccess(t *testing.T) {
w := getNewWatcher() w := newWatcher()
ctx, errors := listenWithSendEvents(w) ctx, errors := listenWithSendEvents(w)
for { for {
@ -91,7 +117,7 @@ func Test_ListenSuccess(t *testing.T) {
} }
func Test_ListenError(t *testing.T) { func Test_ListenError(t *testing.T) {
w := getNewWatcherWithError() w := newWatcherWithDeployError()
ctx, errors := listenWithSendEvents(w) ctx, errors := listenWithSendEvents(w)
for { for {
@ -107,7 +133,7 @@ func Test_ListenError(t *testing.T) {
} }
func Test_ListenErrorChanClose(t *testing.T) { func Test_ListenErrorChanClose(t *testing.T) {
w := getNewWatcher() w := newWatcher()
ctx, errors := listenWithSendEvents(w) ctx, errors := listenWithSendEvents(w)
close(w.fswatcher.Events) close(w.fswatcher.Events)
for { for {

View File

@ -11,13 +11,17 @@ import (
pkgwatcher "gitea.urkob.com/urko/git-webhook-ci/pkg/watcher" pkgwatcher "gitea.urkob.com/urko/git-webhook-ci/pkg/watcher"
) )
var watcherIface pkgwatcher.WatcherIface var (
watcherIface pkgwatcher.WatcherIface
notifierIface pkgwatcher.NotifyIface
)
func main() { func main() {
isProd := os.Getenv("ENV") == "prod" isProd := os.Getenv("ENV") == "prod"
config := cfg.NewConfig(isProd) config := cfg.NewConfig(isProd)
watcherIface = watcher.NewWatcher(pkgwatcher.Deploy) notifierIface = watcher.NewNotifier()
watcherIface = watcher.NewWatcher(notifierIface, pkgwatcher.Deploy)
defer func() { defer func() {
if err := watcherIface.Close(); err != nil { if err := watcherIface.Close(); err != nil {

View File

@ -5,8 +5,14 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"github.com/fsnotify/fsnotify"
) )
type NotifyIface interface {
NewWatcher() (*fsnotify.Watcher, error)
}
type WatcherIface interface { type WatcherIface interface {
Monitor(path string) error Monitor(path string) error
Listen(binaryPath, scriptPath string, outputErr chan<- error) Listen(binaryPath, scriptPath string, outputErr chan<- error)

View File

@ -3,12 +3,13 @@ package watcher
import ( import (
"testing" "testing"
"gitea.urkob.com/urko/git-webhook-ci/cfg"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var ( var (
binaryPath = "/bin/bash" binaryPath = "/bin/bash"
scriptPath = "./test-script.sh" scriptPath = cfg.RootDir() + "/test-script.sh"
) )
func TestDeploy(t *testing.T) { func TestDeploy(t *testing.T) {