1
0
mirror of https://github.com/go-task/task.git synced 2025-08-10 22:42:19 +02:00

fix: ensure that calls to other tasks can be silenced (#680)

This commit is contained in:
Mads H. Danquah
2023-04-27 08:23:45 +02:00
committed by Andrey Nering
parent f0e9751f7e
commit 9a406f5998
7 changed files with 142 additions and 13 deletions

View File

@@ -177,7 +177,7 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
} }
if upToDate && preCondMet { if upToDate && preCondMet {
if e.Verbose || (!t.Silent && !e.Taskfile.Silent && !e.Silent) { if e.Verbose || (!call.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) {
e.Logger.Errf(logger.Magenta, "task: Task %q is up to date\n", t.Name()) e.Logger.Errf(logger.Magenta, "task: Task %q is up to date\n", t.Name())
} }
return nil return nil
@@ -237,9 +237,8 @@ func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
for _, d := range t.Deps { for _, d := range t.Deps {
d := d d := d
g.Go(func() error { g.Go(func() error {
err := e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars}) err := e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars, Silent: d.Silent})
if err != nil { if err != nil {
return err return err
} }
@@ -267,7 +266,7 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
reacquire := e.releaseConcurrencyLimit() reacquire := e.releaseConcurrencyLimit()
defer reacquire() defer reacquire()
err := e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars}) err := e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars, Silent: cmd.Silent})
if err != nil { if err != nil {
return err return err
} }
@@ -278,7 +277,7 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
return nil return nil
} }
if e.Verbose || (!cmd.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) { if e.Verbose || (!call.Silent && !cmd.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) {
e.Logger.Errf(logger.Green, "task: [%s] %s\n", t.Name(), cmd.Cmd) e.Logger.Errf(logger.Green, "task: [%s] %s\n", t.Name(), cmd.Cmd)
} }

View File

@@ -1892,3 +1892,77 @@ func TestSplitArgs(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "3\n", buff.String()) assert.Equal(t, "3\n", buff.String())
} }
func TestSilence(t *testing.T) {
var buff bytes.Buffer
e := task.Executor{
Dir: "testdata/silent",
Stdout: &buff,
Stderr: &buff,
Silent: false,
}
require.NoError(t, e.Setup())
// First verify that the silent flag is in place.
task, err := e.GetTask(taskfile.Call{Task: "task-test-silent-calls-chatty-silenced"})
require.NoError(t, err, "Unable to look up task task-test-silent-calls-chatty-silenced")
require.True(t, task.Cmds[0].Silent, "The task task-test-silent-calls-chatty-silenced should have a silent call to chatty")
// Then test the two basic cases where the task is silent or not.
// A silenced task.
err = e.Run(context.Background(), taskfile.Call{Task: "silent"})
require.NoError(t, err)
require.Empty(t, buff.String(), "siWhile running lent: Expected not see output, because the task is silent")
buff.Reset()
// A chatty (not silent) task.
err = e.Run(context.Background(), taskfile.Call{Task: "chatty"})
require.NoError(t, err)
require.NotEmpty(t, buff.String(), "chWhile running atty: Expected to see output, because the task is not silent")
buff.Reset()
// Then test invoking the two task from other tasks.
// A silenced task that calls a chatty task.
err = e.Run(context.Background(), taskfile.Call{Task: "task-test-silent-calls-chatty-non-silenced"})
require.NoError(t, err)
require.NotEmpty(t, buff.String(), "While running task-test-silent-calls-chatty-non-silenced: Expected to see output. The task is silenced, but the called task is not. Silence does not propagate to called tasks.")
buff.Reset()
// A silent task that does a silent call to a chatty task.
err = e.Run(context.Background(), taskfile.Call{Task: "task-test-silent-calls-chatty-silenced"})
require.NoError(t, err)
require.Empty(t, buff.String(), "While running task-test-silent-calls-chatty-silenced: Expected not to see output. The task calls chatty task, but the call is silenced.")
buff.Reset()
// A chatty task that does a call to a chatty task.
err = e.Run(context.Background(), taskfile.Call{Task: "task-test-chatty-calls-chatty-non-silenced"})
require.NoError(t, err)
require.NotEmpty(t, buff.String(), "While running task-test-chatty-calls-chatty-non-silenced: Expected to see output. Both caller and callee are chatty and not silenced.")
buff.Reset()
// A chatty task that does a silenced call to a chatty task.
err = e.Run(context.Background(), taskfile.Call{Task: "task-test-chatty-calls-chatty-silenced"})
require.NoError(t, err)
require.NotEmpty(t, buff.String(), "While running task-test-chatty-calls-chatty-silenced: Expected to see output. Call to a chatty task is silenced, but the parent task is not.")
buff.Reset()
// A chatty task with no cmd's of its own that does a silenced call to a chatty task.
err = e.Run(context.Background(), taskfile.Call{Task: "task-test-no-cmds-calls-chatty-silenced"})
require.NoError(t, err)
require.Empty(t, buff.String(), "While running task-test-no-cmds-calls-chatty-silenced: Expected not to see output. While the task itself is not silenced, it does not have any cmds and only does an invocation of a silenced task.")
buff.Reset()
// A chatty task that does a silenced invocation of a task.
err = e.Run(context.Background(), taskfile.Call{Task: "task-test-chatty-calls-silenced-cmd"})
require.NoError(t, err)
require.Empty(t, buff.String(), "While running task-test-chatty-calls-silenced-cmd: Expected not to see output. While the task itself is not silenced, its call to the chatty task is silent.")
buff.Reset()
}

