Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: requires allowed values works with dynamic var #2033

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions requires.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,10 @@ func (e *Executor) areTaskRequiredVarsSet(t *ast.Task) error {
}

var missingVars []string
var notAllowedValuesVars []errors.NotAllowedVar
for _, requiredVar := range t.Requires.Vars {
value, ok := t.Vars.Get(requiredVar.Name)
_, ok := t.Vars.Get(requiredVar.Name)
if !ok {
missingVars = append(missingVars, requiredVar.Name)
} else {
value, isString := value.Value.(string)
if isString && requiredVar.Enum != nil && !slices.Contains(requiredVar.Enum, value) {
notAllowedValuesVars = append(notAllowedValuesVars, errors.NotAllowedVar{
Value: value,
Enum: requiredVar.Enum,
Name: requiredVar.Name,
})
}
}
}

Expand All @@ -37,6 +27,29 @@ func (e *Executor) areTaskRequiredVarsSet(t *ast.Task) error {
}
}

return nil
}

func (e *Executor) areTaskRequiredVarsAllowedValuesSet(t *ast.Task) error {
if t.Requires == nil || len(t.Requires.Vars) == 0 {
return nil
}

var notAllowedValuesVars []errors.NotAllowedVar
for _, requiredVar := range t.Requires.Vars {
varValue, _ := t.Vars.Get(requiredVar.Name)

value, isString := varValue.Value.(string)
if isString && requiredVar.Enum != nil && !slices.Contains(requiredVar.Enum, value) {
notAllowedValuesVars = append(notAllowedValuesVars, errors.NotAllowedVar{
Value: value,
Enum: requiredVar.Enum,
Name: requiredVar.Name,
})
}

}

if len(notAllowedValuesVars) > 0 {
return &errors.TaskNotAllowedVars{
TaskName: t.Name(),
Expand Down
5 changes: 5 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ func (e *Executor) RunTask(ctx context.Context, call *ast.Call) error {
if err != nil {
return err
}

if err := e.areTaskRequiredVarsAllowedValuesSet(t); err != nil {
return err
}

if !e.Watch && atomic.AddInt32(e.taskCallCount[t.Task], 1) >= MaximumTaskCall {
return &errors.TaskCalledTooManyTimesError{
TaskName: t.Task,
Expand Down
12 changes: 8 additions & 4 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,24 +180,28 @@ func TestRequires(t *testing.T) {
}

require.NoError(t, e.Setup())
require.ErrorContains(t, e.Run(context.Background(), &ast.Call{Task: "missing-var"}), "task: Task \"missing-var\" cancelled because it is missing required variables: foo")
require.ErrorContains(t, e.Run(context.Background(), &ast.Call{Task: "missing-var"}), "task: Task \"missing-var\" cancelled because it is missing required variables: FOO")
buff.Reset()
require.NoError(t, e.Setup())

vars := ast.NewVars()
vars.Set("foo", ast.Var{Value: "bar"})
vars.Set("FOO", ast.Var{Value: "bar"})
require.NoError(t, e.Run(context.Background(), &ast.Call{
Task: "missing-var",
Vars: vars,
}))
buff.Reset()

require.NoError(t, e.Setup())
require.ErrorContains(t, e.Run(context.Background(), &ast.Call{Task: "validation-var", Vars: vars}), "task: Task \"validation-var\" cancelled because it is missing required variables:\n - foo has an invalid value : 'bar' (allowed values : [one two])")
require.ErrorContains(t, e.Run(context.Background(), &ast.Call{Task: "validation-var", Vars: vars}), "task: Task \"validation-var\" cancelled because it is missing required variables:\n - FOO has an invalid value : 'bar' (allowed values : [one two])")
buff.Reset()

require.NoError(t, e.Setup())
vars.Set("foo", ast.Var{Value: "one"})
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "validation-var-dynamic", Vars: vars}))
buff.Reset()

require.NoError(t, e.Setup())
vars.Set("FOO", ast.Var{Value: "one"})
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "validation-var", Vars: vars}))
buff.Reset()

Expand Down
15 changes: 13 additions & 2 deletions testdata/requires/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tasks:
missing-var:
requires:
vars:
- foo
- FOO
cmd: echo "{{.foo}}"

var-defined-in-task:
Expand All @@ -19,10 +19,21 @@ tasks:
cmd: echo "{{.FOO}}"


validation-var-dynamic:
vars:
FOO:
sh: echo "one"

requires:
vars:
- name: FOO
enum: ['one', 'two']


validation-var:
requires:
vars:
- name: foo
- name: FOO
enum: ['one', 'two']


Expand Down
Loading