From 2a2dfce1378e18341fabfd489b5ca4f3d146c67c Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Wed, 17 May 2017 14:37:11 -0300 Subject: [PATCH] Add status option to prevent task from running Closes #27 --- task.go | 20 ++++++++++++++++++++ task_test.go | 32 ++++++++++++++++++++++++++++++++ testdata/status/.gitignore | 1 + testdata/status/Taskfile.yml | 5 +++++ 4 files changed, 58 insertions(+) create mode 100644 testdata/status/.gitignore create mode 100644 testdata/status/Taskfile.yml diff --git a/task.go b/task.go index 330e60c4..8e824f0e 100644 --- a/task.go +++ b/task.go @@ -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 } diff --git a/task_test.go b/task_test.go index 8ce72515..6aa562d4 100644 --- a/task_test.go +++ b/task_test.go @@ -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()) + } +} diff --git a/testdata/status/.gitignore b/testdata/status/.gitignore new file mode 100644 index 00000000..2211df63 --- /dev/null +++ b/testdata/status/.gitignore @@ -0,0 +1 @@ +*.txt diff --git a/testdata/status/Taskfile.yml b/testdata/status/Taskfile.yml new file mode 100644 index 00000000..e3a69232 --- /dev/null +++ b/testdata/status/Taskfile.yml @@ -0,0 +1,5 @@ +gen-foo: + cmds: + - touch foo.txt + status: + - test -f foo.txt