1
0
mirror of https://github.com/go-task/task.git synced 2025-11-23 22:24:45 +02:00

for task up to date check both status and sources (#1035)

* remove redundant if statement

* add subtests to TestStatusChecksum
This commit is contained in:
Harel Wahnich
2023-03-06 08:16:41 +02:00
committed by GitHub
parent d4ed7c3cfc
commit 99ab2a4d62
3 changed files with 49 additions and 32 deletions

View File

@@ -29,9 +29,8 @@ func (e *Executor) Status(ctx context.Context, calls ...taskfile.Call) error {
} }
func (e *Executor) isTaskUpToDate(ctx context.Context, t *taskfile.Task) (bool, error) { func (e *Executor) isTaskUpToDate(ctx context.Context, t *taskfile.Task) (bool, error) {
if len(t.Status) == 0 && len(t.Sources) == 0 { isUpToDateStatus := true
return false, nil isUpToDateChecker := true
}
if len(t.Status) > 0 { if len(t.Status) > 0 {
isUpToDate, err := e.isTaskUpToDateStatus(ctx, t) isUpToDate, err := e.isTaskUpToDateStatus(ctx, t)
@@ -39,7 +38,7 @@ func (e *Executor) isTaskUpToDate(ctx context.Context, t *taskfile.Task) (bool,
return false, err return false, err
} }
if !isUpToDate { if !isUpToDate {
return false, nil isUpToDateStatus = false
} }
} }
@@ -53,11 +52,13 @@ func (e *Executor) isTaskUpToDate(ctx context.Context, t *taskfile.Task) (bool,
return false, err return false, err
} }
if !isUpToDate { if !isUpToDate {
return false, nil isUpToDateChecker = false
} }
} }
return true, nil isUpToDate := isUpToDateStatus && isUpToDateChecker
return isUpToDate, nil
} }
func (e *Executor) statusOnError(t *taskfile.Task) error { func (e *Executor) statusOnError(t *taskfile.Task) error {

View File

@@ -447,36 +447,43 @@ func TestGenerates(t *testing.T) {
func TestStatusChecksum(t *testing.T) { func TestStatusChecksum(t *testing.T) {
const dir = "testdata/checksum" const dir = "testdata/checksum"
files := []string{ tests := []struct {
"generated.txt", files []string
".task/checksum/build", task string
}{
{[]string{"generated.txt", ".task/checksum/build"}, "build"},
{[]string{"generated.txt", ".task/checksum/build-with-status"}, "build-with-status"},
} }
for _, f := range files { for _, test := range tests {
_ = os.Remove(filepathext.SmartJoin(dir, f)) t.Run(test.task, func(t *testing.T) {
for _, f := range test.files {
_ = os.Remove(filepathext.SmartJoin(dir, f))
_, err := os.Stat(filepathext.SmartJoin(dir, f)) _, err := os.Stat(filepathext.SmartJoin(dir, f))
assert.Error(t, err) assert.Error(t, err)
}
var buff bytes.Buffer
e := task.Executor{
Dir: dir,
TempDir: filepathext.SmartJoin(dir, ".task"),
Stdout: &buff,
Stderr: &buff,
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: test.task}))
for _, f := range test.files {
_, err := os.Stat(filepathext.SmartJoin(dir, f))
assert.NoError(t, err)
}
buff.Reset()
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: test.task}))
assert.Equal(t, `task: Task "`+test.task+`" is up to date`+"\n", buff.String())
})
} }
var buff bytes.Buffer
e := task.Executor{
Dir: dir,
TempDir: filepathext.SmartJoin(dir, ".task"),
Stdout: &buff,
Stderr: &buff,
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"}))
for _, f := range files {
_, err := os.Stat(filepathext.SmartJoin(dir, f))
assert.NoError(t, err)
}
buff.Reset()
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"}))
assert.Equal(t, `task: Task "build" is up to date`+"\n", buff.String())
} }
func TestAlias(t *testing.T) { func TestAlias(t *testing.T) {

View File

@@ -10,3 +10,12 @@ tasks:
generates: generates:
- ./generated.txt - ./generated.txt
method: checksum method: checksum
build-with-status:
cmds:
- cp ./source.txt ./generated.txt
sources:
- ./source.txt
status:
- test -f ./generated.txt