From 1b30c9dbcaba51d3424801cecdf35a17a7e60d9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20H=C3=B8gstedt=20Danquah?= Date: Thu, 6 Apr 2023 03:18:58 +0200 Subject: [PATCH] Make status checks respect task and global silent mode (#1107) --- task.go | 2 +- task_test.go | 41 ++++++++++++++++++++++++++++++++++++ testdata/status/Taskfile.yml | 9 +++++++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/task.go b/task.go index e85441f5..fb039729 100644 --- a/task.go +++ b/task.go @@ -176,7 +176,7 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error { } if upToDate && preCondMet { - if !e.Silent { + if e.Verbose || (!t.Silent && !e.Taskfile.Silent && !e.Silent) { e.Logger.Errf(logger.Magenta, `task: Task "%s" is up to date`, t.Name()) } return nil diff --git a/task_test.go b/task_test.go index b98cc526..b261423d 100644 --- a/task_test.go +++ b/task_test.go @@ -297,6 +297,7 @@ func TestStatus(t *testing.T) { files := []string{ "foo.txt", "bar.txt", + "baz.txt", } for _, f := range files { @@ -316,8 +317,16 @@ func TestStatus(t *testing.T) { Silent: true, } assert.NoError(t, e.Setup()) + // gen-foo creates foo.txt, and will always fail it's status check. assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-foo"})) + // gen-foo creates bar.txt, and will pass its status-check the 3. time it + // is run. It creates bar.txt, but also lists it as its source. So, the checksum + // for the file won't match before after the second run as we the file + // only exists after the first run. assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-bar"})) + // gen-silent-baz is marked as being silent, and should only produce output + // if e.Verbose is set to true. + assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-silent-baz"})) for _, f := range files { if _, err := os.Stat(filepathext.SmartJoin(dir, f)); err != nil { @@ -325,6 +334,24 @@ func TestStatus(t *testing.T) { } } + // Run gen-bar a second time to produce a checksum file that matches bar.txt + assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-bar"})) + + // Run gen-bar a third time, to make sure we've triggered the status check. + assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-bar"})) + + // We're silent, so no output should have been produced. + assert.Empty(t, buff.String()) + + // Now, let's remove source file, and run the task again to to prepare + // for the next test. + err := os.Remove(filepathext.SmartJoin(dir, "bar.txt")) + assert.NoError(t, err) + assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-bar"})) + buff.Reset() + + // Global silence switched of, so we should see output unless the task itself + // is silent. e.Silent = false // all: not up-to-date @@ -344,6 +371,20 @@ func TestStatus(t *testing.T) { assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-bar"})) assert.Equal(t, `task: Task "gen-bar" is up to date`, strings.TrimSpace(buff.String())) buff.Reset() + + // sources: not up-to-date, no output produced. + assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-silent-baz"})) + assert.Empty(t, buff.String()) + + // up-to-date, no output produced + assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-silent-baz"})) + assert.Empty(t, buff.String()) + + e.Verbose = true + // up-to-date, output produced due to Verbose mode. + assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "gen-silent-baz"})) + assert.Equal(t, `task: Task "gen-silent-baz" is up to date`, strings.TrimSpace(buff.String())) + buff.Reset() } func TestPrecondition(t *testing.T) { diff --git a/testdata/status/Taskfile.yml b/testdata/status/Taskfile.yml index c7ddfcb5..fcf79957 100644 --- a/testdata/status/Taskfile.yml +++ b/testdata/status/Taskfile.yml @@ -14,5 +14,12 @@ tasks: - touch bar.txt sources: - ./bar.txt - status: + status: - test 1 = 1 + + gen-silent-baz: + silent: true + cmds: + - touch baz.txt + sources: + - ./baz.txt