From 044d3a0ff9a425241a2e930dab295e793e483ae7 Mon Sep 17 00:00:00 2001 From: Stephen Prater Date: Tue, 28 May 2019 13:02:59 -0700 Subject: [PATCH] Remove ignore_errors --- internal/taskfile/precondition.go | 32 +++++++++++--------------- internal/taskfile/precondition_test.go | 9 ++++---- precondition.go | 20 ++++------------ task.go | 18 +++++---------- task_test.go | 26 +++------------------ testdata/precondition/Taskfile.yml | 15 ++---------- variables.go | 14 +++++------ 7 files changed, 39 insertions(+), 95 deletions(-) diff --git a/internal/taskfile/precondition.go b/internal/taskfile/precondition.go index 04b116a5..554cdf12 100644 --- a/internal/taskfile/precondition.go +++ b/internal/taskfile/precondition.go @@ -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 } diff --git a/internal/taskfile/precondition_test.go b/internal/taskfile/precondition_test.go index acf89f27..799e9ac4 100644 --- a/internal/taskfile/precondition_test.go +++ b/internal/taskfile/precondition_test.go @@ -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 { diff --git a/precondition.go b/precondition.go index c8284ab4..115c19dd 100644 --- a/precondition.go +++ b/precondition.go @@ -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 } diff --git a/task.go b/task.go index f0877f08..608622f8 100644 --- a/task.go +++ b/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 != "": diff --git a/task_test.go b/task_test.go index 51dce8c8..05290fd5 100644 --- a/task_test.go +++ b/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) { diff --git a/testdata/precondition/Taskfile.yml b/testdata/precondition/Taskfile.yml index 02b798ac..b9f2c338 100644 --- a/testdata/precondition/Taskfile.yml +++ b/testdata/precondition/Taskfile.yml @@ -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 - diff --git a/variables.go b/variables.go index 5af5f873..9c227372 100644 --- a/variables.go +++ b/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), } } }