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
}
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 {

View File

@ -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 {

View File

@ -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)

View File

@ -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) {