From dbe053277e0f4afec8215ee0b0da5c401a32d01a Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Sun, 23 Feb 2025 12:40:55 +0100 Subject: [PATCH] preconditions cannot be in included taskfile --- task_test.go | 15 +++++++++++++++ taskfile/ast/precondition.go | 4 +++- taskfile/ast/preconditions.go | 14 -------------- taskfile/ast/taskfile.go | 7 ++++++- .../precondition/global/included/Taskfile.yml | 8 ++++++++ .../precondition/global/included/included.yml | 5 +++++ 6 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 testdata/precondition/global/included/Taskfile.yml create mode 100644 testdata/precondition/global/included/included.yml diff --git a/task_test.go b/task_test.go index 9fe02aa0..b4b73c6b 100644 --- a/task_test.go +++ b/task_test.go @@ -539,6 +539,21 @@ func TestPreconditionGlobal(t *testing.T) { if buff.String() != "task: 1 != 0 obviously!\n" { t.Errorf("Wrong output message: %s", buff.String()) } + + buff.Reset() + + e = &task.Executor{ + Dir: "testdata/precondition/global/included", + Stdout: &buff, + Stderr: &buff, + } + + err := e.Setup() + require.Error(t, err) + + assert.Equal(t, "task: Included Taskfiles can't have preconditions declarations. Please, move the preconditions declaration to the main Taskfile", err.Error()) + buff.Reset() + } func TestGenerates(t *testing.T) { diff --git a/taskfile/ast/precondition.go b/taskfile/ast/precondition.go index 2a25396b..b0570936 100644 --- a/taskfile/ast/precondition.go +++ b/taskfile/ast/precondition.go @@ -2,8 +2,10 @@ package ast import ( "fmt" - "github.com/go-task/task/v3/errors" + "gopkg.in/yaml.v3" + + "github.com/go-task/task/v3/errors" ) // Precondition represents a precondition necessary for a task to run diff --git a/taskfile/ast/preconditions.go b/taskfile/ast/preconditions.go index 5afa0412..cf72d2fe 100644 --- a/taskfile/ast/preconditions.go +++ b/taskfile/ast/preconditions.go @@ -34,20 +34,6 @@ func (p *Preconditions) DeepCopy() *Preconditions { } } -func (p *Preconditions) Merge(other *Preconditions) { - if p == nil || p.Values == nil || other == nil { - return - } - - p.mutex.Lock() - defer p.mutex.Unlock() - - other.mutex.RLock() - defer other.mutex.RUnlock() - - p.Values = append(p.Values, deepcopy.Slice(other.Values)...) -} - func (p *Preconditions) UnmarshalYAML(node *yaml.Node) error { if p == nil || p.Values == nil { *p = *NewPreconditions() diff --git a/taskfile/ast/taskfile.go b/taskfile/ast/taskfile.go index 988ae216..47cc540e 100644 --- a/taskfile/ast/taskfile.go +++ b/taskfile/ast/taskfile.go @@ -18,6 +18,9 @@ var V3 = semver.MustParse("3") // ErrIncludedTaskfilesCantHaveDotenvs is returned when a included Taskfile contains dotenvs var ErrIncludedTaskfilesCantHaveDotenvs = errors.New("task: Included Taskfiles can't have dotenv declarations. Please, move the dotenv declaration to the main Taskfile") +// ErrIncludedTaskfilesCantHavePreconditions is returned when a included Taskfile contains Preconditions +var ErrIncludedTaskfilesCantHavePreconditions = errors.New("task: Included Taskfiles can't have preconditions declarations. Please, move the preconditions declaration to the main Taskfile") + // Taskfile is the abstract syntax tree for a Taskfile type Taskfile struct { Location string @@ -45,6 +48,9 @@ func (t1 *Taskfile) Merge(t2 *Taskfile, include *Include) error { if len(t2.Dotenv) > 0 { return ErrIncludedTaskfilesCantHaveDotenvs } + if len(t2.Preconditions.Values) > 0 { + return ErrIncludedTaskfilesCantHavePreconditions + } if t2.Output.IsSet() { t1.Output = t2.Output } @@ -65,7 +71,6 @@ func (t1 *Taskfile) Merge(t2 *Taskfile, include *Include) error { } t1.Vars.Merge(t2.Vars, include) t1.Env.Merge(t2.Env, include) - t1.Preconditions.Merge(t2.Preconditions) return t1.Tasks.Merge(t2.Tasks, include, t1.Vars) } diff --git a/testdata/precondition/global/included/Taskfile.yml b/testdata/precondition/global/included/Taskfile.yml new file mode 100644 index 00000000..fca98bda --- /dev/null +++ b/testdata/precondition/global/included/Taskfile.yml @@ -0,0 +1,8 @@ +version: 3 + +includes: + included: included.yml + +preconditions: + - sh: "[ 1 = 0 ]" + msg: "1 != 0 obviously!" diff --git a/testdata/precondition/global/included/included.yml b/testdata/precondition/global/included/included.yml new file mode 100644 index 00000000..9f4cfab7 --- /dev/null +++ b/testdata/precondition/global/included/included.yml @@ -0,0 +1,5 @@ +version: 3 + +preconditions: + - sh: "[ 1 = 0 ]" + msg: "1 != 0 obviously!"