mirror of
https://github.com/go-task/task.git
synced 2025-04-23 12:18:57 +02:00
102 lines
2.0 KiB
Go
102 lines
2.0 KiB
Go
//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)
|
|
})
|
|
}
|
|
}
|