mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
ab28ecde88
Fixes three bugs that caused `coder update` to always re-prompt for multi-select (`list(string)`) parameters instead of reusing previous build values: 1. **`isValidTemplateParameterOption` failed for multi-select values** (`cli/parameterresolver.go`): It compared the entire JSON array string (e.g. `["vim","emacs"]`) against individual option values, which never matched. Now parses the JSON array and validates each element separately. 2. **`RichParameter` ignored previous build value for multi-select** (`cli/cliui/parameter.go`): The `list(string)` branch always used the template's default value instead of the `defaultValue` argument (which carries the previous build's value). Now uses `defaultValue` when available, falling back to the template default. 3. **Pre-existing crash when `list(string)` has no default value** (`cli/cliui/parameter.go`): `json.Unmarshal` on an empty string caused `unexpected end of JSON input`. Now skips unmarshaling when the default source is empty. Fixes #19956
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
func TestIsValidTemplateParameterOption(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
options := []codersdk.TemplateVersionParameterOption{
|
|
{Name: "Vim", Value: "vim"},
|
|
{Name: "Emacs", Value: "emacs"},
|
|
{Name: "VS Code", Value: "vscode"},
|
|
}
|
|
|
|
t.Run("SingleSelectValid", func(t *testing.T) {
|
|
t.Parallel()
|
|
bp := codersdk.WorkspaceBuildParameter{Name: "editor", Value: "vim"}
|
|
tvp := codersdk.TemplateVersionParameter{
|
|
Name: "editor",
|
|
Type: "string",
|
|
Options: options,
|
|
}
|
|
assert.True(t, isValidTemplateParameterOption(bp, tvp))
|
|
})
|
|
|
|
t.Run("SingleSelectInvalid", func(t *testing.T) {
|
|
t.Parallel()
|
|
bp := codersdk.WorkspaceBuildParameter{Name: "editor", Value: "notepad"}
|
|
tvp := codersdk.TemplateVersionParameter{
|
|
Name: "editor",
|
|
Type: "string",
|
|
Options: options,
|
|
}
|
|
assert.False(t, isValidTemplateParameterOption(bp, tvp))
|
|
})
|
|
|
|
t.Run("MultiSelectAllValid", func(t *testing.T) {
|
|
t.Parallel()
|
|
bp := codersdk.WorkspaceBuildParameter{Name: "editors", Value: `["vim","emacs"]`}
|
|
tvp := codersdk.TemplateVersionParameter{
|
|
Name: "editors",
|
|
Type: "list(string)",
|
|
Options: options,
|
|
}
|
|
assert.True(t, isValidTemplateParameterOption(bp, tvp))
|
|
})
|
|
|
|
t.Run("MultiSelectOneInvalid", func(t *testing.T) {
|
|
t.Parallel()
|
|
bp := codersdk.WorkspaceBuildParameter{Name: "editors", Value: `["vim","notepad"]`}
|
|
tvp := codersdk.TemplateVersionParameter{
|
|
Name: "editors",
|
|
Type: "list(string)",
|
|
Options: options,
|
|
}
|
|
assert.False(t, isValidTemplateParameterOption(bp, tvp))
|
|
})
|
|
|
|
t.Run("MultiSelectEmptyArray", func(t *testing.T) {
|
|
t.Parallel()
|
|
bp := codersdk.WorkspaceBuildParameter{Name: "editors", Value: `[]`}
|
|
tvp := codersdk.TemplateVersionParameter{
|
|
Name: "editors",
|
|
Type: "list(string)",
|
|
Options: options,
|
|
}
|
|
assert.True(t, isValidTemplateParameterOption(bp, tvp))
|
|
})
|
|
|
|
t.Run("MultiSelectInvalidJSON", func(t *testing.T) {
|
|
t.Parallel()
|
|
bp := codersdk.WorkspaceBuildParameter{Name: "editors", Value: `not-json`}
|
|
tvp := codersdk.TemplateVersionParameter{
|
|
Name: "editors",
|
|
Type: "list(string)",
|
|
Options: options,
|
|
}
|
|
assert.False(t, isValidTemplateParameterOption(bp, tvp))
|
|
})
|
|
}
|