mirror of
https://github.com/go-task/task.git
synced 2025-03-19 21:17:46 +02:00
Remove ignore_errors
This commit is contained in:
parent
659cae6a4c
commit
044d3a0ff9
@ -12,9 +12,8 @@ var (
|
||||
|
||||
// Precondition represents a precondition necessary for a task to run
|
||||
type Precondition struct {
|
||||
Sh string
|
||||
Msg string
|
||||
IgnoreError bool
|
||||
Sh string
|
||||
Msg string
|
||||
}
|
||||
|
||||
// UnmarshalYAML implements yaml.Unmarshaler interface.
|
||||
@ -24,28 +23,23 @@ func (p *Precondition) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
if err := unmarshal(&cmd); err == nil {
|
||||
p.Sh = cmd
|
||||
p.Msg = fmt.Sprintf("`%s` failed", cmd)
|
||||
p.IgnoreError = false
|
||||
return nil
|
||||
}
|
||||
|
||||
var sh struct {
|
||||
Sh string
|
||||
Msg string
|
||||
IgnoreError bool `yaml:"ignore_error"`
|
||||
Sh string
|
||||
Msg string
|
||||
}
|
||||
|
||||
err := unmarshal(&sh)
|
||||
|
||||
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
|
||||
if err := unmarshal(&sh); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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",
|
||||
&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 ]'",
|
||||
&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 ]"
|
||||
msg: "1 is not 2"
|
||||
`,
|
||||
&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 ]"
|
||||
msg: "1 is not 2"
|
||||
ignore_error: true
|
||||
`,
|
||||
&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 {
|
||||
|
@ -10,16 +10,12 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNecessaryPreconditionFailed is returned when a precondition fails
|
||||
ErrNecessaryPreconditionFailed = 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")
|
||||
// ErrPreconditionFailed is returned when a precondition fails
|
||||
ErrPreconditionFailed = errors.New("task: precondition not met")
|
||||
)
|
||||
|
||||
func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *taskfile.Task) (bool, error) {
|
||||
var optionalPreconditionFailed bool
|
||||
for _, p := range t.Precondition {
|
||||
for _, p := range t.Preconditions {
|
||||
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
|
||||
Command: p.Sh,
|
||||
Dir: t.Dir,
|
||||
@ -28,17 +24,9 @@ func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *taskfile.Task
|
||||
|
||||
if err != nil {
|
||||
e.Logger.Outf(p.Msg)
|
||||
if p.IgnoreError == true {
|
||||
optionalPreconditionFailed = true
|
||||
} else {
|
||||
return false, ErrNecessaryPreconditionFailed
|
||||
}
|
||||
return false, ErrPreconditionFailed
|
||||
}
|
||||
}
|
||||
|
||||
if optionalPreconditionFailed == true {
|
||||
return true, ErrOptionalPreconditionFailed
|
||||
}
|
||||
|
||||
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 {
|
||||
upToDate, err := e.isTaskUpToDate(ctx, t)
|
||||
preCondMet, err := e.areTaskPreconditionsMet(ctx, t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
preCondMet, err := e.areTaskPreconditionsMet(ctx, t)
|
||||
upToDate, err := e.isTaskUpToDate(ctx, t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -233,11 +233,8 @@ func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
|
||||
g.Go(func() error {
|
||||
err := e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars})
|
||||
if err != nil {
|
||||
if err == ErrOptionalPreconditionFailed {
|
||||
e.Logger.Errf("%s", err)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
e.Logger.Errf("%s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@ -253,11 +250,8 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
|
||||
case cmd.Task != "":
|
||||
err := e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars})
|
||||
if err != nil {
|
||||
if err == ErrOptionalPreconditionFailed {
|
||||
e.Logger.Errf("%s", err)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
e.Logger.Errf("%s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
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
|
||||
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())
|
||||
}
|
||||
buff.Reset()
|
||||
|
||||
// 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"}))
|
||||
if buff.String() != "1 != 0\n" {
|
||||
if buff.String() != "1 != 0\ntask: precondition not met\n" {
|
||||
t.Errorf("Wrong output message: %s", buff.String())
|
||||
}
|
||||
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) {
|
||||
|
15
testdata/precondition/Taskfile.yml
vendored
15
testdata/precondition/Taskfile.yml
vendored
@ -2,20 +2,14 @@ version: '2'
|
||||
|
||||
tasks:
|
||||
foo:
|
||||
precondition:
|
||||
preconditions:
|
||||
- test -f foo.txt
|
||||
|
||||
impossible:
|
||||
precondition:
|
||||
preconditions:
|
||||
- sh: "[ 1 = 0 ]"
|
||||
msg: "1 != 0"
|
||||
|
||||
impossible_but_i_dont_care:
|
||||
precondition:
|
||||
- sh: "[ 2 = 1 ]"
|
||||
msg: "2 != 1"
|
||||
ignore_error: true
|
||||
|
||||
depends_on_imposssible:
|
||||
deps:
|
||||
- impossible
|
||||
@ -27,8 +21,3 @@ tasks:
|
||||
depends_on_failure_of_impossible:
|
||||
deps:
|
||||
- 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))
|
||||
for i, precond := range origTask.Precondition {
|
||||
new.Precondition[i] = &taskfile.Precondition{
|
||||
Sh: r.Replace(precond.Sh),
|
||||
Msg: r.Replace(precond.Msg),
|
||||
IgnoreError: precond.IgnoreError,
|
||||
|
||||
if len(origTask.Preconditions) > 0 {
|
||||
new.Preconditions = make([]*taskfile.Precondition, len(origTask.Preconditions))
|
||||
for i, precond := range origTask.Preconditions {
|
||||
new.Preconditions[i] = &taskfile.Precondition{
|
||||
Sh: r.Replace(precond.Sh),
|
||||
Msg: r.Replace(precond.Msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user