Christian Schmied
097c4b3d84
All checks were successful
go/folderwatcher/pipeline/head This commit looks good
87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package folderwatcher
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestOldFilesInCreationOrder(t *testing.T) {
|
|
tmpPath, err := os.MkdirTemp("", ".folderwatcher_test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(tmpPath)
|
|
quitChan := make(chan struct{})
|
|
|
|
filesIn := []string{"2", "C", "B", "A", "1"}
|
|
makeAbsFilePaths(t, tmpPath, &filesIn)
|
|
var filesOut []string
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
conf := Config{
|
|
Folder: tmpPath,
|
|
}
|
|
|
|
for _, f := range filesIn {
|
|
wg.Add(1)
|
|
_ = os.WriteFile(f, []byte{0}, os.ModePerm)
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
|
|
watcher, err := NewFolderWatcher(conf, true, quitChan)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
go watcher.Watch(func(filePath string) (bool, error) {
|
|
if !filepath.IsAbs(filePath) {
|
|
t.Errorf("file %s is not an absolute path", filePath)
|
|
}
|
|
filesOut = append(filesOut, filePath)
|
|
wg.Done()
|
|
return true, nil
|
|
}, func(s string, err error) {
|
|
})
|
|
|
|
finishedChan := make(chan struct{})
|
|
go func() {
|
|
wg.Wait()
|
|
finishedChan <- struct{}{}
|
|
}()
|
|
|
|
ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer ctxCancel()
|
|
|
|
select {
|
|
case <-finishedChan:
|
|
case <-ctx.Done():
|
|
t.Error(ctx.Err())
|
|
}
|
|
quitChan <- struct{}{}
|
|
|
|
if len(filesOut) != len(filesIn) {
|
|
t.Errorf("filesOut length %d != %d", len(filesOut), len(filesIn))
|
|
}
|
|
|
|
filesInS := fmt.Sprintf("%s", filesIn)
|
|
filesOutS := fmt.Sprintf("%s", filesOut)
|
|
if filesInS != filesOutS {
|
|
t.Errorf("filesIn != filesOut %s != %s", filesIn, filesOut)
|
|
}
|
|
}
|
|
|
|
func makeAbsFilePaths(t *testing.T, rootPath string, filesIn *[]string) {
|
|
t.Helper()
|
|
for i, f := range *filesIn { //remap filesIn to Full Path
|
|
(*filesIn)[i] = filepath.Join(rootPath, f)
|
|
if !filepath.IsAbs((*filesIn)[i]) {
|
|
t.Fatalf("file %s is not an absolute path", (*filesIn)[i])
|
|
}
|
|
}
|
|
}
|