View File

@@ -2,6 +2,7 @@ package taskfile
// Call is the parameters to a task call // Call is the parameters to a task call
type Call struct { type Call struct {
Task string Task string
Vars *Vars Vars *Vars
Silent bool
} }

View File

@@ -99,6 +99,7 @@ func (c *Cmd) UnmarshalYAML(node *yaml.Node) error {
if err := node.Decode(&taskCall); err == nil && taskCall.Task != "" { if err := node.Decode(&taskCall); err == nil && taskCall.Task != "" {
c.Task = taskCall.Task c.Task = taskCall.Task
c.Vars = taskCall.Vars c.Vars = taskCall.Vars
c.Silent = cmdStruct.Silent
return nil return nil
} }

View File

@@ -8,8 +8,9 @@ import (
// Dep is a task dependency // Dep is a task dependency
type Dep struct { type Dep struct {
Task string Task string
Vars *Vars Vars *Vars
Silent bool
} }
func (d *Dep) DeepCopy() *Dep { func (d *Dep) DeepCopy() *Dep {
@@ -35,14 +36,16 @@ func (d *Dep) UnmarshalYAML(node *yaml.Node) error {
case yaml.MappingNode: case yaml.MappingNode:
var taskCall struct { var taskCall struct {
Task string Task string
Vars *Vars Vars *Vars
Silent bool
} }
if err := node.Decode(&taskCall); err != nil { if err := node.Decode(&taskCall); err != nil {
return err return err
} }
d.Task = taskCall.Task d.Task = taskCall.Task
d.Vars = taskCall.Vars d.Vars = taskCall.Vars
d.Silent = taskCall.Silent
return nil return nil
} }

50
testdata/silent/Taskfile.yml vendored Normal file
View File

@@ -0,0 +1,50 @@
version: '3'
tasks:
silent:
desc: "silent"
silent: true
cmds:
- exit 0
chatty:
desc: "chatty"
silent: false
cmds:
- exit 0
# Test combinations of silent and chatty tasks
task-test-silent-calls-chatty-non-silenced:
silent: true
cmds:
- task: chatty
task-test-silent-calls-chatty-silenced:
silent: true
cmds:
- task: chatty
silent: true
task-test-no-cmds-calls-chatty-silenced:
silent: false
cmds:
- task: chatty
silent: true
task-test-chatty-calls-chatty-non-silenced:
silent: false
cmds:
- cmd: exit 0
- task: chatty
task-test-chatty-calls-chatty-silenced:
silent: false
cmds:
- cmd: exit 0
- task: chatty
silent: true
task-test-chatty-calls-silenced-cmd:
silent: false
cmds:
- cmd: exit 0
silent: true

View File

@@ -142,8 +142,9 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
continue continue
} }
new.Deps = append(new.Deps, &taskfile.Dep{ new.Deps = append(new.Deps, &taskfile.Dep{
Task: r.Replace(dep.Task), Task: r.Replace(dep.Task),
Vars: r.ReplaceVars(dep.Vars), Vars: r.ReplaceVars(dep.Vars),
Silent: dep.Silent,
}) })
} }
} }