From 99ab2a4d6241813cf1c4f3389ac5c9fe7002f90f Mon Sep 17 00:00:00 2001 From: Harel Wahnich <63955749+harelwa@users.noreply.github.com> Date: Mon, 6 Mar 2023 08:16:41 +0200 Subject: [PATCH] for task up to date check both status and sources (#1035) * remove redundant if statement * add subtests to TestStatusChecksum --- status.go | 13 ++++---- task_test.go | 59 +++++++++++++++++++--------------- testdata/checksum/Taskfile.yml | 9 ++++++ 3 files changed, 49 insertions(+), 32 deletions(-) diff --git a/status.go b/status.go index 2c2f118b..457c24e6 100644 --- a/status.go +++ b/status.go @@ -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) { - if len(t.Status) == 0 && len(t.Sources) == 0 { - return false, nil - } + isUpToDateStatus := true + isUpToDateChecker := true if len(t.Status) > 0 { isUpToDate, err := e.isTaskUpToDateStatus(ctx, t) @@ -39,7 +38,7 @@ func (e *Executor) isTaskUpToDate(ctx context.Context, t *taskfile.Task) (bool, return false, err } 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 } if !isUpToDate { - return false, nil + isUpToDateChecker = false } } - return true, nil + isUpToDate := isUpToDateStatus && isUpToDateChecker + + return isUpToDate, nil } func (e *Executor) statusOnError(t *taskfile.Task) error { diff --git a/task_test.go b/task_test.go index 8ddc84c9..d26592f7 100644 --- a/task_test.go +++ b/task_test.go @@ -447,36 +447,43 @@ func TestGenerates(t *testing.T) { func TestStatusChecksum(t *testing.T) { const dir = "testdata/checksum" - files := []string{ - "generated.txt", - ".task/checksum/build", + tests := []struct { + files []string + task string + }{ + {[]string{"generated.txt", ".task/checksum/build"}, "build"}, + {[]string{"generated.txt", ".task/checksum/build-with-status"}, "build-with-status"}, } - for _, f := range files { - _ = os.Remove(filepathext.SmartJoin(dir, f)) + for _, test := range tests { + 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)) - assert.Error(t, err) + _, err := os.Stat(filepathext.SmartJoin(dir, f)) + 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) { diff --git a/testdata/checksum/Taskfile.yml b/testdata/checksum/Taskfile.yml index d2de1086..29ef7b70 100644 --- a/testdata/checksum/Taskfile.yml +++ b/testdata/checksum/Taskfile.yml @@ -10,3 +10,12 @@ tasks: generates: - ./generated.txt method: checksum + + build-with-status: + cmds: + - cp ./source.txt ./generated.txt + sources: + - ./source.txt + status: + - test -f ./generated.txt +