@@ -696,35 +696,61 @@ data "coder_parameter" "region" {
696696//
697697//nolint:paralleltest,tparallel // Parameters load values from env vars
698698func TestParameterValidationEnforcement (t * testing.T ) {
699+ // Some interesting observations:
700+ // - Validation logic does not apply to the value of 'options'
701+ // - [NumDefInvOpt] So an invalid option can be present and selected, but would fail
702+ // - Validation logic does not apply to the default if a value is given
703+ // - [NumIns/DefInv] So the default can be invalid if an input value is valid.
704+ // The value is therefore not really optional, but it is marked as such.
705+ // - [NumInsNotOptsVal | NumsInsNotOpts] values do not need to be in the option set?
706+
699707 table := strings .TrimSpace (`
700- | Name | Type | Input Value | Default | Options | Validation | -> | Output Value | Optional | Error |
701- |---------------|---------------|-------------|---------|-------------------|------------|----|--------------|----------|--------------|
702- | | Empty Vals | | | | | | | | |
703- | Emty | string,number | | | | | | "" | false | |
704- | EmtyOpts | string,number | | | 1,2,3 | | | "" | false | |
705- | EmtyRegex | string | | | | world | | | | regex error |
706- | EmtyMin | number | | | | 1-10 | | | | 1 < < 10 |
707- | EmtyMinOpt | number | | | 1,2,3 | 2-5 | | | | 2 < < 5 |
708- | EmtyRegexOpt | string | | | "hello","goodbye" | goodbye | | | | regex error |
709- | EmtyRegexOk | string | | | | .* | | "" | false | |
710- | | | | | | | | | | |
711- | | Default Set | No inputs | | | | | | | |
712- | NumDef | number | | 5 | | | | 5 | true | |
713- | NumDefVal | number | | 5 | | 3-7 | | 5 | true | |
714- | NumDefInv | number | | 5 | | 10- | | 5 | | 10 < 5 < 0 |
715- | NumDefOpts | number | | 5 | 1,3,5,7 | 2-6 | | 5 | true | |
716- | NumDefNotOpts | number | | 5 | 1,3,7,9 | 2-6 | | | | valid option |
717- | | | | | | | | | | |
718- | StrDef | string | | hello | | | | hello | true | |
719- | StrDefInv | string | | hello | | world | | | | regex error |
720- | StrDefOpts | string | | a | a,b,c | | | a | true | |
721- | StrDefNotOpts | string | | a | b,c,d | | | | | valid option |
722- | StrDefOpts | string | | a | a,b,c,d,e,f | [a-c] | | a | true | |
723- | | | | | | | | | | |
724- | | Input Vals | | | | | | | | |
725- | NumIns | number | 3 | 5 | | | | 3 | true | |
726- | | | | | | | | | | |
727- | | | | | | | | | | |
708+ | Name | Type | Input Value | Default | Options | Validation | -> | Output Value | Optional | Error |
709+ |---------------------|---------------|-------------|---------|-------------------|------------|----|--------------|----------|--------------|
710+ | | Empty Vals | | | | | | | | |
711+ | Emty | string,number | | | | | | "" | false | |
712+ | EmtyOpts | string,number | | | 1,2,3 | | | "" | false | |
713+ | EmtyRegex | string | | | | world | | | | regex error |
714+ | EmtyMin | number | | | | 1-10 | | | | 1 < < 10 |
715+ | EmtyMinOpt | number | | | 1,2,3 | 2-5 | | | | 2 < < 5 |
716+ | EmtyRegexOpt | string | | | "hello","goodbye" | goodbye | | | | regex error |
717+ | EmtyRegexOk | string | | | | .* | | "" | false | |
718+ | | | | | | | | | | |
719+ | | Default Set | No inputs | | | | | | | |
720+ | NumDef | number | | 5 | | | | 5 | true | |
721+ | NumDefVal | number | | 5 | | 3-7 | | 5 | true | |
722+ | NumDefInv | number | | 5 | | 10- | | | | 10 < 5 < 0 |
723+ | NumDefOpts | number | | 5 | 1,3,5,7 | 2-6 | | 5 | true | |
724+ | NumDefNotOpts | number | | 5 | 1,3,7,9 | 2-6 | | | | valid option |
725+ | NumDefInvOpt | number | | 5 | 1,3,5,7 | 6-10 | | | | 6 < 5 < 10 |
726+ | | | | | | | | | | |
727+ | StrDef | string | | hello | | | | hello | true | |
728+ | StrDefInv | string | | hello | | world | | | | regex error |
729+ | StrDefOpts | string | | a | a,b,c | | | a | true | |
730+ | StrDefNotOpts | string | | a | b,c,d | | | | | valid option |
731+ | StrDefOpts | string | | a | a,b,c,d,e,f | [a-c] | | a | true | |
732+ | StrDefInvOpt | string | | d | a,b,c,d,e,f | [a-c] | | | | regex error |
733+ | | | | | | | | | | |
734+ | | Input Vals | | | | | | | | |
735+ | NumIns | number | 3 | | | | | 3 | false | |
736+ | NumInsDef | number | 3 | 5 | | | | 3 | true | |
737+ | NumIns/DefInv | number | 3 | 5 | | 1-3 | | 3 | true | |
738+ | NumIns=DefInv | number | 5 | 5 | | 1-3 | | | | 1 < 5 < 3 |
739+ | NumInsOpts | number | 3 | 5 | 1,2,3,4,5 | 1-3 | | 3 | true | |
740+ | NumInsNotOptsVal | number | 3 | 5 | 1,2,4,5 | 1-3 | | 3 | true | |
741+ | NumInsNotOptsInv | number | 3 | 5 | 1,2,4,5 | 1-2 | | | true | 1 < 3 < 2 |
742+ | NumInsNotOpts | number | 3 | 5 | 1,2,4,5 | | | 3 | true | |
743+ | NumInsNotOpts/NoDef | number | 3 | | 1,2,4,5 | | | 3 | false | |
744+ | | | | | | | | | | |
745+ | StrIns | string | c | | | | | c | false | |
746+ | StrInsDef | string | c | e | | | | c | true | |
747+ | StrIns/DefInv | string | c | e | | [a-c] | | c | true | |
748+ | NumIns=DefInv | string | e | e | | [a-c] | | | | regex error |
749+ | StrInsOpts | string | c | e | a,b,c,d,e | [a-c] | | c | true | |
750+ | StrInsNotOptsVal | string | c | e | a,b,d,e | [a-c] | | c | true | |
751+ | StrInsNotOptsInv | string | c | e | a,b,d,e | [a-b] | | | | regex error |
752+ | StrInsNotOpts | string | c | e | a,b,d,e | | | c | true | |
753+ | StrInsNotOpts/NoDef | string | c | | a,b,d,e | | | c | false | |
728754` )
729755
730756 type row struct {
@@ -832,6 +858,12 @@ func TestParameterValidationEnforcement(t *testing.T) {
832858 t .Setenv (provider .ParameterEnvironmentVariable ("parameter" ), row .InputValue )
833859 }
834860
861+ if row .Error != nil {
862+ if row .OutputValue != "" {
863+ t .Errorf ("output value %q should not be set if error is set" , row .OutputValue )
864+ }
865+ }
866+
835867 var cfg strings.Builder
836868 cfg .WriteString ("data \" coder_parameter\" \" parameter\" {\n " )
837869 cfg .WriteString ("\t name = \" parameter\" \n " )
@@ -896,7 +928,7 @@ func TestParameterValidationEnforcement(t *testing.T) {
896928 for key , expected := range map [string ]string {
897929 "optional" : strconv .FormatBool (row .Optional ),
898930 } {
899- require .Equal (t , expected , param .Primary .Attributes [key ])
931+ require .Equal (t , expected , param .Primary .Attributes [key ], "optional" )
900932 }
901933
902934 return nil
0 commit comments