Skip to content

Commit

Permalink
SPECS: Update and add more tests across the board.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cian911 committed Dec 16, 2021
1 parent 25b8adb commit b8fc6b1
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 26 deletions.
7 changes: 3 additions & 4 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ type Event struct {
Operation string
}

func (e *Event) Move() {
func (e *Event) Move() error {
log.Printf("Moving e.Path: %s to %s/%s\n", e.Path, e.Destination, e.File)

err := os.Rename(e.Path, fmt.Sprintf("%s/%s", e.Destination, e.File))
if err != nil {
log.Fatalf("Unable to move file from { %s } to { %s }: %v", e.Path, e.Destination, err)
}
return err
}

func (e *Event) IsValidEvent(ext string) bool {
Expand Down
49 changes: 47 additions & 2 deletions event/event_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package event

import "testing"
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
)

var (
event = &Event{
Expand All @@ -11,7 +18,8 @@ var (
Operation: "CREATE",
}

ext = ".txt"
ext = ".txt"
file = "sample.txt"
)

func TestEvent(t *testing.T) {
Expand All @@ -32,4 +40,41 @@ func TestEvent(t *testing.T) {
t.Errorf("event extension is not valid, when it should have been: want=%t, got=%t", want, got)
}
})

t.Run("It moves file from one dir to another dir", func(t *testing.T) {
event := eventSetup(t)
event.Move()

// If the file does not exist, log an error
if _, err := os.Stat(fmt.Sprintf("%s/%s", event.Destination, event.File)); errors.Is(err, os.ErrNotExist) {
t.Fatalf("Failed to move from %s/%s to %s/%s: %v : %v", event.Path, event.File, event.Destination, event.File, err, *event)
}
})

t.Run("It does not move file from one dir to another dir", func(t *testing.T) {
event := eventSetup(t)
event.Destination = "/abcdefg"
err := event.Move()

if err == nil {
log.Fatal("event.Move() should have thrown error but didn't.")
}
})
}

func eventSetup(t *testing.T) *Event {
path := t.TempDir()
_, err := ioutil.TempFile(path, file)

if err != nil {
t.Fatalf("Unable to create temp file: %v", err)
}

return &Event{
File: file,
Path: path,
Destination: t.TempDir(),
Ext: ext,
Operation: "CREATE",
}
}
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func ValidatePath(path string) bool {
}

func ValidateFileExt(ext string) bool {
if ext[0:1] != "." {
if ext == "" || ext[0:1] != "." {
return false
}

Expand Down
75 changes: 58 additions & 17 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,63 @@ const (
)

func TestUtils(t *testing.T) {
tests := []struct {
input string
expectedOutput string
}{
{"/home/test/movie.mp4", ".mp4"},
{"/home/test/movie.maaaap4.aaa.mp4", ".mp4"},
{"/home/test/", ""},
{"/home/test", ""},
{"/home/test/movie.mp4/", ""},
}

for _, tt := range tests {
got := ExtractFileExt(tt.input)

if got != tt.expectedOutput {
t.Errorf("Failed extracting file extention: got=%s, want=%s", got, tt.expectedOutput)
t.Run("It tests ExtractFileExt()", func(t *testing.T) {
tests := []struct {
input string
expectedOutput string
}{
{"/home/test/movie.mp4", ".mp4"},
{"/home/test/movie.maaaap4.aaa.mp4", ".mp4"},
{"/home/test/", ""},
{"/home/test", ""},
{"/home/test/movie.mp4/", ""},
}
}

for _, tt := range tests {
got := ExtractFileExt(tt.input)

if got != tt.expectedOutput {
t.Errorf("Failed extracting file extention: got=%s, want=%s", got, tt.expectedOutput)
}
}
})

t.Run("It tests ValidatePath()", func(t *testing.T) {
tests := []struct {
input string
expectedOutput bool
}{
{"", false},
{"/abba/asdas/asda", false},
{t.TempDir(), true},
}

for _, tt := range tests {
got := ValidatePath(tt.input)

if got != tt.expectedOutput {
t.Errorf("Failed extracting file extention: got=%t, want=%t", got, tt.expectedOutput)
}
}
})

t.Run("It tests ValidateFileExt()", func(t *testing.T) {
tests := []struct {
input string
expectedOutput bool
}{
{"", false},
{"file", false},
{"file...txt", false},
{".txt", true},
}

for _, tt := range tests {
got := ValidateFileExt(tt.input)

if got != tt.expectedOutput {
t.Errorf("Failed extracting file extention: got=%t, want=%t", got, tt.expectedOutput)
}
}
})
}
8 changes: 6 additions & 2 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ func (pc *PathConsumer) Receive(path, ev string) {
}

func (pc *PathConsumer) Process(e *event.Event) {
e.Move()
log.Println("Event has been processed.")
err := e.Move()
if err != nil {
log.Fatalf("Unable to move file from { %s } to { %s }: %v", e.Path, e.Destination, err)
} else {
log.Println("Event has been processed.")
}
}

func (pw *PathWatcher) AddPath(path string) {
Expand Down

0 comments on commit b8fc6b1

Please sign in to comment.