Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ tasks:
cmds:
- go test ./...

test:e2e:
desc: Runs E2E test suite
aliases: [t]
sources:
- "**/*.go"
- "./cmd/task/testdata/**/*"
cmds:
- go test ./cmd/task/... -tags test_e2e

test:watch:
desc: Runs test suite with watch tests included
deps: [sleepit:build]
Expand Down
5 changes: 5 additions & 0 deletions cmd/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
)

func main() {
os.Exit(main_())
}

func main_() (rc int) {
if err := run(); err != nil {
l := &logger.Logger{
Stdout: os.Stdout,
Expand All @@ -39,6 +43,7 @@ func main() {
os.Exit(errors.CodeUnknown)
}
os.Exit(errors.CodeOk)
return
}

func run() error {
Expand Down
59 changes: 59 additions & 0 deletions cmd/task/task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//go:build test_e2e
// +build test_e2e

package main

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/rogpeppe/go-internal/testscript"
)

func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"task": main_,
}))
}

func TestE2E(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata",
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
"sleep": sleep,
"touch": touch,
},
Setup: func(e *testscript.Env) error { return nil },
})
}

func sleep(ts *testscript.TestScript, neg bool, args []string) {
duration := time.Second
if len(args) == 1 {
d, err := time.ParseDuration(args[0])
ts.Check(err)
duration = d
}
time.Sleep(duration)
}

func touch(ts *testscript.TestScript, neg bool, args []string) {
if len(args) != 1 {
ts.Fatalf("touch <file>")
}
// Get the relative path to the scripts current directory.
path := ts.MkAbs(args[0])
// Create the file (if necessary).
err := os.MkdirAll(filepath.Dir(path), 0750)
ts.Check(err)
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
ts.Check(err)
err = file.Close()
ts.Check(err)
// Now update the timestamp.
t := time.Now()
err = os.Chtimes(path, t, t)
ts.Check(err)
}
28 changes: 28 additions & 0 deletions cmd/task/testdata/cli.flags.init.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Test: task init
task --init
exists Taskfile.yml
stdout 'Taskfile created: Taskfile.yml'
cmp Taskfile.yml expect

# Test: taskfile exists, content not modified
exec sh -c 'echo "foo" > Taskfile.yml'
! task --init
exists Taskfile.yml
stderr 'task: A Taskfile already exists'
exec cat Taskfile.yml
stdout 'foo'


-- expect --
# https://taskfile.dev

version: '3'

vars:
GREETING: Hello, World!

tasks:
default:
cmds:
- echo "{{.GREETING}}"
silent: true
31 changes: 31 additions & 0 deletions cmd/task/testdata/cli.flags.interval.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mkdir src

# Test with extended interval, change should not be detected.
task --interval=1200ms &
sleep 500ms
touch src/a
sleep 500ms
kill -INT
wait
cmp stdout expect_stdout
cmp stderr expect_stderr


-- Taskfile.yml --
version: '3'
tasks:
default:
watch: true
sources:
- 'src/*'
cmds:
- echo "Task running!"
- sleep 2

-- expect_stdout --
Task running!
task: Signal received: "interrupt"
-- expect_stderr --
task: Started watching for tasks: default
task: [default] echo "Task running!"
task: [default] sleep 2
31 changes: 31 additions & 0 deletions cmd/task/testdata/cli.flags.watch.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mkdir src
task --watch &
sleep 500ms
touch src/a
sleep 500ms
kill -INT
wait
cmp stdout expect_stdout
cmp stderr expect_stderr


-- Taskfile.yml --
version: '3'
tasks:
default:
sources:
- 'src/*'
cmds:
- echo "Task running!"
- sleep 2

-- expect_stdout --
Task running!
Task running!
task: Signal received: "interrupt"
-- expect_stderr --
task: Started watching for tasks: default
task: [default] echo "Task running!"
task: [default] sleep 2
task: [default] echo "Task running!"
task: [default] sleep 2
32 changes: 32 additions & 0 deletions cmd/task/testdata/watch.interval.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
mkdir src

