Skip to content

test: unit test to document validation behavior of parameters #387

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

Merged
merged 4 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
extract tests to a file
  • Loading branch information
Emyrk committed Apr 30, 2025
commit cec2d970ad44291b21fd40c51eca69336ef561f0
61 changes: 10 additions & 51 deletions provider/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider_test

import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -703,55 +704,8 @@ func TestParameterValidationEnforcement(t *testing.T) {
// - [NumIns/DefInv] So the default can be invalid if an input value is valid.
// The value is therefore not really optional, but it is marked as such.
// - [NumInsNotOptsVal | NumsInsNotOpts] values do not need to be in the option set?

table := strings.TrimSpace(`
| Name | Type | Input Value | Default | Options | Validation | -> | Output Value | Optional | Error |
|---------------------|---------------|-------------|---------|-------------------|------------|----|--------------|----------|--------------|
| | Empty Vals | | | | | | | | |
| Emty | string,number | | | | | | "" | false | |
| EmtyOpts | string,number | | | 1,2,3 | | | "" | false | |
| EmtyRegex | string | | | | world | | | | regex error |
| EmtyMin | number | | | | 1-10 | | | | 1 < < 10 |
| EmtyMinOpt | number | | | 1,2,3 | 2-5 | | | | 2 < < 5 |
| EmtyRegexOpt | string | | | "hello","goodbye" | goodbye | | | | regex error |
| EmtyRegexOk | string | | | | .* | | "" | false | |
| | | | | | | | | | |
| | Default Set | No inputs | | | | | | | |
| NumDef | number | | 5 | | | | 5 | true | |
| NumDefVal | number | | 5 | | 3-7 | | 5 | true | |
| NumDefInv | number | | 5 | | 10- | | | | 10 < 5 < 0 |
| NumDefOpts | number | | 5 | 1,3,5,7 | 2-6 | | 5 | true | |
| NumDefNotOpts | number | | 5 | 1,3,7,9 | 2-6 | | | | valid option |
| NumDefInvOpt | number | | 5 | 1,3,5,7 | 6-10 | | | | 6 < 5 < 10 |
| | | | | | | | | | |
| StrDef | string | | hello | | | | hello | true | |
| StrDefInv | string | | hello | | world | | | | regex error |
| StrDefOpts | string | | a | a,b,c | | | a | true | |
| StrDefNotOpts | string | | a | b,c,d | | | | | valid option |
| StrDefOpts | string | | a | a,b,c,d,e,f | [a-c] | | a | true | |
| StrDefInvOpt | string | | d | a,b,c,d,e,f | [a-c] | | | | regex error |
| | | | | | | | | | |
| | Input Vals | | | | | | | | |
| NumIns | number | 3 | | | | | 3 | false | |
| NumInsDef | number | 3 | 5 | | | | 3 | true | |
| NumIns/DefInv | number | 3 | 5 | | 1-3 | | 3 | true | |
| NumIns=DefInv | number | 5 | 5 | | 1-3 | | | | 1 < 5 < 3 |
| NumInsOpts | number | 3 | 5 | 1,2,3,4,5 | 1-3 | | 3 | true | |
| NumInsNotOptsVal | number | 3 | 5 | 1,2,4,5 | 1-3 | | 3 | true | |
| NumInsNotOptsInv | number | 3 | 5 | 1,2,4,5 | 1-2 | | | true | 1 < 3 < 2 |
| NumInsNotOpts | number | 3 | 5 | 1,2,4,5 | | | 3 | true | |
| NumInsNotOpts/NoDef | number | 3 | | 1,2,4,5 | | | 3 | false | |
| | | | | | | | | | |
| StrIns | string | c | | | | | c | false | |
| StrInsDef | string | c | e | | | | c | true | |
| StrIns/DefInv | string | c | e | | [a-c] | | c | true | |
| NumIns=DefInv | string | e | e | | [a-c] | | | | regex error |
| StrInsOpts | string | c | e | a,b,c,d,e | [a-c] | | c | true | |
| StrInsNotOptsVal | string | c | e | a,b,d,e | [a-c] | | c | true | |
| StrInsNotOptsInv | string | c | e | a,b,d,e | [a-b] | | | | regex error |
| StrInsNotOpts | string | c | e | a,b,d,e | | | c | true | |
| StrInsNotOpts/NoDef | string | c | | a,b,d,e | | | c | false | |
`)
table, err := os.ReadFile("testdata/parameter_table.md")
require.NoError(t, err)

type row struct {
Name string
Expand All @@ -766,7 +720,7 @@ func TestParameterValidationEnforcement(t *testing.T) {
}

rows := make([]row, 0)
lines := strings.Split(table, "\n")
lines := strings.Split(string(table), "\n")
validMinMax := regexp.MustCompile("^[0-9]*-[0-9]*$")
for _, line := range lines[2:] {
columns := strings.Split(line, "|")
Expand Down Expand Up @@ -867,7 +821,12 @@ func TestParameterValidationEnforcement(t *testing.T) {
var cfg strings.Builder
cfg.WriteString("data \"coder_parameter\" \"parameter\" {\n")
cfg.WriteString("\tname = \"parameter\"\n")
cfg.WriteString(fmt.Sprintf("\ttype = \"%s\"\n", rt))
if rt == "multi-select" || rt == "tag-select" {
cfg.WriteString(fmt.Sprintf("\ttype = \"%s\"\n", "list(string)"))
cfg.WriteString(fmt.Sprintf("\tform_type = \"%s\"\n", rt))
} else {
cfg.WriteString(fmt.Sprintf("\ttype = \"%s\"\n", rt))
}
if row.Default != "" {
cfg.WriteString(fmt.Sprintf("\tdefault = %s\n", stringLiteral(row.Default)))
}
Expand Down
70 changes: 70 additions & 0 deletions provider/testdata/parameter_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
| Name | Type | Input | Default | Options | Validation | -> | Output | Optional | Error |
|----------------------|---------------|-----------|---------|-------------------|------------|----|--------|----------|--------------|
| | Empty Vals | | | | | | | | |
| Empty | string,number | | | | | | "" | false | |
| EmptyList | list(string) | | | | | | "" | false | |
| EmptyMulti | tag-select | | | | | | "" | false | |
| EmptyOpts | string,number | | | 1,2,3 | | | "" | false | |
| EmptyRegex | string | | | | world | | | | regex error |
| EmptyMin | number | | | | 1-10 | | | | 1 < < 10 |
| EmptyMinOpt | number | | | 1,2,3 | 2-5 | | | | 2 < < 5 |
| EmptyRegexOpt | string | | | "hello","goodbye" | goodbye | | | | regex error |
| EmptyRegexOk | string | | | | .* | | "" | false | |
| | | | | | | | | | |
| | Default Set | No inputs | | | | | | | |
| NumDef | number | | 5 | | | | 5 | true | |
| NumDefVal | number | | 5 | | 3-7 | | 5 | true | |
| NumDefInv | number | | 5 | | 10- | | | | 10 < 5 < 0 |
| NumDefOpts | number | | 5 | 1,3,5,7 | 2-6 | | 5 | true | |
| NumDefNotOpts | number | | 5 | 1,3,7,9 | 2-6 | | | | valid option |
| NumDefInvOpt | number | | 5 | 1,3,5,7 | 6-10 | | | | 6 < 5 < 10 |
| | | | | | | | | | |
| StrDef | string | | hello | | | | hello | true | |
| StrDefInv | string | | hello | | world | | | | regex error |
| StrDefOpts | string | | a | a,b,c | | | a | true | |
| StrDefNotOpts | string | | a | b,c,d | | | | | valid option |
| StrDefValOpts | string | | a | a,b,c,d,e,f | [a-c] | | a | true | |
| StrDefInvOpt | string | | d | a,b,c,d,e,f | [a-c] | | | | regex error |
| | | | | | | | | | |
| LStrDef | list(string) | | ["a"] | | | | ["a"] | true | |
| LStrDefOpts | list(string) | | ["a"] | ["a"], ["b"] | | | ["a"] | true | |
| LStrDefNotOpts | list(string) | | ["a"] | ["b"], ["c"] | | | | | valid option |
| | | | | | | | | | |
| MulDef | tag-select | | ["a"] | | | | ["a"] | true | |
| MulDefOpts | multi-select | | ["a"] | a,b | | | ["a"] | true | |
| MulDefNotOpts | multi-select | | ["a"] | b,c | | | | | valid option |
| | | | | | | | | | |
| | Input Vals | | | | | | | | |
| NumIns | number | 3 | | | | | 3 | false | |
| NumInsDef | number | 3 | 5 | | | | 3 | true | |
| NumIns/DefInv | number | 3 | 5 | | 1-3 | | 3 | true | |
| NumIns=DefInv | number | 5 | 5 | | 1-3 | | | | 1 < 5 < 3 |
| NumInsOpts | number | 3 | 5 | 1,2,3,4,5 | 1-3 | | 3 | true | |
| NumInsNotOptsVal | number | 3 | 5 | 1,2,4,5 | 1-3 | | 3 | true | |
| NumInsNotOptsInv | number | 3 | 5 | 1,2,4,5 | 1-2 | | | true | 1 < 3 < 2 |
| NumInsNotOpts | number | 3 | 5 | 1,2,4,5 | | | 3 | true | |
| NumInsNotOpts/NoDef | number | 3 | | 1,2,4,5 | | | 3 | false | |
| | | | | | | | | | |
| StrIns | string | c | | | | | c | false | |
| StrInsDef | string | c | e | | | | c | true | |
| StrIns/DefInv | string | c | e | | [a-c] | | c | true | |
| StrIns=DefInv | string | e | e | | [a-c] | | | | regex error |
| StrInsOpts | string | c | e | a,b,c,d,e | [a-c] | | c | true | |
| StrInsNotOptsVal | string | c | e | a,b,d,e | [a-c] | | c | true | |
| StrInsNotOptsInv | string | c | e | a,b,d,e | [a-b] | | | | regex error |
| StrInsNotOpts | string | c | e | a,b,d,e | | | c | true | |
| StrInsNotOpts/NoDef | string | c | | a,b,d,e | | | c | false | |
| StrInsBadVal | string | c | | a,b,c,d,e | 1-10 | | | | min cannot |
| | | | | | | | | | |
| | list(string) | | | | | | | | |
| LStrIns | list(string) | ["c"] | | | | | ["c"] | false | |
| LStrInsDef | list(string) | ["c"] | ["e"] | | | | ["c"] | true | |
| LStrIns/DefInv | list(string) | ["c"] | ["e"] | | [a-c] | | | | regex cannot |
| LStrInsOpts | list(string) | ["c"] | ["e"] | ["c"],["d"],["e"] | | | ["c"] | true | |
| LStrInsNotOpts | list(string) | ["c"] | ["e"] | ["d"],["e"] | | | ["c"] | true | |
| LStrInsNotOpts/NoDef | list(string) | ["c"] | | ["d"],["e"] | | | ["c"] | false | |
| | | | | | | | | | |
| MulInsOpts | multi-select | ["c"] | ["e"] | c,d,e | | | ["c"] | true | |
| MulInsNotOpts | multi-select | ["c"] | ["e"] | d,e | | | ["c"] | true | |
| MulInsNotOpts/NoDef | multi-select | ["c"] | | d,e | | | ["c"] | false | |
| MulInsInvOpts | multi-select | ["c"] | ["e"] | c,d,e | [a-c] | | | | regex cannot |
Loading