1
0
mirror of https://github.com/go-task/task.git synced 2025-01-12 04:34:11 +02:00

Merge branch 'status'

This commit is contained in:
Andrey Nering 2017-05-17 14:43:36 -03:00
commit b590e74ce6
5 changed files with 74 additions and 0 deletions

View File

@ -199,6 +199,22 @@ will compare the modification date/time of the files to determine if it's
necessary to run the task. If not, it will just print
`Task "js" is up to date`.
Alternatively, you can inform a sequence of tests as `status`. If no error
is returned (exit status 0), the task is considered up-to-date:
```yml
generate-files:
cmds:
- mkdir directory
- touch directory/file1.txt
- touch directory/file2.txt
# test existence of files
status:
- test -d directory
- test -f directory/file1.txt
- test -f directory/file2.txt
```
You can use `--force` or `-f` if you want to force a task to run even when
up-to-date.

20
task.go
View File

@ -34,6 +34,7 @@ type Task struct {
Desc string
Sources []string
Generates []string
Status []string
Dir string
Vars map[string]string
Set string
@ -140,6 +141,25 @@ func (t *Task) runDeps(ctx context.Context) error {
}
func (t *Task) isUpToDate() (bool, error) {
if len(t.Status) > 0 {
environ, err := t.getEnviron()
if err != nil {
return false, err
}
for _, s := range t.Status {
err = execext.RunCommand(&execext.RunCommandOptions{
Command: s,
Dir: t.Dir,
Env: environ,
})
if err != nil {
return false, nil
}
}
return true, nil
}
if len(t.Sources) == 0 || len(t.Generates) == 0 {
return false, nil
}

View File

@ -1,6 +1,7 @@
package task_test
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
@ -112,3 +113,34 @@ func TestTaskCall(t *testing.T) {
}
}
}
func TestStatus(t *testing.T) {
const dir = "testdata/status"
var file = filepath.Join(dir, "foo.txt")
_ = os.Remove(file)
if _, err := os.Stat(file); err == nil {
t.Errorf("File should not exists: %v", err)
}
c := exec.Command("task", "gen-foo")
c.Dir = dir
if err := c.Run(); err != nil {
t.Error(err)
}
if _, err := os.Stat(file); err != nil {
t.Errorf("File should exists: %v", err)
}
buff := bytes.NewBuffer(nil)
c = exec.Command("task", "gen-foo")
c.Dir = dir
c.Stderr = buff
c.Stdout = buff
if err := c.Run(); err != nil {
t.Error(err)
}
if buff.String() != `task: Task "gen-foo" is up to date`+"\n" {
t.Errorf("Wrong output message: %s", buff.String())
}
}

1
testdata/status/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.txt

5
testdata/status/Taskfile.yml vendored Normal file
View File

@ -0,0 +1,5 @@
gen-foo:
cmds:
- touch foo.txt
status:
- test -f foo.txt