|
| 1 | +package task |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + |
| 8 | + "github.com/go-task/task/v3/taskfile/ast" |
| 9 | +) |
| 10 | + |
| 11 | +func TestResolveEnumRefForPrompt(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + vars := ast.NewVars() |
| 15 | + vars.Set("ALLOWED_ENVS", ast.Var{Value: []any{"dev", "staging", "prod"}}) |
| 16 | + |
| 17 | + t.Run("resolves a static ref into values", func(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + v := &ast.VarsWithValidation{Name: "ENV", Enum: &ast.Enum{Ref: ".ALLOWED_ENVS"}} |
| 21 | + |
| 22 | + resolved := resolveEnumRefForPrompt(v, vars) |
| 23 | + |
| 24 | + require.Equal(t, []string{"dev", "staging", "prod"}, getEnumValues(resolved.Enum)) |
| 25 | + require.Empty(t, v.Enum.Value, "input var must not be mutated") |
| 26 | + require.Equal(t, ".ALLOWED_ENVS", v.Enum.Ref) |
| 27 | + }) |
| 28 | + |
| 29 | + t.Run("leaves an unresolvable ref as-is", func(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + v := &ast.VarsWithValidation{Name: "ENV", Enum: &ast.Enum{Ref: ".NONEXISTENT"}} |
| 33 | + |
| 34 | + require.Empty(t, getEnumValues(resolveEnumRefForPrompt(v, vars).Enum)) |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("passes through a static enum unchanged", func(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + |
| 40 | + v := &ast.VarsWithValidation{Name: "ENV", Enum: &ast.Enum{Value: []string{"a", "b"}}} |
| 41 | + |
| 42 | + require.Same(t, v, resolveEnumRefForPrompt(v, vars)) |
| 43 | + }) |
| 44 | +} |
0 commit comments