Skip to content

requires_check_nil #2315

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,38 @@ func TestRequires(t *testing.T) {
),
WithTask("var-defined-in-task"),
)
NewExecutorTest(t,
WithName("require null"),
WithExecutorOptions(
task.WithDir("testdata/requires"),
),
WithTask("require-null"),
WithRunError(),
)
NewExecutorTest(t,
WithName("require empty string"),
WithExecutorOptions(
task.WithDir("testdata/requires"),
),
WithTask("require-empty-string"),
WithRunError(),
)
NewExecutorTest(t,
WithName("require empty template"),
WithExecutorOptions(
task.WithDir("testdata/requires"),
),
WithTask("require-empty-template"),
WithRunError(),
)
NewExecutorTest(t,
WithName("require empty dynamic var"),
WithExecutorOptions(
task.WithDir("testdata/requires"),
),
WithTask("require-empty-dynamic-var"),
WithRunError(),
)
}

// TODO: mock fs
Expand Down
16 changes: 14 additions & 2 deletions requires.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@ import (
"github.com/go-task/task/v3/taskfile/ast"
)

func isNilValue(value any) bool {
if value == nil {
return true
} else {
v, ok := value.(string)
if ok && len(v) == 0 {
return true
}
}
return false
}

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

var missingVars []errors.MissingVar
for _, requiredVar := range t.Requires.Vars {
_, ok := t.Vars.Get(requiredVar.Name)
if !ok {
varValue, ok := t.Vars.Get(requiredVar.Name)
if !ok || isNilValue(varValue.Value) {
missingVars = append(missingVars, errors.MissingVar{
Name: requiredVar.Name,
AllowedValues: requiredVar.Enum,
Expand Down
4 changes: 4 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (e *Executor) RunTask(ctx context.Context, call *Call) error {
return nil
}

t, err = e.FastCompiledTaskWithVars(call)
if err != nil {
return err
}
if err := e.areTaskRequiredVarsSet(t); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/json_list_format/testdata/TestJsonListFormat.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"location": {
"line": 4,
"column": 3,
"taskfile": "{{ .TaskfileLocation }}"
"taskfile": "/Users/runner/work/task/testdata/json_list_format/Taskfile.yml"
}
}
],
"location": "{{ .TaskfileLocation }}"
"location": "/Users/runner/work/task/testdata/json_list_format/Taskfile.yml"
}
33 changes: 33 additions & 0 deletions testdata/requires/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,36 @@ tasks:
{{range .MY_VAR | splitList " " }}
echo {{.}}
{{end}}

require-null:
vars:
FOO: null
cmds:
- echo "{{.FOO}}"
requires:
vars: [FOO]

require-empty-string:
vars:
FOO: ''
cmds:
- echo "{{.FOO}}"
requires:
vars: [FOO]

require-empty-template:
vars:
FOO: '{{.C}}'
cmds:
- echo "{{.FOO}}"
requires:
vars: [FOO]

require-empty-dynamic-var:
vars:
FOO:
sh: echo ""
cmds:
- echo "{{.FOO}}"
requires:
vars: [FOO]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
task: Task "require-empty-dynamic-var" cancelled because it is missing required variables: FOO
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
task: Task "require-empty-string" cancelled because it is missing required variables: FOO
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
task: Task "require-empty-template" cancelled because it is missing required variables: FOO
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
task: Task "require-null" cancelled because it is missing required variables: FOO
Empty file.
16 changes: 11 additions & 5 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,28 @@ import (
// CompiledTask returns a copy of a task, but replacing variables in almost all
// properties using the Go template package.
func (e *Executor) CompiledTask(call *Call) (*ast.Task, error) {
return e.compiledTask(call, true)
return e.compiledTask(call, true, true)
}

// FastCompiledTask is like CompiledTask, but it skippes dynamic variables.
// FastCompiledTask is like CompiledTask, but it skips dynamic variables.
func (e *Executor) FastCompiledTask(call *Call) (*ast.Task, error) {
return e.compiledTask(call, false)
return e.compiledTask(call, false, false)
}

func (e *Executor) compiledTask(call *Call, evaluateShVars bool) (*ast.Task, error) {
// FastCompiledTaskWithVars is like FastCompiledTask, but dynamic variables
// of the task are processed.
func (e *Executor) FastCompiledTaskWithVars(call *Call) (*ast.Task, error) {
return e.compiledTask(call, false, true)
}

func (e *Executor) compiledTask(call *Call, evaluateShVars bool, evaluateTaskVarsOnly bool) (*ast.Task, error) {
origTask, err := e.GetTask(call)
if err != nil {
return nil, err
}

var vars *ast.Vars
if evaluateShVars {
if evaluateShVars || evaluateTaskVarsOnly {
vars, err = e.Compiler.GetVariables(origTask, call)
} else {
vars, err = e.Compiler.FastGetVariables(origTask, call)
Expand Down