# Test with extended interval, change should not be detected.
task &
sleep 500ms
touch src/a
sleep 500ms
kill -INT
wait
cmp stdout expect_stdout
cmp stderr expect_stderr


-- Taskfile.yml --
version: '3'
interval: 1200ms
tasks:
default:
watch: true
sources:
- "src/*"
cmds:
- echo "Task running!"
- sleep 2

-- expect_stdout --
Task running!
task: Signal received: "interrupt"
-- expect_stderr --
task: Started watching for tasks: default
task: [default] echo "Task running!"
task: [default] sleep 2
79 changes: 79 additions & 0 deletions cmd/task/testdata/watch.service.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Build the service.
mkdir $WORK/.cache/go-build
env GOCACHE=$WORK/.cache/go-build
task build

# Test that the service is restarted when watched file changed.
task run &
sleep 0.5s
touch watch.file
sleep 0.5s
kill -INT
wait
stdout 'Service received signal: interrupt'
stdout 'Starting Service'
stdout 'Wait for signal'
grep -count=1 'Service received signal: interrupt'
grep -count=2 'Starting Service'
grep -count=2 'Wait for signal'

# Test that the service is _not_ restarted when an excluded file is changed.
task -s run &
sleep 0.5s
touch watch.exclude
sleep 0.5s
kill -INT
wait
! stdout 'Service received signal: interrupt'
stdout 'Starting Service'
stdout 'Wait for signal'
! grep 'Service received signal: interrupt'
grep -count=1 'Starting Service'
grep -count=1 'Wait for signal'


-- Taskfile.yml --

version: '3'
tasks:
provision:
cmds:
- touch watch.exclude
build:
sources:
- 'service.go'
generates:
- './out/service'
cmds:
- rm -rf ./out
- task: provision
- go build -o ./out/ service.go
run:
deps: [run:service]
watch: true
sources:
- 'watch.file'
- exclude: 'watch.exclude'
method: none
run:service:
cmds:
- './out/service'


-- service.go --
package main

import (
"fmt"
"os"
"os/signal"
)

func main() {
fmt.Println("Starting Service ...")
c := make (chan os.Signal, 1)
signal.Notify(c)
fmt.Println("Wait for signal ...")
s := <-c
fmt.Println("Service received signal:", s)
}
30 changes: 30 additions & 0 deletions cmd/task/testdata/watch.sources.exclude.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
mkdir src
task &
sleep 500ms
touch src/foo
sleep 500ms
kill -INT
wait
cmp stdout expect_stdout
cmp stderr expect_stderr


-- Taskfile.yml --
version: '3'
tasks:
default:
watch: true
sources:
- "src/*"
- exclude: 'src/foo'
cmds:
- echo "Task running!"
- sleep 2

-- expect_stdout --
Task running!
task: Signal received: "interrupt"
-- expect_stderr --
task: Started watching for tasks: default
task: [default] echo "Task running!"
task: [default] sleep 2
47 changes: 47 additions & 0 deletions cmd/task/testdata/watch.sources.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
mkdir src

# Test with default conditions, change should be detected.
task &
sleep 500ms
touch src/a
sleep 500ms
kill -INT
wait
cmp stdout expect_stdout
cmp stderr expect_stderr

# Test with duplicate events, task should be restated only once.
task &
sleep 500ms
touch src/a
touch src/b
touch src/c
touch src/d
sleep 500ms
kill -INT
wait
cmp stdout expect_stdout
cmp stderr expect_stderr


-- Taskfile.yml --
version: '3'
tasks:
default:
watch: true
sources:
- 'src/*'
cmds:
- echo "Task running!"
- sleep 2

-- expect_stdout --
Task running!
Task running!
task: Signal received: "interrupt"
-- expect_stderr --
task: Started watching for tasks: default
task: [default] echo "Task running!"
task: [default] sleep 2
task: [default] echo "Task running!"
task: [default] sleep 2
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ require (
github.com/otiai10/mint v1.6.3 // indirect
github.com/pjbgf/sha1cd v0.3.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.3.1 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/tools v0.27.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Loading