refactor(watch): detect filesystem changes with fsbroker (#3064)
Assisted-by: Crush:prism
This commit is contained in:
parent
94ae261312
commit
847c43976e
12 changed files with 330 additions and 104 deletions
|
|
@ -4,6 +4,12 @@
|
|||
|
||||
### 🚀 Features
|
||||
|
||||
- Task's [watch functionality](https://taskfile.dev/docs/guide/watch)
|
||||
(`--watch` or `watch: true`) was partly rewritten to be significantly more
|
||||
reliable, specially when it comes to detecting duplicated events.
|
||||
We're now using [@helshabini](https://github.com/helshabini)'s
|
||||
[`fsbroker`](https://github.com/helshabini/fsbroker) package which is a lot
|
||||
smarter than the custom code we had before.
|
||||
- Interactive selection prompts for `enum` variables now handle long lists: they
|
||||
show a scrollable window of choices, support case-insensitive search with `/`,
|
||||
and add Page Up/Page Down and Home/End navigation (#3071 by @vmaerten).
|
||||
|
|
|
|||
3
go.mod
3
go.mod
|
|
@ -14,11 +14,11 @@ require (
|
|||
github.com/dominikbraun/graph v0.23.0
|
||||
github.com/elliotchance/orderedmap/v3 v3.1.1
|
||||
github.com/fatih/color v1.19.0
|
||||
github.com/fsnotify/fsnotify v1.10.1
|
||||
github.com/go-task/slim-sprig/v3 v3.0.0
|
||||
github.com/go-task/template v0.2.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/hashicorp/go-getter v1.8.10
|
||||
github.com/helshabini/fsbroker v1.0.5
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/mitchellh/hashstructure/v2 v2.0.2
|
||||
github.com/puzpuzpuz/xsync/v4 v4.5.0
|
||||
|
|
@ -81,6 +81,7 @@ require (
|
|||
github.com/envoyproxy/go-control-plane/envoy v1.40.0 // indirect
|
||||
github.com/envoyproxy/protoc-gen-validate v1.3.3 // indirect
|
||||
github.com/felixge/httpsnoop v1.1.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.10.1 // indirect
|
||||
github.com/go-jose/go-jose/v4 v4.1.6 // indirect
|
||||
github.com/go-logr/logr v1.4.4 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -171,6 +171,8 @@ github.com/hashicorp/go-getter v1.8.10 h1:uufJp+uacA0fSDRxclsHkiE6yUQTKgwv5b25Qe
|
|||
github.com/hashicorp/go-getter v1.8.10/go.mod h1:5HyKGCQmT+I3MGAM76vk/fWpktjwxn7xruCUr9CrVuo=
|
||||
github.com/hashicorp/go-version v1.9.0 h1:CeOIz6k+LoN3qX9Z0tyQrPtiB1DFYRPfCIBtaXPSCnA=
|
||||
github.com/hashicorp/go-version v1.9.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
|
||||
github.com/helshabini/fsbroker v1.0.5 h1:XXbLbq67AWsmiszsfzT6wvNe5WDLEgXR81ooASh5axE=
|
||||
github.com/helshabini/fsbroker v1.0.5/go.mod h1:aOjlwOw26/rSNtzFcSPchbuXAgxWWI6GYw7e8YIV4hs=
|
||||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
package fsnotifyext
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
type Deduper struct {
|
||||
w *fsnotify.Watcher
|
||||
waitTime time.Duration
|
||||
}
|
||||
|
||||
func NewDeduper(w *fsnotify.Watcher, waitTime time.Duration) *Deduper {
|
||||
return &Deduper{
|
||||
w: w,
|
||||
waitTime: waitTime,
|
||||
}
|
||||
}
|
||||
|
||||
// GetChan returns a chan of deduplicated [fsnotify.Event].
|
||||
//
|
||||
// [fsnotify.Chmod] operations will be skipped.
|
||||
func (d *Deduper) GetChan() <-chan fsnotify.Event {
|
||||
channel := make(chan fsnotify.Event)
|
||||
|
||||
go func() {
|
||||
timers := make(map[string]*time.Timer)
|
||||
for {
|
||||
event, ok := <-d.w.Events
|
||||
switch {
|
||||
case !ok:
|
||||
return
|
||||
case event.Has(fsnotify.Chmod):
|
||||
continue
|
||||
}
|
||||
|
||||
timer, ok := timers[event.String()]
|
||||
if !ok {
|
||||
timer = time.AfterFunc(math.MaxInt64, func() { channel <- event })
|
||||
timer.Stop()
|
||||
timers[event.String()] = timer
|
||||
}
|
||||
|
||||
timer.Reset(d.waitTime)
|
||||
}
|
||||
}()
|
||||
|
||||
return channel
|
||||
}
|
||||
1
testdata/watch_newdir/.gitignore
vendored
Normal file
1
testdata/watch_newdir/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
src/*
|
||||
10
testdata/watch_newdir/Taskfile.yaml
vendored
Normal file
10
testdata/watch_newdir/Taskfile.yaml
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# https://taskfile.dev
|
||||
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
default:
|
||||
sources:
|
||||
- "src/**/*"
|
||||
cmds:
|
||||
- echo "Task running!"
|
||||
1
testdata/watch_remove/.gitignore
vendored
Normal file
1
testdata/watch_remove/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
src/*
|
||||
10
testdata/watch_remove/Taskfile.yaml
vendored
Normal file
10
testdata/watch_remove/Taskfile.yaml
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# https://taskfile.dev
|
||||
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
default:
|
||||
sources:
|
||||
- "src/*"
|
||||
cmds:
|
||||
- echo "Task running!"
|
||||
1
testdata/watch_sources/.gitignore
vendored
Normal file
1
testdata/watch_sources/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
src/*
|
||||
10
testdata/watch_sources/Taskfile.yaml
vendored
Normal file
10
testdata/watch_sources/Taskfile.yaml
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# https://taskfile.dev
|
||||
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
default:
|
||||
sources:
|
||||
- "src/*.txt"
|
||||
cmds:
|
||||
- echo "Task running!"
|
||||
186
watch.go
186
watch.go
|
|
@ -3,6 +3,7 @@ package task
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
|
|
@ -11,13 +12,11 @@ import (
|
|||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/helshabini/fsbroker"
|
||||
"github.com/puzpuzpuz/xsync/v4"
|
||||
|
||||
"github.com/go-task/task/v3/errors"
|
||||
"github.com/go-task/task/v3/internal/filepathext"
|
||||
"github.com/go-task/task/v3/internal/fingerprint"
|
||||
"github.com/go-task/task/v3/internal/fsnotifyext"
|
||||
"github.com/go-task/task/v3/internal/logger"
|
||||
"github.com/go-task/task/v3/internal/slicesext"
|
||||
"github.com/go-task/task/v3/taskfile/ast"
|
||||
|
|
@ -56,27 +55,52 @@ func (e *Executor) watchTasks(calls ...*Call) error {
|
|||
waitTime = defaultWaitTime
|
||||
}
|
||||
|
||||
w, err := fsnotify.NewWatcher()
|
||||
config := fsbroker.DefaultFSConfig()
|
||||
config.Timeout = waitTime
|
||||
config.IgnoreHiddenFiles = false
|
||||
|
||||
broker, err := fsbroker.NewFSBroker(config)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return err
|
||||
}
|
||||
defer w.Close()
|
||||
defer broker.Stop()
|
||||
|
||||
deduper := fsnotifyext.NewDeduper(w, waitTime)
|
||||
eventsChan := deduper.GetChan()
|
||||
closeOnInterrupt(broker)
|
||||
|
||||
closeOnInterrupt(w)
|
||||
e.watchedDirs = xsync.NewMap[string, bool]()
|
||||
|
||||
broker.Start()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case event, ok := <-eventsChan:
|
||||
case action, ok := <-broker.Next():
|
||||
if !ok {
|
||||
cancel()
|
||||
return
|
||||
}
|
||||
e.Logger.VerboseErrf(logger.Magenta, "task: received watch event: %v\n", event)
|
||||
|
||||
actions := e.filterWatchActions(broker, drainActions(broker, action))
|
||||
if len(actions) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
files, err := e.collectSources(calls)
|
||||
if err != nil {
|
||||
e.Logger.Errf(logger.Red, "%v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if !slices.ContainsFunc(actions, func(a *fsbroker.FSAction) bool {
|
||||
return isRelevantWatchAction(a, files)
|
||||
}) {
|
||||
for _, a := range actions {
|
||||
relPath, _ := filepath.Rel(e.Dir, a.Subject.Path)
|
||||
e.Logger.VerboseErrf(logger.Magenta, "task: skipped for file not in sources: %s\n", relPath)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
cancel()
|
||||
ctx, cancel = context.WithCancel(context.Background())
|
||||
|
|
@ -84,37 +108,16 @@ func (e *Executor) watchTasks(calls ...*Call) error {
|
|||
e.Compiler.ResetCache()
|
||||
|
||||
for _, c := range calls {
|
||||
go func() {
|
||||
if ShouldIgnore(event.Name) {
|
||||
e.Logger.VerboseErrf(logger.Magenta, "task: event skipped for being an ignored dir: %s\n", event.Name)
|
||||
return
|
||||
}
|
||||
t, err := e.GetTask(c)
|
||||
if err != nil {
|
||||
e.Logger.Errf(logger.Red, "%v\n", err)
|
||||
return
|
||||
}
|
||||
baseDir := filepathext.SmartJoin(e.Dir, t.Dir)
|
||||
files, err := e.collectSources(calls)
|
||||
if err != nil {
|
||||
e.Logger.Errf(logger.Red, "%v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !event.Has(fsnotify.Remove) && !slices.Contains(files, filepath.ToSlash(event.Name)) {
|
||||
relPath, _ := filepath.Rel(baseDir, event.Name)
|
||||
e.Logger.VerboseErrf(logger.Magenta, "task: skipped for file not in sources: %s\n", relPath)
|
||||
return
|
||||
}
|
||||
err = e.RunTask(ctx, c)
|
||||
go func(ctx context.Context, c *Call) {
|
||||
err := e.RunTask(ctx, c)
|
||||
if err == nil {
|
||||
e.Logger.Errf(logger.Green, "task: task \"%s\" finished running\n", c.Task)
|
||||
} else if !isContextError(err) {
|
||||
e.Logger.Errf(logger.Red, "%v\n", err)
|
||||
}
|
||||
}()
|
||||
}(ctx, c)
|
||||
}
|
||||
case err, ok := <-w.Errors:
|
||||
case err, ok := <-broker.Error():
|
||||
switch {
|
||||
case !ok:
|
||||
cancel()
|
||||
|
|
@ -126,14 +129,12 @@ func (e *Executor) watchTasks(calls ...*Call) error {
|
|||
}
|
||||
}()
|
||||
|
||||
e.watchedDirs = xsync.NewMap[string, bool]()
|
||||
|
||||
go func() {
|
||||
// NOTE(@andreynering): New files can be created in directories
|
||||
// that were previously empty, so we need to check for new dirs
|
||||
// from time to time.
|
||||
for {
|
||||
if err := e.registerWatchedDirs(w, calls...); err != nil {
|
||||
if err := e.registerWatchedDirs(broker, calls...); err != nil {
|
||||
e.Logger.Errf(logger.Red, "%v\n", err)
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
|
|
@ -152,39 +153,120 @@ func isContextError(err error) bool {
|
|||
return errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)
|
||||
}
|
||||
|
||||
func closeOnInterrupt(w *fsnotify.Watcher) {
|
||||
func closeOnInterrupt(broker *fsbroker.FSBroker) {
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-ch
|
||||
w.Close()
|
||||
broker.Stop()
|
||||
os.Exit(0)
|
||||
}()
|
||||
}
|
||||
|
||||
func (e *Executor) registerWatchedDirs(w *fsnotify.Watcher, calls ...*Call) error {
|
||||
// drainActions returns the given action along with any other action that is
|
||||
// already available, so that a burst of changes (e.g. a git checkout) results
|
||||
// in a single task run instead of one per changed file.
|
||||
func drainActions(broker *fsbroker.FSBroker, action *fsbroker.FSAction) []*fsbroker.FSAction {
|
||||
actions := []*fsbroker.FSAction{action}
|
||||
for {
|
||||
select {
|
||||
case next, ok := <-broker.Next():
|
||||
if !ok {
|
||||
return actions
|
||||
}
|
||||
actions = append(actions, next)
|
||||
default:
|
||||
return actions
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// filterWatchActions applies the side effects of the given actions and returns
|
||||
// the ones that are candidates for triggering a task run.
|
||||
func (e *Executor) filterWatchActions(broker *fsbroker.FSBroker, actions []*fsbroker.FSAction) []*fsbroker.FSAction {
|
||||
filtered := make([]*fsbroker.FSAction, 0, len(actions))
|
||||
for _, action := range actions {
|
||||
if action.Subject == nil || action.Type == fsbroker.NoOp || action.Type == fsbroker.Chmod {
|
||||
continue
|
||||
}
|
||||
|
||||
e.Logger.VerboseErrf(logger.Magenta, "task: received watch event: %s: %s\n", action.Type, action.Subject.Path)
|
||||
|
||||
if action.Type == fsbroker.Remove || action.Type == fsbroker.Rename {
|
||||
e.watchedDirs.Delete(action.Subject.Path)
|
||||
if oldPath, ok := action.Properties["OldPath"].(string); ok {
|
||||
e.watchedDirs.Delete(oldPath)
|
||||
}
|
||||
}
|
||||
|
||||
if ShouldIgnore(action.Subject.Path) {
|
||||
e.Logger.VerboseErrf(logger.Magenta, "task: event skipped for being an ignored dir: %s\n", action.Subject.Path)
|
||||
continue
|
||||
}
|
||||
|
||||
if action.Subject.IsDir() && action.Type != fsbroker.Remove {
|
||||
if err := e.registerWatchedTree(broker, action.Subject.Path); err != nil {
|
||||
e.Logger.VerboseErrf(logger.Magenta, "task: failed to watch dir %s: %v\n", action.Subject.Path, err)
|
||||
}
|
||||
}
|
||||
|
||||
filtered = append(filtered, action)
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
func isRelevantWatchAction(action *fsbroker.FSAction, files []string) bool {
|
||||
if action.Subject.IsDir() || action.Type == fsbroker.Remove || action.Type == fsbroker.Rename {
|
||||
return true
|
||||
}
|
||||
return slices.Contains(files, filepath.ToSlash(action.Subject.Path))
|
||||
}
|
||||
|
||||
func (e *Executor) registerWatchedDirs(broker *fsbroker.FSBroker, calls ...*Call) error {
|
||||
files, err := e.collectSources(calls)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, f := range files {
|
||||
d := filepath.Dir(f)
|
||||
if isSet, ok := e.watchedDirs.Load(d); ok && isSet {
|
||||
continue
|
||||
}
|
||||
if ShouldIgnore(d) {
|
||||
continue
|
||||
}
|
||||
if err := w.Add(d); err != nil {
|
||||
if err := e.registerWatchedDir(broker, filepath.Dir(f)); err != nil {
|
||||
return err
|
||||
}
|
||||
e.watchedDirs.Store(d, true)
|
||||
relPath, _ := filepath.Rel(e.Dir, d)
|
||||
e.Logger.VerboseOutf(logger.Green, "task: watching new dir: %v\n", relPath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Executor) registerWatchedTree(broker *fsbroker.FSBroker, dir string) error {
|
||||
return filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
e.Logger.VerboseErrf(logger.Magenta, "task: failed to walk dir %s: %v\n", path, err)
|
||||
return filepath.SkipDir
|
||||
}
|
||||
if !d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if ShouldIgnore(path) {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return e.registerWatchedDir(broker, path)
|
||||
})
|
||||
}
|
||||
|
||||
func (e *Executor) registerWatchedDir(broker *fsbroker.FSBroker, d string) error {
|
||||
if isSet, ok := e.watchedDirs.Load(d); ok && isSet {
|
||||
return nil
|
||||
}
|
||||
if ShouldIgnore(d) {
|
||||
return nil
|
||||
}
|
||||
if err := broker.AddWatch(d); err != nil {
|
||||
return err
|
||||
}
|
||||
e.watchedDirs.Store(d, true)
|
||||
relPath, _ := filepath.Rel(e.Dir, d)
|
||||
e.Logger.VerboseOutf(logger.Green, "task: watching new dir: %v\n", relPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
var ignorePaths = []string{
|
||||
"/.task",
|
||||
"/.git",
|
||||
|
|
|
|||
153
watch_test.go
153
watch_test.go
|
|
@ -8,6 +8,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -18,6 +19,23 @@ import (
|
|||
"github.com/go-task/task/v3/internal/filepathext"
|
||||
)
|
||||
|
||||
type syncBuffer struct {
|
||||
mu sync.Mutex
|
||||
buf bytes.Buffer
|
||||
}
|
||||
|
||||
func (b *syncBuffer) Write(p []byte) (int, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return b.buf.Write(p)
|
||||
}
|
||||
|
||||
func (b *syncBuffer) String() string {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return b.buf.String()
|
||||
}
|
||||
|
||||
func TestFileWatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
@ -80,6 +98,141 @@ task: task "default" finished running
|
|||
assert.Equal(t, expectedOutput, strings.TrimSpace(buff.String()))
|
||||
}
|
||||
|
||||
// TestFileWatchNewDir checks that a directory created while watching is
|
||||
// watched without having to wait for the periodic sources rescan.
|
||||
func TestFileWatchNewDir(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const dir = "testdata/watch_newdir"
|
||||
_ = os.RemoveAll(filepathext.SmartJoin(dir, ".task"))
|
||||
_ = os.RemoveAll(filepathext.SmartJoin(dir, "src"))
|
||||
|
||||
var buff syncBuffer
|
||||
e := task.NewExecutor(
|
||||
task.WithDir(dir),
|
||||
task.WithStdout(&buff),
|
||||
task.WithStderr(&buff),
|
||||
task.WithWatch(true),
|
||||
)
|
||||
require.NoError(t, e.Setup())
|
||||
|
||||
dirPath := filepathext.SmartJoin(dir, "src")
|
||||
require.NoError(t, os.MkdirAll(dirPath, 0o755))
|
||||
require.NoError(t, os.WriteFile(filepathext.SmartJoin(dirPath, "a"), []byte("test"), 0o644))
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
if err := e.Run(ctx, &task.Call{Task: "default"}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
|
||||
newDirPath := filepathext.SmartJoin(dirPath, "sub")
|
||||
require.NoError(t, os.MkdirAll(newDirPath, 0o755))
|
||||
require.NoError(t, os.WriteFile(filepathext.SmartJoin(newDirPath, "b"), []byte("test"), 0o644))
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
return strings.Count(buff.String(), `"default" finished running`) >= 2
|
||||
}, 2*time.Second, 50*time.Millisecond)
|
||||
|
||||
cancel()
|
||||
}
|
||||
|
||||
// TestFileWatchSources checks that changes to files that are not task sources
|
||||
// do not trigger a run.
|
||||
func TestFileWatchSources(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const dir = "testdata/watch_sources"
|
||||
_ = os.RemoveAll(filepathext.SmartJoin(dir, ".task"))
|
||||
_ = os.RemoveAll(filepathext.SmartJoin(dir, "src"))
|
||||
|
||||
expectedOutput := strings.TrimSpace(`
|
||||
task: Started watching for tasks: default
|
||||
task: [default] echo "Task running!"
|
||||
Task running!
|
||||
task: task "default" finished running
|
||||
`)
|
||||
|
||||
var buff syncBuffer
|
||||
e := task.NewExecutor(
|
||||
task.WithDir(dir),
|
||||
task.WithStdout(&buff),
|
||||
task.WithStderr(&buff),
|
||||
task.WithWatch(true),
|
||||
)
|
||||
require.NoError(t, e.Setup())
|
||||
|
||||
dirPath := filepathext.SmartJoin(dir, "src")
|
||||
require.NoError(t, os.MkdirAll(dirPath, 0o755))
|
||||
require.NoError(t, os.WriteFile(filepathext.SmartJoin(dirPath, "a.txt"), []byte("test"), 0o644))
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
if err := e.Run(ctx, &task.Call{Task: "default"}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
|
||||
require.NoError(t, os.WriteFile(filepathext.SmartJoin(dirPath, "notes.md"), []byte("other"), 0o644))
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
cancel()
|
||||
assert.Equal(t, expectedOutput, strings.TrimSpace(buff.String()))
|
||||
}
|
||||
|
||||
// TestFileWatchRemove checks that removing a source file triggers a run.
|
||||
func TestFileWatchRemove(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const dir = "testdata/watch_remove"
|
||||
_ = os.RemoveAll(filepathext.SmartJoin(dir, ".task"))
|
||||
_ = os.RemoveAll(filepathext.SmartJoin(dir, "src"))
|
||||
|
||||
expectedOutput := strings.TrimSpace(`
|
||||
task: Started watching for tasks: default
|
||||
task: [default] echo "Task running!"
|
||||
Task running!
|
||||
task: task "default" finished running
|
||||
task: [default] echo "Task running!"
|
||||
Task running!
|
||||
task: task "default" finished running
|
||||
`)
|
||||
|
||||
var buff syncBuffer
|
||||
e := task.NewExecutor(
|
||||
task.WithDir(dir),
|
||||
task.WithStdout(&buff),
|
||||
task.WithStderr(&buff),
|
||||
task.WithWatch(true),
|
||||
)
|
||||
require.NoError(t, e.Setup())
|
||||
|
||||
dirPath := filepathext.SmartJoin(dir, "src")
|
||||
filePath := filepathext.SmartJoin(dirPath, "a")
|
||||
require.NoError(t, os.MkdirAll(dirPath, 0o755))
|
||||
require.NoError(t, os.WriteFile(filePath, []byte("test"), 0o644))
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
if err := e.Run(ctx, &task.Call{Task: "default"}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
|
||||
require.NoError(t, os.Remove(filePath))
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
cancel()
|
||||
assert.Equal(t, expectedOutput, strings.TrimSpace(buff.String()))
|
||||
}
|
||||
|
||||
func TestShouldIgnore(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue