test: unit test to document validation behavior of parameters#387
Merged
Conversation
Emyrk
commented
Apr 30, 2025
Comment on lines
+1055
to
1074
| }, { | ||
| Name: "ValidListOfStrings", | ||
| Type: "list(string)", | ||
| Value: `["first","second","third"]`, | ||
| MinDisabled: true, | ||
| MaxDisabled: true, | ||
| }, { | ||
| Name: "InvalidListOfStrings", | ||
| Type: "list(string)", | ||
| Value: `["first","second","third"`, | ||
| MinDisabled: true, | ||
| MaxDisabled: true, | ||
| Error: regexp.MustCompile("is not valid list of strings"), | ||
| }, { | ||
| Name: "EmptyListOfStrings", | ||
| Type: "list(string)", | ||
| Value: `[]`, | ||
| MinDisabled: true, | ||
| MaxDisabled: true, | ||
| }} { |
Member
Author
There was a problem hiding this comment.
Added some extra test cases down here too
johnstcn
reviewed
Apr 30, 2025
Comment on lines
+771
to
+831
| for _, line := range lines[2:] { | ||
| columns := strings.Split(line, "|") | ||
| columns = columns[1 : len(columns)-1] | ||
| for i := range columns { | ||
| // Trim the whitespace from all columns | ||
| columns[i] = strings.TrimSpace(columns[i]) | ||
| } | ||
|
|
||
| if columns[0] == "" { | ||
| continue // Skip rows with empty names | ||
| } | ||
|
|
||
| optional, err := strconv.ParseBool(columns[8]) | ||
| if columns[8] != "" { | ||
| // Value does not matter if not specified | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| var rerr *regexp.Regexp | ||
| if columns[9] != "" { | ||
| rerr, err = regexp.Compile(columns[9]) | ||
| if err != nil { | ||
| t.Fatalf("failed to parse error column %q: %v", columns[9], err) | ||
| } | ||
| } | ||
| var options []string | ||
| if columns[4] != "" { | ||
| options = strings.Split(columns[4], ",") | ||
| } | ||
|
|
||
| var validation *provider.Validation | ||
| if columns[5] != "" { | ||
| // Min-Max validation should look like: | ||
| // 1-10 :: min=1, max=10 | ||
| // -10 :: max=10 | ||
| // 1- :: min=1 | ||
| if validMinMax.MatchString(columns[5]) { | ||
| parts := strings.Split(columns[5], "-") | ||
| min, _ := strconv.ParseInt(parts[0], 10, 64) | ||
| max, _ := strconv.ParseInt(parts[1], 10, 64) | ||
| validation = &provider.Validation{ | ||
| Min: int(min), | ||
| MinDisabled: parts[0] == "", | ||
| Max: int(max), | ||
| MaxDisabled: parts[1] == "", | ||
| Monotonic: "", | ||
| Regex: "", | ||
| Error: "{min} < {value} < {max}", | ||
| } | ||
| } else { | ||
| validation = &provider.Validation{ | ||
| Min: 0, | ||
| MinDisabled: true, | ||
| Max: 0, | ||
| MaxDisabled: true, | ||
| Monotonic: "", | ||
| Regex: columns[5], | ||
| Error: "regex error", | ||
| } | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
suggestion: Extract this to a testutil package maybe?
Member
Author
There was a problem hiding this comment.
Since it cannot be reused outside the test, I am going to leave it here.
johnstcn
approved these changes
Apr 30, 2025
johnstcn
left a comment
Member
There was a problem hiding this comment.
Some comments below, but these are optional for a new unit test.
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Locking in the behavior of validations before I change things. My goal is to move all validation logic into this provider. At present, some of it is done in
coder/coder.If you see #381, adding more strict validation would be a regression in behavior. So I might have to add 2 validation modes so that template import can still be done with relaxed valiation.
Regardless, this test will help illustrate what I change when I add stricter validation.
I confirmed this test passes on a commit 3 weeks ago, before any of my refactors.
Some things we should probably fix or document:
coder/coderNOTE: Monotonic is not yet checked, it should be added into the terraform