Skip to content

Commit 35ea4e0

Browse files
authored
feat: display allowed values when vars are not provided (#2052)
1 parent 2b4d9bf commit 35ea4e0

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

errors/errors_task.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,37 @@ func (err *TaskCancelledNoTerminalError) Code() int {
141141
return CodeTaskCancelled
142142
}
143143

144-
// TaskMissingRequiredVars is returned when a task is missing required variables.
145-
type TaskMissingRequiredVars struct {
144+
// TaskMissingRequiredVarsError is returned when a task is missing required variables.
145+
146+
type MissingVar struct {
147+
Name string
148+
AllowedValues []string
149+
}
150+
type TaskMissingRequiredVarsError struct {
146151
TaskName string
147-
MissingVars []string
152+
MissingVars []MissingVar
153+
}
154+
155+
func (v MissingVar) String() string {
156+
if len(v.AllowedValues) == 0 {
157+
return v.Name
158+
}
159+
return fmt.Sprintf("%s (allowed values: %v)", v.Name, v.AllowedValues)
148160
}
149161

150-
func (err *TaskMissingRequiredVars) Error() string {
162+
func (err *TaskMissingRequiredVarsError) Error() string {
163+
var vars []string
164+
for _, v := range err.MissingVars {
165+
vars = append(vars, v.String())
166+
}
167+
151168
return fmt.Sprintf(
152169
`task: Task %q cancelled because it is missing required variables: %s`,
153170
err.TaskName,
154-
strings.Join(err.MissingVars, ", "),
155-
)
171+
strings.Join(vars, ", "))
156172
}
157173

158-
func (err *TaskMissingRequiredVars) Code() int {
174+
func (err *TaskMissingRequiredVarsError) Code() int {
159175
return CodeTaskMissingRequiredVars
160176
}
161177

@@ -165,12 +181,12 @@ type NotAllowedVar struct {
165181
Name string
166182
}
167183

168-
type TaskNotAllowedVars struct {
184+
type TaskNotAllowedVarsError struct {
169185
TaskName string
170186
NotAllowedVars []NotAllowedVar
171187
}
172188

173-
func (err *TaskNotAllowedVars) Error() string {
189+
func (err *TaskNotAllowedVarsError) Error() string {
174190
var builder strings.Builder
175191

176192
builder.WriteString(fmt.Sprintf("task: Task %q cancelled because it is missing required variables:\n", err.TaskName))
@@ -181,6 +197,6 @@ func (err *TaskNotAllowedVars) Error() string {
181197
return builder.String()
182198
}
183199

184-
func (err *TaskNotAllowedVars) Code() int {
200+
func (err *TaskNotAllowedVarsError) Code() int {
185201
return CodeTaskNotAllowedVars
186202
}

requires.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ func (e *Executor) areTaskRequiredVarsSet(t *ast.Task) error {
1212
return nil
1313
}
1414

15-
var missingVars []string
15+
var missingVars []errors.MissingVar
1616
for _, requiredVar := range t.Requires.Vars {
1717
_, ok := t.Vars.Get(requiredVar.Name)
1818
if !ok {
19-
missingVars = append(missingVars, requiredVar.Name)
19+
missingVars = append(missingVars, errors.MissingVar{
20+
Name: requiredVar.Name,
21+
AllowedValues: requiredVar.Enum,
22+
})
2023
}
2124
}
2225

2326
if len(missingVars) > 0 {
24-
return &errors.TaskMissingRequiredVars{
27+
return &errors.TaskMissingRequiredVarsError{
2528
TaskName: t.Name(),
2629
MissingVars: missingVars,
2730
}
@@ -51,7 +54,7 @@ func (e *Executor) areTaskRequiredVarsAllowedValuesSet(t *ast.Task) error {
5154
}
5255

5356
if len(notAllowedValuesVars) > 0 {
54-
return &errors.TaskNotAllowedVars{
57+
return &errors.TaskNotAllowedVarsError{
5558
TaskName: t.Name(),
5659
NotAllowedVars: notAllowedValuesVars,
5760
}

task_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,30 @@ func TestRequires(t *testing.T) {
186186

187187
vars := ast.NewVars()
188188
vars.Set("FOO", ast.Var{Value: "bar"})
189+
189190
require.NoError(t, e.Run(context.Background(), &ast.Call{
190191
Task: "missing-var",
191192
Vars: vars,
192193
}))
193194
buff.Reset()
194195

196+
vars.Set("ENV", ast.Var{Value: "dev"})
195197
require.NoError(t, e.Setup())
196198
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])")
197199
buff.Reset()
198200

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

206+
vars = ast.NewVars()
203207
require.NoError(t, e.Setup())
204-
vars.Set("FOO", ast.Var{Value: "one"})
205-
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "validation-var", Vars: vars}))
208+
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: ENV, FOO (allowed values: [one two])")
209+
buff.Reset()
210+
211+
require.NoError(t, e.Setup())
212+
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "validation-var-dynamic", Vars: vars}))
206213
buff.Reset()
207214

208215
require.NoError(t, e.Setup())

testdata/requires/Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ tasks:
3333
validation-var:
3434
requires:
3535
vars:
36+
- ENV
3637
- name: FOO
3738
enum: ['one', 'two']
3839

39-
4040
require-before-compile:
4141
requires:
4242
vars: [ MY_VAR ]

0 commit comments

Comments
 (0)