-
-
Notifications
You must be signed in to change notification settings - Fork 666
/
Copy pathwatch_test.go
101 lines (82 loc) · 1.99 KB
/
watch_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//go:build watch
// +build watch
package task_test
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-task/task/v3"
"github.com/go-task/task/v3/internal/filepathext"
)
func TestFileWatch(t *testing.T) {
t.Parallel()
const dir = "testdata/watch"
_ = 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: Task "default" is up to date
task: task "default" finished running
`)
var buff bytes.Buffer
e := task.NewExecutor(
task.ExecutorWithDir(dir),
task.ExecutorWithStdout(&buff),
task.ExecutorWithStderr(&buff),
task.ExecutorWithWatch(true),
)
require.NoError(t, e.Setup())
buff.Reset()
dirPath := filepathext.SmartJoin(dir, "src")
filePath := filepathext.SmartJoin(dirPath, "a")
err := os.MkdirAll(dirPath, 0755)
require.NoError(t, err)
err = os.WriteFile(filePath, []byte("test"), 0644)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
go func() {
for {
select {
case <-ctx.Done():
return
default:
err := e.Run(ctx, &task.Call{Task: "default"})
if err != nil {
panic(err)
}
}
}
}()
time.Sleep(10 * time.Millisecond)
err = os.WriteFile(filePath, []byte("test updated"), 0644)
require.NoError(t, err)
time.Sleep(150 * time.Millisecond)
cancel()
assert.Equal(t, expectedOutput, strings.TrimSpace(buff.String()))
}
func TestShouldIgnoreFile(t *testing.T) {
t.Parallel()
tt := []struct {
path string
expect bool
}{
{"/.git/hooks", true},
{"/.github/workflows/build.yaml", false},
}
for k, ct := range tt {
ct := ct
t.Run(fmt.Sprintf("ignore - %d", k), func(t *testing.T) {
t.Parallel()
require.Equal(t, task.ShouldIgnoreFile(ct.path), ct.expect)
})
}
}