Skip to content

Commit

Permalink
chore: update fsnotify to use custom fork
Browse files Browse the repository at this point in the history
  • Loading branch information
Cian911 committed Aug 18, 2024
1 parent 4d09be1 commit 1025a3c
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 40 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ run:
@go run ./cmd/main.go

test-watcher:
@go test -v ./watcher
@gotest -v ./watcher

test-watcher-observe:
@go test -v ./watcher -test.run TestObserve
@gotest -v ./watcher -test.run TestObserve

test-event:
@go test -v ./event
@gotest -v ./event

test-utils:
@go test -v ./utils
@gotest -v ./utils

test-cmd:
@go test -v ./cmd
@gotest -v ./cmd

test-all: test-all test-watcher test-watcher-observe test-event test-utils test-cmd

Expand Down
6 changes: 3 additions & 3 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

var validOperations = map[string]bool{
"CREATE": true,
"WRITE": true,
"CLOSEWRITE": true,
"CREATE": true,
"WRITE": true,
"CLOSE_WRITE": true,
}

// Event is a struct that holds the information for a file event
Expand Down
4 changes: 1 addition & 3 deletions event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package event
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
Expand Down Expand Up @@ -98,8 +97,7 @@ func TestEvent(t *testing.T) {

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

_, err := os.CreateTemp(path, file)
if err != nil {
t.Fatalf("Unable to create temp file: %v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/fsnotify/fsnotify v1.7.0 => github.com/cian911/fsnotify v1.7.4
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
github.com/cian911/fsnotify v1.7.4 h1:O8KJ8orrsUpTRhtR9EHZi8jzbziX3c6wesa3C6WjRUk=
github.com/cian911/fsnotify v1.7.4/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down
3 changes: 1 addition & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -61,7 +60,7 @@ func ValidateFileExt(ext string) bool {
// im_a_dir -> true
// sample.txt -> false
func ScanFilesInDir(path string) (map[string]bool, error) {
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func TestUtils(t *testing.T) {

for _, tt := range tests {
got, err := ScanFilesInDir(tempDir)

if err != nil {
t.Fatalf("Could not scan files in dir: %v", err)
}
Expand Down Expand Up @@ -165,8 +164,7 @@ func setupTempDir(name string, t *testing.T) string {
}

func setupTempFile(name, dir string, t *testing.T) *os.File {
file, err := ioutil.TempFile(dir, name)

file, err := os.CreateTemp(dir, name)
if err != nil {
t.Fatalf("Unable to create temp file: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions watcher/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package watcher

import (
"io/ioutil"
"os"
"testing"
"time"

Expand All @@ -21,8 +21,7 @@ var (
// TestEventSetup sets up a new event for testing purposes
func TestEventSetup(t *testing.T) *event.Event {
path := t.TempDir()
_, err := ioutil.TempFile(path, HelperFile)

_, err := os.CreateTemp(path, HelperFile)
if err != nil {
t.Fatalf("Unable to create temp file: %v", err)
}
Expand All @@ -38,7 +37,7 @@ func TestEventSetup(t *testing.T) *event.Event {
}

// TestSimulateMultipleEvents takes a list of operations as args
// ["CREATE", "WRITE", "CLOSEWRITE"]
// ["CREATE", "WRITE", "CLOSE_WRITE"]
// and returns them as a list of events
func TestSimulateMultipleEvents(operationList []string, t *testing.T) []event.Event {
eventList := []event.Event{}
Expand Down
6 changes: 2 additions & 4 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
"regexp"
"time"

"github.com/fsnotify/fsnotify"

"github.com/cian911/switchboard/event"
"github.com/cian911/switchboard/utils"
"github.com/fsnotify/fsnotify"
)

// Producer interface for the watcher
Expand Down Expand Up @@ -109,7 +110,6 @@ 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)
}
Expand All @@ -118,7 +118,6 @@ func (pc *PathConsumer) ProcessDirEvent(e *event.Event) {
if utils.ExtractFileExt(file) == pc.Ext {
ev := event.New(file, e.Path, e.Destination, pc.Ext)
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 Expand Up @@ -222,5 +221,4 @@ func validateRegexEventMatch(pc *PathConsumer, event *event.Event) bool {

log.Println("Regex did not match")
return false

}
10 changes: 4 additions & 6 deletions watcher/watcher_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
"regexp"
"time"

"github.com/fsnotify/fsnotify"

"github.com/cian911/switchboard/event"
"github.com/cian911/switchboard/utils"
"github.com/fsnotify/fsnotify"
)

// Monitor for IN_CLOSE_WRITE events on these file exts
Expand Down Expand Up @@ -115,7 +116,6 @@ 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)
}
Expand All @@ -124,7 +124,6 @@ func (pc *PathConsumer) ProcessDirEvent(e *event.Event) {
if utils.ExtractFileExt(file) == pc.Ext {
ev := event.New(file, e.Path, e.Destination, pc.Ext)
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 Expand Up @@ -187,13 +186,13 @@ func (pw *PathWatcher) Observe(pollInterval int) {
case event := <-watcher.Events:
if event.Op.String() == "CREATE" && utils.IsDir(event.Name) {
watcher.Add(event.Name)
} else if event.Op.String() == "CLOSEWRITE" {
} else if event.Op.String() == "CLOSE_WRITE" {
ev := newEvent(event.Name, event.Op.String())

if specialWatchedFileExts[ev.Ext] {
// If the file is in the special file list
// add it to the queue and wait for it to be finished
log.Println("Adding CLOSEWRITE event to queue.")
log.Println("Adding CLOSE_WRITE event to queue.")
pw.Queue.Add(*ev)
} else {
// Otherwise process the event immediatly
Expand Down Expand Up @@ -225,7 +224,6 @@ func validateRegexEventMatch(pc *PathConsumer, event *event.Event) bool {

log.Println("Regex did not match")
return false

}

// Notify consumers of an event
Expand Down
5 changes: 2 additions & 3 deletions watcher/watcher_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestObserve(t *testing.T) {
t.Run("CLOSEWRITE", func(t *testing.T) {
t.Run("CLOSE_WRITE", func(t *testing.T) {
HelperPath = t.TempDir()
HelperDestination = t.TempDir()
HelperExt = ".txt"
Expand All @@ -34,7 +34,7 @@ func TestObserve(t *testing.T) {
files, _ := utils.ScanFilesInDir(HelperDestination)

if len(files) != 1 {
t.Errorf("CLOSEWRITE event was not processed - want: %d, got: %d", 1, len(files))
t.Errorf("CLOSE_WRITE event was not processed - want: %d, got: %d", 1, len(files))
}
})

Expand All @@ -60,6 +60,5 @@ func TestObserve(t *testing.T) {
})

t.Run("WRITE", func(t *testing.T) {

})
}
7 changes: 2 additions & 5 deletions watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package watcher

import (
"fmt"
"io/ioutil"
"os"
"testing"
"time"
Expand Down Expand Up @@ -52,7 +51,6 @@ func TestWatcher(t *testing.T) {
})

t.Run("It processes a new dir event", func(t *testing.T) {

ev := eventSetup(t)
ev.Path = t.TempDir()
ev.File = utils.ExtractFileExt(ev.Path)
Expand Down Expand Up @@ -135,8 +133,7 @@ func setup(p, d, e, rp string) (Producer, Consumer) {

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

_, err := os.CreateTemp(path, file)
if err != nil {
t.Fatalf("Unable to create temp file: %v", err)
}
Expand All @@ -152,7 +149,7 @@ func eventSetup(t *testing.T) *event.Event {
}

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

0 comments on commit 1025a3c

Please sign in to comment.