Skip to content

Commit

Permalink
WATCHER: Process Dir events first - update tests to add cases for dir…
Browse files Browse the repository at this point in the history
… events.
  • Loading branch information
Cian911 committed Dec 28, 2021
1 parent 93baf23 commit fa1bd6f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
16 changes: 8 additions & 8 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ func (pc *PathConsumer) Receive(path, ev string) {
log.Printf("pc.Path: {%s}", pc.Path)
log.Printf("Event: %v", e)

if e.IsValidEvent(pc.Ext) {
log.Println("Event is valid")
pc.Process(e)
} else if e.IsNewDirEvent() {
if e.IsNewDirEvent() {
log.Println("Event is a new dir")

// Recursively scan dir for items with our ext
// Then add all recursive dirs as paths
pc.ProcessDirEvent(e)
} else if e.IsValidEvent(pc.Ext) {
log.Println("Event is valid")
pc.Process(e)
}
}

// Process takes an event and moves it to the destination
func (pc *PathConsumer) Process(e *event.Event) {
err := e.Move(e.Path)
err := e.Move(e.Path, "")
if err != nil {
log.Fatalf("Unable to move file from { %s } to { %s }: %v", e.Path, e.Destination, err)
} else {
Expand All @@ -97,17 +97,17 @@ func (pc *PathConsumer) Process(e *event.Event) {
// ProcessDirEvent takes an event and scans files ext
func (pc *PathConsumer) ProcessDirEvent(e *event.Event) {
files, err := utils.ScanFilesInDir(e.Path)

if err != nil {
log.Fatalf("Unable to scan files in dir event: error: %v, path: %s", err, e.Path)
}

// TODO: Copy files to destination nad only move on last file in dir that matches ext

for file, _ := range files {
for file := range files {
if utils.ExtractFileExt(file) == pc.Ext {
ev := event.New(file, e.Path, e.Destination, pc.Ext)
log.Printf("EVENT DIR: %v", ev)
err = ev.Move(ev.Path)
err = ev.Move(ev.Path, "/"+file)

if err != nil {
log.Printf("Unable to move file: %s from path: %s to dest: %s: %v", file, ev.Path, ev.Destination, err)
Expand Down
39 changes: 20 additions & 19 deletions watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"
"testing"
"time"

"github.com/cian911/switchboard/event"
"github.com/cian911/switchboard/utils"
Expand All @@ -31,7 +30,7 @@ var (

func TestWatcher(t *testing.T) {
t.Run("It registers a consumer", func(t *testing.T) {
pw, pc := setup()
pw, pc := setup(path, destination, ext)

pw.Register(&pc)

Expand All @@ -41,7 +40,7 @@ func TestWatcher(t *testing.T) {
})

t.Run("It unregisters a consumer", func(t *testing.T) {
pw, pc := setup()
pw, pc := setup(path, destination, ext)

pw.Register(&pc)
pw.Unregister(&pc)
Expand All @@ -52,17 +51,19 @@ func TestWatcher(t *testing.T) {
})

t.Run("It processes a new dir event", func(t *testing.T) {
pw, pc := setup()

pw.Register(&pc)
pw.Unregister(&pc)

ev := eventSetup(t)
ev.Path = t.TempDir()
ev.File = utils.ExtractFileExt(ev.Path)

pw, pc := setup(ev.Path, ev.Destination, ev.Ext)
pw.Register(&pc)
pw.Unregister(&pc)
t.Log("PATH: " + ev.Path + " FILE: " + ev.File)

for i := 1; i <= 3; i++ {
createTempFile(ev.Path, ".txt", t)
file := createTempFile(ev.Path, ".txt", t)
defer os.Remove(file.Name())
}

pc.Receive(ev.Path, "CREATE")
Expand All @@ -78,19 +79,20 @@ func TestWatcher(t *testing.T) {
got := len(filesInDir)

if want != got {
t.Fatalf("want: %d != got: %d", want, got)
t.Fatalf("want: %d != got: %d - debug - files: %v, event: %v", want, got, filesInDir, ev)
}
})
}

func setup() (Producer, Consumer) {
func setup(p, d, e string) (Producer, Consumer) {
var pw Producer = &PathWatcher{
Path: path,
Path: p,
}

var pc Consumer = &PathConsumer{
Path: path,
Destination: destination,
Path: p,
Destination: d,
Ext: e,
}

return pw, pc
Expand All @@ -113,12 +115,11 @@ func eventSetup(t *testing.T) *event.Event {
}
}

func createTempFile(path, ext string, t *testing.T) {
data := []byte("hello\nworld\n")
fileName := fmt.Sprintf("%d.%s", time.Now().Unix(), ext)
err := os.WriteFile(fmt.Sprintf("%s/%s", path, fileName), data, 0644)

func createTempFile(path, ext string, t *testing.T) *os.File {
file, err := ioutil.TempFile(path, fmt.Sprintf("*%s", ext))
if err != nil {
t.Fatalf("Could not create test file: %v", err)
t.Fatalf("Could not create temp file: %v", err)
}

return file
}

0 comments on commit fa1bd6f

Please sign in to comment.