mirror of
https://github.com/go-task/task.git
synced 2025-06-23 00:38:19 +02:00
Remove ignore_errors
This commit is contained in:
@ -12,9 +12,8 @@ var (
|
|||||||
|
|
||||||
// Precondition represents a precondition necessary for a task to run
|
// Precondition represents a precondition necessary for a task to run
|
||||||
type Precondition struct {
|
type Precondition struct {
|
||||||
Sh string
|
Sh string
|
||||||
Msg string
|
Msg string
|
||||||
IgnoreError bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalYAML implements yaml.Unmarshaler interface.
|
// UnmarshalYAML implements yaml.Unmarshaler interface.
|
||||||
@ -24,28 +23,23 @@ func (p *Precondition) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||||||
if err := unmarshal(&cmd); err == nil {
|
if err := unmarshal(&cmd); err == nil {
|
||||||
p.Sh = cmd
|
p.Sh = cmd
|
||||||
p.Msg = fmt.Sprintf("`%s` failed", cmd)
|
p.Msg = fmt.Sprintf("`%s` failed", cmd)
|
||||||
p.IgnoreError = false
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var sh struct {
|
var sh struct {
|
||||||
Sh string
|
Sh string
|
||||||
Msg string
|
Msg string
|
||||||
IgnoreError bool `yaml:"ignore_error"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := unmarshal(&sh)
|
if err := unmarshal(&sh); err != nil {
|
||||||
|
return err
|
||||||
if err == nil {
|
|
||||||
p.Sh = sh.Sh
|
|
||||||
p.Msg = sh.Msg
|
|
||||||
if p.Msg == "" {
|
|
||||||
p.Msg = fmt.Sprintf("%s failed", sh.Sh)
|
|
||||||
}
|
|
||||||
|
|
||||||
p.IgnoreError = sh.IgnoreError
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
p.Sh = sh.Sh
|
||||||
|
p.Msg = sh.Msg
|
||||||
|
if p.Msg == "" {
|
||||||
|
p.Msg = fmt.Sprintf("%s failed", sh.Sh)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -18,27 +18,26 @@ func TestPreconditionParse(t *testing.T) {
|
|||||||
{
|
{
|
||||||
"test -f foo.txt",
|
"test -f foo.txt",
|
||||||
&taskfile.Precondition{},
|
&taskfile.Precondition{},
|
||||||
&taskfile.Precondition{Sh: `test -f foo.txt`, Msg: "`test -f foo.txt` failed", IgnoreError: false},
|
&taskfile.Precondition{Sh: `test -f foo.txt`, Msg: "`test -f foo.txt` failed"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"sh: '[ 1 = 0 ]'",
|
"sh: '[ 1 = 0 ]'",
|
||||||
&taskfile.Precondition{},
|
&taskfile.Precondition{},
|
||||||
&taskfile.Precondition{Sh: "[ 1 = 0 ]", Msg: "[ 1 = 0 ] failed", IgnoreError: false},
|
&taskfile.Precondition{Sh: "[ 1 = 0 ]", Msg: "[ 1 = 0 ] failed"},
|
||||||
},
|
},
|
||||||
{`
|
{`
|
||||||
sh: "[ 1 = 2 ]"
|
sh: "[ 1 = 2 ]"
|
||||||
msg: "1 is not 2"
|
msg: "1 is not 2"
|
||||||
`,
|
`,
|
||||||
&taskfile.Precondition{},
|
&taskfile.Precondition{},
|
||||||
&taskfile.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2", IgnoreError: false},
|
&taskfile.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2"},
|
||||||
},
|
},
|
||||||
{`
|
{`
|
||||||
sh: "[ 1 = 2 ]"
|
sh: "[ 1 = 2 ]"
|
||||||
msg: "1 is not 2"
|
msg: "1 is not 2"
|
||||||
ignore_error: true
|
|
||||||
`,
|
`,
|
||||||
&taskfile.Precondition{},
|
&taskfile.Precondition{},
|
||||||
&taskfile.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2", IgnoreError: true},
|
&taskfile.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
@ -10,16 +10,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrNecessaryPreconditionFailed is returned when a precondition fails
|
// ErrPreconditionFailed is returned when a precondition fails
|
||||||
ErrNecessaryPreconditionFailed = errors.New("task: precondition not met")
|
ErrPreconditionFailed = errors.New("task: precondition not met")
|
||||||
// ErrOptionalPreconditionFailed is returned when a precondition fails
|
|
||||||
// that has ignore_error set to true
|
|
||||||
ErrOptionalPreconditionFailed = errors.New("task: optional precondition not met")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *taskfile.Task) (bool, error) {
|
func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *taskfile.Task) (bool, error) {
|
||||||
var optionalPreconditionFailed bool
|
for _, p := range t.Preconditions {
|
||||||
for _, p := range t.Precondition {
|
|
||||||
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
|
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
|
||||||
Command: p.Sh,
|
Command: p.Sh,
|
||||||
Dir: t.Dir,
|
Dir: t.Dir,
|
||||||
@ -28,17 +24,9 @@ func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *taskfile.Task
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e.Logger.Outf(p.Msg)
|
e.Logger.Outf(p.Msg)
|
||||||
if p.IgnoreError == true {
|
return false, ErrPreconditionFailed
|
||||||
optionalPreconditionFailed = true
|
|
||||||
} else {
|
|
||||||
return false, ErrNecessaryPreconditionFailed
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if optionalPreconditionFailed == true {
|
|
||||||
return true, ErrOptionalPreconditionFailed
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
18
task.go
18
task.go
@ -189,12 +189,12 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !e.Force {
|
if !e.Force {
|
||||||
upToDate, err := e.isTaskUpToDate(ctx, t)
|
preCondMet, err := e.areTaskPreconditionsMet(ctx, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
preCondMet, err := e.areTaskPreconditionsMet(ctx, t)
|
upToDate, err := e.isTaskUpToDate(ctx, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -233,11 +233,8 @@ func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
|
|||||||
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})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == ErrOptionalPreconditionFailed {
|
e.Logger.Errf("%s", err)
|
||||||
e.Logger.Errf("%s", err)
|
return err
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -253,11 +250,8 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
|
|||||||
case cmd.Task != "":
|
case cmd.Task != "":
|
||||||
err := e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars})
|
err := e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == ErrOptionalPreconditionFailed {
|
e.Logger.Errf("%s", err)
|
||||||
e.Logger.Errf("%s", err)
|
return err
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
case cmd.Cmd != "":
|
case cmd.Cmd != "":
|
||||||
|
26
task_test.go
26
task_test.go
@ -301,38 +301,18 @@ func TestPrecondition(t *testing.T) {
|
|||||||
|
|
||||||
// Calling a task with a precondition in a dependency fails the task
|
// Calling a task with a precondition in a dependency fails the task
|
||||||
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "depends_on_imposssible"}))
|
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "depends_on_imposssible"}))
|
||||||
if buff.String() != "1 != 0\n" {
|
|
||||||
|
if buff.String() != "1 != 0\ntask: precondition not met\n" {
|
||||||
t.Errorf("Wrong output message: %s", buff.String())
|
t.Errorf("Wrong output message: %s", buff.String())
|
||||||
}
|
}
|
||||||
buff.Reset()
|
buff.Reset()
|
||||||
|
|
||||||
// Calling a task with a precondition in a cmd fails the task
|
// Calling a task with a precondition in a cmd fails the task
|
||||||
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "executes_failing_task_as_cmd"}))
|
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "executes_failing_task_as_cmd"}))
|
||||||
if buff.String() != "1 != 0\n" {
|
if buff.String() != "1 != 0\ntask: precondition not met\n" {
|
||||||
t.Errorf("Wrong output message: %s", buff.String())
|
t.Errorf("Wrong output message: %s", buff.String())
|
||||||
}
|
}
|
||||||
buff.Reset()
|
buff.Reset()
|
||||||
|
|
||||||
// A task with a failing precondition and ignore_errors on still fails
|
|
||||||
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "impossible_but_i_dont_care"}))
|
|
||||||
if buff.String() != "2 != 1\n" {
|
|
||||||
t.Errorf("Wrong output message: %s", buff.String())
|
|
||||||
}
|
|
||||||
buff.Reset()
|
|
||||||
|
|
||||||
// If a precondition has ignore errors, then it will allow _dependent_ tasks to execute
|
|
||||||
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "depends_on_failure_of_impossible"}))
|
|
||||||
if buff.String() != "2 != 1\ntask: optional precondition not met\n" {
|
|
||||||
t.Errorf("Wrong output message: %s", buff.String())
|
|
||||||
}
|
|
||||||
buff.Reset()
|
|
||||||
|
|
||||||
// If a precondition has ignore errors, then it will allow tasks calling it to execute
|
|
||||||
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "executes_failing_task_as_cmd_but_succeeds"}))
|
|
||||||
if buff.String() != "2 != 1\ntask: optional precondition not met\n" {
|
|
||||||
t.Errorf("Wrong output message: %s", buff.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenerates(t *testing.T) {
|
func TestGenerates(t *testing.T) {
|
||||||
|
15
testdata/precondition/Taskfile.yml
vendored
15
testdata/precondition/Taskfile.yml
vendored
@ -2,20 +2,14 @@ version: '2'
|
|||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
foo:
|
foo:
|
||||||
precondition:
|
preconditions:
|
||||||
- test -f foo.txt
|
- test -f foo.txt
|
||||||
|
|
||||||
impossible:
|
impossible:
|
||||||
precondition:
|
preconditions:
|
||||||
- sh: "[ 1 = 0 ]"
|
- sh: "[ 1 = 0 ]"
|
||||||
msg: "1 != 0"
|
msg: "1 != 0"
|
||||||
|
|
||||||
impossible_but_i_dont_care:
|
|
||||||
precondition:
|
|
||||||
- sh: "[ 2 = 1 ]"
|
|
||||||
msg: "2 != 1"
|
|
||||||
ignore_error: true
|
|
||||||
|
|
||||||
depends_on_imposssible:
|
depends_on_imposssible:
|
||||||
deps:
|
deps:
|
||||||
- impossible
|
- impossible
|
||||||
@ -27,8 +21,3 @@ tasks:
|
|||||||
depends_on_failure_of_impossible:
|
depends_on_failure_of_impossible:
|
||||||
deps:
|
deps:
|
||||||
- impossible_but_i_dont_care
|
- impossible_but_i_dont_care
|
||||||
|
|
||||||
executes_failing_task_as_cmd_but_succeeds:
|
|
||||||
cmds:
|
|
||||||
- task: impossible_but_i_dont_care
|
|
||||||
|
|
||||||
|
14
variables.go
14
variables.go
@ -83,13 +83,13 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(origTask.Precondition) > 0 {
|
|
||||||
new.Precondition = make([]*taskfile.Precondition, len(origTask.Precondition))
|
if len(origTask.Preconditions) > 0 {
|
||||||
for i, precond := range origTask.Precondition {
|
new.Preconditions = make([]*taskfile.Precondition, len(origTask.Preconditions))
|
||||||
new.Precondition[i] = &taskfile.Precondition{
|
for i, precond := range origTask.Preconditions {
|
||||||
Sh: r.Replace(precond.Sh),
|
new.Preconditions[i] = &taskfile.Precondition{
|
||||||
Msg: r.Replace(precond.Msg),
|
Sh: r.Replace(precond.Sh),
|
||||||
IgnoreError: precond.IgnoreError,
|
Msg: r.Replace(precond.Msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user