2022-12-17 16:08:41 +02:00
|
|
|
//go:build watch
|
|
|
|
// +build watch
|
|
|
|
|
|
|
|
package task_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-10-07 23:38:14 +02:00
|
|
|
"fmt"
|
2022-12-17 16:08:41 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-08-10 02:12:50 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-12-17 16:08:41 +02:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3"
|
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2023-12-29 22:32:03 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile/ast"
|
2022-12-17 16:08:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileWatcherInterval(t *testing.T) {
|
|
|
|
const dir = "testdata/watcher_interval"
|
|
|
|
expectedOutput := strings.TrimSpace(`
|
|
|
|
task: Started watching for tasks: default
|
|
|
|
task: [default] echo "Hello, World!"
|
|
|
|
Hello, World!
|
|
|
|
task: [default] echo "Hello, World!"
|
|
|
|
Hello, World!
|
|
|
|
`)
|
|
|
|
|
|
|
|
var buff bytes.Buffer
|
|
|
|
e := &task.Executor{
|
|
|
|
Dir: dir,
|
|
|
|
Stdout: &buff,
|
|
|
|
Stderr: &buff,
|
|
|
|
Watch: true,
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:18:41 +02:00
|
|
|
require.NoError(t, e.Setup())
|
2022-12-17 16:08:41 +02:00
|
|
|
buff.Reset()
|
|
|
|
|
|
|
|
err := os.MkdirAll(filepathext.SmartJoin(dir, "src"), 0755)
|
2023-04-06 12:18:41 +02:00
|
|
|
require.NoError(t, err)
|
2022-12-17 16:08:41 +02:00
|
|
|
|
|
|
|
err = os.WriteFile(filepathext.SmartJoin(dir, "src/a"), []byte("test"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
go func(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2024-04-09 02:45:07 +02:00
|
|
|
err := e.Run(ctx, &ast.Call{Task: "default"})
|
2022-12-17 16:08:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(ctx)
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
err = os.WriteFile(filepathext.SmartJoin(dir, "src/a"), []byte("test updated"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
time.Sleep(700 * time.Millisecond)
|
|
|
|
cancel()
|
|
|
|
assert.Equal(t, expectedOutput, strings.TrimSpace(buff.String()))
|
|
|
|
buff.Reset()
|
|
|
|
err = os.RemoveAll(filepathext.SmartJoin(dir, ".task"))
|
2023-04-06 12:18:41 +02:00
|
|
|
require.NoError(t, err)
|
2022-12-17 16:08:41 +02:00
|
|
|
err = os.RemoveAll(filepathext.SmartJoin(dir, "src"))
|
2023-04-06 12:18:41 +02:00
|
|
|
require.NoError(t, err)
|
2022-12-17 16:08:41 +02:00
|
|
|
}
|
2023-10-07 23:38:14 +02:00
|
|
|
|
|
|
|
func TestShouldIgnoreFile(t *testing.T) {
|
|
|
|
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()
|
2024-04-09 02:45:07 +02:00
|
|
|
require.Equal(t, task.ShouldIgnoreFile(ct.path), ct.expect)
|
2023-10-07 23:38:14 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|