-
-
Notifications
You must be signed in to change notification settings - Fork 887
fix: resolve enum refs that depend on dynamic variables in interactive prompts #2969
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
Closed
ikshantshukla123
wants to merge
3
commits into
go-task:main
from
ikshantshukla123:fix/interactive-dynamic-enum-ref
+226
−11
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,166 @@ | ||
| package task | ||
|
|
||
| import ( | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/go-task/task/v3/internal/logger" | ||
| "github.com/go-task/task/v3/taskfile/ast" | ||
| ) | ||
|
|
||
| // newTestPromptExecutor returns an Executor whose Compiler is wired up so that | ||
| // dynamic (sh:) variables can be evaluated during enum ref resolution. | ||
| func newTestPromptExecutor(t *testing.T) *Executor { | ||
| t.Helper() | ||
| e := NewExecutor(WithAssumeTerm(true)) | ||
| e.Compiler = &Compiler{ | ||
| Dir: t.TempDir(), | ||
| TaskfileEnv: ast.NewVars(), | ||
| TaskfileVars: ast.NewVars(), | ||
| Logger: &logger.Logger{Stderr: io.Discard}, | ||
| } | ||
| return e | ||
| } | ||
|
|
||
| // TestPromptEnumRefResolution exercises the real prompt path: fast-compiled | ||
| // task vars are resolved through resolveEnumRefForPrompt, exactly as | ||
| // promptDepsVars does, against a real Taskfile fixture. | ||
| func TestPromptEnumRefResolution(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| e := NewExecutor(WithDir("testdata/enum_ref_prompt"), WithAssumeTerm(true)) | ||
| require.NoError(t, e.Setup()) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| task string | ||
| varName string | ||
| wantEnum []string | ||
| }{ | ||
| { | ||
| name: "enum ref to a dynamic sh variable is evaluated", | ||
| task: "deploy", | ||
| varName: "SERVICE", | ||
| wantEnum: []string{"api", "web", "db"}, | ||
| }, | ||
| { | ||
| name: "enum ref to a static list variable resolves", | ||
| task: "release", | ||
| varName: "ENV", | ||
| wantEnum: []string{"dev", "staging", "prod"}, | ||
| }, | ||
| { | ||
| name: "enum ref to an empty dynamic variable yields no options", | ||
| task: "deploy-empty", | ||
| varName: "SERVICE", | ||
| // resolveEnumRefs always assigns a (possibly empty) slice, which is | ||
| // what keeps the prompter on free-form input. | ||
| wantEnum: []string{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| compiledTask, err := e.FastCompiledTask(&Call{Task: tt.task}) | ||
| require.NoError(t, err) | ||
|
|
||
| missing := getMissingRequiredVars(compiledTask) | ||
| require.Len(t, missing, 1) | ||
| require.Equal(t, tt.varName, missing[0].Name) | ||
|
|
||
| resolved := e.resolveEnumRefForPrompt(missing[0], compiledTask.Vars, compiledTask.Dir) | ||
|
|
||
| require.Equal(t, tt.wantEnum, getEnumValues(resolved.Enum)) | ||
| require.Empty(t, missing[0].Enum.Value, "input var must not be mutated") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveEnumRefForPrompt(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| vars := ast.NewVars() | ||
| vars.Set("ALLOWED_ENVS", ast.Var{Value: []any{"dev", "staging", "prod"}}) | ||
| e := newTestPromptExecutor(t) | ||
| dir := e.Compiler.Dir | ||
|
|
||
| t.Run("resolves a static ref into values", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| vars := ast.NewVars() | ||
| vars.Set("ALLOWED_ENVS", ast.Var{Value: []any{"dev", "staging", "prod"}}) | ||
|
|
||
| v := &ast.VarsWithValidation{Name: "ENV", Enum: &ast.Enum{Ref: ".ALLOWED_ENVS"}} | ||
|
|
||
| resolved := resolveEnumRefForPrompt(v, vars) | ||
| resolved := e.resolveEnumRefForPrompt(v, vars, dir) | ||
|
|
||
| require.Equal(t, []string{"dev", "staging", "prod"}, getEnumValues(resolved.Enum)) | ||
| require.Empty(t, v.Enum.Value, "input var must not be mutated") | ||
| require.Equal(t, ".ALLOWED_ENVS", v.Enum.Ref) | ||
| }) | ||
|
|
||
| t.Run("leaves an unresolvable ref as-is", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| vars := ast.NewVars() | ||
| vars.Set("ALLOWED_ENVS", ast.Var{Value: []any{"dev", "staging", "prod"}}) | ||
|
|
||
| v := &ast.VarsWithValidation{Name: "ENV", Enum: &ast.Enum{Ref: ".NONEXISTENT"}} | ||
|
|
||
| require.Empty(t, getEnumValues(resolveEnumRefForPrompt(v, vars).Enum)) | ||
| require.Empty(t, getEnumValues(e.resolveEnumRefForPrompt(v, vars, dir).Enum)) | ||
| }) | ||
|
|
||
| t.Run("passes through a static enum unchanged", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| v := &ast.VarsWithValidation{Name: "ENV", Enum: &ast.Enum{Value: []string{"a", "b"}}} | ||
|
|
||
| require.Same(t, v, resolveEnumRefForPrompt(v, vars)) | ||
| require.Same(t, v, e.resolveEnumRefForPrompt(v, ast.NewVars(), dir)) | ||
| }) | ||
|
|
||
| t.Run("resolves a ref to a dynamic sh variable", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // FastGetVariables stores un-evaluated dynamic vars as {Value: "", Sh: ...}. | ||
| // The sh command must still be evaluated so the ref resolves. | ||
| fastVars := ast.NewVars() | ||
| fastVars.Set("AVAILABLE_SERVICES", ast.Var{Value: "", Sh: strPtr("printf 'api\nweb\ndb\n'")}) | ||
|
|
||
| v := &ast.VarsWithValidation{Name: "SERVICE", Enum: &ast.Enum{Ref: ".AVAILABLE_SERVICES | splitLines | compact"}} | ||
|
|
||
| resolved := e.resolveEnumRefForPrompt(v, fastVars, dir) | ||
|
|
||
| require.Equal(t, []string{"api", "web", "db"}, getEnumValues(resolved.Enum)) | ||
| require.Empty(t, v.Enum.Value, "input var must not be mutated") | ||
| }) | ||
|
|
||
| t.Run("keeps free-form fallback when a dynamic ref is empty", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| fastVars := ast.NewVars() | ||
| fastVars.Set("AVAILABLE_SERVICES", ast.Var{Value: "", Sh: strPtr("printf ''")}) | ||
|
|
||
| v := &ast.VarsWithValidation{Name: "SERVICE", Enum: &ast.Enum{Ref: ".AVAILABLE_SERVICES | splitLines | compact"}} | ||
|
|
||
| require.Empty(t, getEnumValues(e.resolveEnumRefForPrompt(v, fastVars, dir).Enum)) | ||
| }) | ||
|
|
||
| t.Run("resolves a dynamic ref even when an unrelated sh var fails", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| fastVars := ast.NewVars() | ||
| fastVars.Set("BROKEN", ast.Var{Value: "", Sh: strPtr("exit 1")}) | ||
| fastVars.Set("AVAILABLE_SERVICES", ast.Var{Value: "", Sh: strPtr("printf 'api\nweb\ndb\n'")}) | ||
|
|
||
| v := &ast.VarsWithValidation{Name: "SERVICE", Enum: &ast.Enum{Ref: ".AVAILABLE_SERVICES | splitLines | compact"}} | ||
|
|
||
| resolved := e.resolveEnumRefForPrompt(v, fastVars, dir) | ||
|
|
||
| require.Equal(t, []string{"api", "web", "db"}, getEnumValues(resolved.Enum)) | ||
| }) | ||
| } | ||
|
|
||
| func strPtr(s string) *string { | ||
| return &s | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| version: '3' | ||
|
|
||
| vars: | ||
| AVAILABLE_SERVICES: | ||
| sh: printf 'api\nweb\ndb\n' | ||
| ALLOWED_ENVS: | ||
| - dev | ||
| - staging | ||
| - prod | ||
| NO_SERVICES: | ||
| sh: printf '' | ||
|
|
||
| tasks: | ||
| deploy: | ||
| desc: Deploy a service chosen interactively from a dynamic sh variable | ||
| requires: | ||
| vars: | ||
| - name: SERVICE | ||
| enum: | ||
| ref: .AVAILABLE_SERVICES | splitLines | compact | ||
| cmds: | ||
| - echo "Deploying {{.SERVICE}}" | ||
|
|
||
| release: | ||
| desc: Release to an environment chosen from a static list var | ||
| requires: | ||
| vars: | ||
| - name: ENV | ||
| enum: | ||
| ref: .ALLOWED_ENVS | ||
| cmds: | ||
| - echo "Releasing to {{.ENV}}" | ||
|
|
||
| deploy-empty: | ||
| desc: Dynamic sh variable produces no values, falls back to free-form input | ||
| requires: | ||
| vars: | ||
| - name: SERVICE | ||
| enum: | ||
| ref: .NO_SERVICES | splitLines | compact | ||
| cmds: | ||
| - echo "Deploying {{.SERVICE}}" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
for _, v ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and if wrong, perhaps why the unit test does not fail.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question vars.Values() returns iter.Seq[Var] (a single-value iterator, not (k, v) pairs), so for v := range vars.Values() yields each Var directly. for _, v := range wouldn't compile here since iter.Seq[Var] ranges over exactly one variable.
On the test question it's guarded by TestPromptEnumRefResolution — if hasDynamicVars returned false for the dynamic sh: fixture, the enum would come back empty and that test would fail. (And hasDynamicVars is really just a cheap guard; evaluateDynamicVarsForPrompt handles the no-op case fine either way.)