@@ -689,7 +689,166 @@ data "coder_parameter" "region" {
689
689
}
690
690
}
691
691
692
- << << << < HEAD
692
+ func TestParameterValidation (t * testing.T ) {
693
+ t .Parallel ()
694
+ opts := func (vals ... string ) []provider.Option {
695
+ options := make ([]provider.Option , 0 , len (vals ))
696
+ for _ , val := range vals {
697
+ options = append (options , provider.Option {
698
+ Name : val ,
699
+ Value : val ,
700
+ })
701
+ }
702
+ return options
703
+ }
704
+
705
+ for _ , tc := range []struct {
706
+ Name string
707
+ Parameter provider.Parameter
708
+ Value string
709
+ ExpectError * regexp.Regexp
710
+ }{
711
+ {
712
+ Name : "ValidStringParameter" ,
713
+ Parameter : provider.Parameter {
714
+ Type : "string" ,
715
+ },
716
+ Value : "alpha" ,
717
+ },
718
+ // Test invalid states
719
+ {
720
+ Name : "InvalidFormType" ,
721
+ Parameter : provider.Parameter {
722
+ Type : "string" ,
723
+ Option : opts ("alpha" , "bravo" , "charlie" ),
724
+ FormType : provider .ParameterFormTypeSlider ,
725
+ },
726
+ Value : "alpha" ,
727
+ ExpectError : regexp .MustCompile ("Invalid form_type for parameter" ),
728
+ },
729
+ {
730
+ Name : "NotInOptions" ,
731
+ Parameter : provider.Parameter {
732
+ Type : "string" ,
733
+ Option : opts ("alpha" , "bravo" , "charlie" ),
734
+ },
735
+ Value : "delta" , // not in option set
736
+ ExpectError : regexp .MustCompile ("Value must be a valid option" ),
737
+ },
738
+ {
739
+ Name : "NumberNotInOptions" ,
740
+ Parameter : provider.Parameter {
741
+ Type : "number" ,
742
+ Option : opts ("1" , "2" , "3" ),
743
+ },
744
+ Value : "0" , // not in option set
745
+ ExpectError : regexp .MustCompile ("Value must be a valid option" ),
746
+ },
747
+ {
748
+ Name : "NonUniqueOptionNames" ,
749
+ Parameter : provider.Parameter {
750
+ Type : "string" ,
751
+ Option : opts ("alpha" , "alpha" ),
752
+ },
753
+ Value : "alpha" ,
754
+ ExpectError : regexp .MustCompile ("Option names must be unique" ),
755
+ },
756
+ {
757
+ Name : "NonUniqueOptionValues" ,
758
+ Parameter : provider.Parameter {
759
+ Type : "string" ,
760
+ Option : []provider.Option {
761
+ {Name : "Alpha" , Value : "alpha" },
762
+ {Name : "AlphaAgain" , Value : "alpha" },
763
+ },
764
+ },
765
+ Value : "alpha" ,
766
+ ExpectError : regexp .MustCompile ("Option values must be unique" ),
767
+ },
768
+ {
769
+ Name : "IncorrectValueTypeOption" ,
770
+ Parameter : provider.Parameter {
771
+ Type : "number" ,
772
+ Option : opts ("not-a-number" ),
773
+ },
774
+ Value : "5" ,
775
+ ExpectError : regexp .MustCompile ("is not a number" ),
776
+ },
777
+ {
778
+ Name : "IncorrectValueType" ,
779
+ Parameter : provider.Parameter {
780
+ Type : "number" ,
781
+ },
782
+ Value : "not-a-number" ,
783
+ ExpectError : regexp .MustCompile ("Parameter value is not of type \" number\" " ),
784
+ },
785
+ {
786
+ Name : "NotListStringDefault" ,
787
+ Parameter : provider.Parameter {
788
+ Type : "list(string)" ,
789
+ Default : "not-a-list" ,
790
+ },
791
+ ExpectError : regexp .MustCompile ("not a valid list of strings" ),
792
+ },
793
+ {
794
+ Name : "NotListStringDefault" ,
795
+ Parameter : provider.Parameter {
796
+ Type : "list(string)" ,
797
+ },
798
+ Value : "not-a-list" ,
799
+ ExpectError : regexp .MustCompile ("not a valid list of strings" ),
800
+ },
801
+ {
802
+ Name : "DefaultListStringNotInOptions" ,
803
+ Parameter : provider.Parameter {
804
+ Type : "list(string)" ,
805
+ Default : `["red", "yellow", "black"]` ,
806
+ Option : opts ("red" , "blue" , "green" ),
807
+ FormType : provider .ParameterFormTypeMultiSelect ,
808
+ },
809
+ ExpectError : regexp .MustCompile ("is not a valid option, values \" yellow, black\" are missing from the options" ),
810
+ },
811
+ {
812
+ Name : "ListStringNotInOptions" ,
813
+ Parameter : provider.Parameter {
814
+ Type : "list(string)" ,
815
+ Default : `["red"]` ,
816
+ Option : opts ("red" , "blue" , "green" ),
817
+ FormType : provider .ParameterFormTypeMultiSelect ,
818
+ },
819
+ Value : `["red", "yellow", "black"]` ,
820
+ ExpectError : regexp .MustCompile ("is not a valid option, values \" yellow, black\" are missing from the options" ),
821
+ },
822
+ {
823
+ Name : "InvalidMiniumum" ,
824
+ Parameter : provider.Parameter {
825
+ Type : "number" ,
826
+ Default : "5" ,
827
+ Validation : []provider.Validation {{
828
+ Min : 10 ,
829
+ Error : "must be greater than 10" ,
830
+ }},
831
+ },
832
+ ExpectError : regexp .MustCompile ("must be greater than 10" ),
833
+ },
834
+ } {
835
+ tc := tc
836
+ t .Run (tc .Name , func (t * testing.T ) {
837
+ t .Parallel ()
838
+ diags := tc .Parameter .Valid (tc .Value )
839
+ if tc .ExpectError != nil {
840
+ require .True (t , diags .HasError ())
841
+ errMsg := fmt .Sprintf ("%+v" , diags [0 ]) // close enough
842
+ require .Truef (t , tc .ExpectError .MatchString (errMsg ), "got: %s" , errMsg )
843
+ } else {
844
+ if ! assert .False (t , diags .HasError ()) {
845
+ t .Logf ("got: %+v" , diags [0 ])
846
+ }
847
+ }
848
+ })
849
+ }
850
+ }
851
+
693
852
// TestParameterValidationEnforcement tests various parameter states and the
694
853
// validation enforcement that should be applied to them. The table is described
695
854
// by a markdown table. This is done so that the test cases can be more easily
@@ -898,165 +1057,6 @@ func TestParameterValidationEnforcement(t *testing.T) {
898
1057
})
899
1058
})
900
1059
}
901
- == == == =
902
- func TestParameterValidation (t * testing .T ) {
903
- t .Parallel ()
904
- opts := func (vals ... string ) []provider.Option {
905
- options := make ([]provider.Option , 0 , len (vals ))
906
- for _ , val := range vals {
907
- options = append (options , provider.Option {
908
- Name : val ,
909
- Value : val ,
910
- })
911
- }
912
- return options
913
- }
914
-
915
- for _ , tc := range []struct {
916
- Name string
917
- Parameter provider.Parameter
918
- Value string
919
- ExpectError * regexp.Regexp
920
- }{
921
- {
922
- Name : "ValidStringParameter" ,
923
- Parameter : provider.Parameter {
924
- Type : "string" ,
925
- },
926
- Value : "alpha" ,
927
- },
928
- // Test invalid states
929
- {
930
- Name : "InvalidFormType" ,
931
- Parameter : provider.Parameter {
932
- Type : "string" ,
933
- Option : opts ("alpha" , "bravo" , "charlie" ),
934
- FormType : provider .ParameterFormTypeSlider ,
935
- },
936
- Value : "alpha" ,
937
- ExpectError : regexp .MustCompile ("Invalid form_type for parameter" ),
938
- },
939
- {
940
- Name : "NotInOptions" ,
941
- Parameter : provider.Parameter {
942
- Type : "string" ,
943
- Option : opts ("alpha" , "bravo" , "charlie" ),
944
- },
945
- Value : "delta" , // not in option set
946
- ExpectError : regexp .MustCompile ("Value must be a valid option" ),
947
- },
948
- {
949
- Name : "NumberNotInOptions" ,
950
- Parameter : provider.Parameter {
951
- Type : "number" ,
952
- Option : opts ("1" , "2" , "3" ),
953
- },
954
- Value : "0" , // not in option set
955
- ExpectError : regexp .MustCompile ("Value must be a valid option" ),
956
- },
957
- {
958
- Name : "NonUniqueOptionNames" ,
959
- Parameter : provider.Parameter {
960
- Type : "string" ,
961
- Option : opts ("alpha" , "alpha" ),
962
- },
963
- Value : "alpha" ,
964
- ExpectError : regexp .MustCompile ("Option names must be unique" ),
965
- },
966
- {
967
- Name : "NonUniqueOptionValues" ,
968
- Parameter : provider.Parameter {
969
- Type : "string" ,
970
- Option : []provider.Option {
971
- {Name : "Alpha" , Value : "alpha" },
972
- {Name : "AlphaAgain" , Value : "alpha" },
973
- },
974
- },
975
- Value : "alpha" ,
976
- ExpectError : regexp .MustCompile ("Option values must be unique" ),
977
- },
978
- {
979
- Name : "IncorrectValueTypeOption" ,
980
- Parameter : provider.Parameter {
981
- Type : "number" ,
982
- Option : opts ("not-a-number" ),
983
- },
984
- Value : "5" ,
985
- ExpectError : regexp .MustCompile ("is not a number" ),
986
- },
987
- {
988
- Name : "IncorrectValueType" ,
989
- Parameter : provider.Parameter {
990
- Type : "number" ,
991
- },
992
- Value : "not-a-number" ,
993
- ExpectError : regexp .MustCompile ("Parameter value is not of type \" number\" " ),
994
- },
995
- {
996
- Name : "NotListStringDefault" ,
997
- Parameter : provider.Parameter {
998
- Type : "list(string)" ,
999
- Default : "not-a-list" ,
1000
- },
1001
- ExpectError : regexp .MustCompile ("not a valid list of strings" ),
1002
- },
1003
- {
1004
- Name : "NotListStringDefault" ,
1005
- Parameter : provider.Parameter {
1006
- Type : "list(string)" ,
1007
- },
1008
- Value : "not-a-list" ,
1009
- ExpectError : regexp .MustCompile ("not a valid list of strings" ),
1010
- },
1011
- {
1012
- Name : "DefaultListStringNotInOptions" ,
1013
- Parameter : provider.Parameter {
1014
- Type : "list(string)" ,
1015
- Default : `["red", "yellow", "black"]` ,
1016
- Option : opts ("red" , "blue" , "green" ),
1017
- FormType : provider .ParameterFormTypeMultiSelect ,
1018
- },
1019
- ExpectError : regexp .MustCompile ("is not a valid option, values \" yellow, black\" are missing from the options" ),
1020
- },
1021
- {
1022
- Name : "ListStringNotInOptions" ,
1023
- Parameter : provider.Parameter {
1024
- Type : "list(string)" ,
1025
- Default : `["red"]` ,
1026
- Option : opts ("red" , "blue" , "green" ),
1027
- FormType : provider .ParameterFormTypeMultiSelect ,
1028
- },
1029
- Value : `["red", "yellow", "black"]` ,
1030
- ExpectError : regexp .MustCompile ("is not a valid option, values \" yellow, black\" are missing from the options" ),
1031
- },
1032
- {
1033
- Name : "InvalidMiniumum" ,
1034
- Parameter : provider.Parameter {
1035
- Type : "number" ,
1036
- Default : "5" ,
1037
- Validation : []provider.Validation {{
1038
- Min : 10 ,
1039
- Error : "must be greater than 10" ,
1040
- }},
1041
- },
1042
- ExpectError : regexp .MustCompile ("must be greater than 10" ),
1043
- },
1044
- } {
1045
- tc := tc
1046
- t .Run (tc .Name , func (t * testing.T ) {
1047
- t .Parallel ()
1048
- diags := tc .Parameter .Valid (tc .Value )
1049
- if tc .ExpectError != nil {
1050
- require .True (t , diags .HasError ())
1051
- errMsg := fmt .Sprintf ("%+v" , diags [0 ]) // close enough
1052
- require .Truef (t , tc .ExpectError .MatchString (errMsg ), "got: %s" , errMsg )
1053
- } else {
1054
- if ! assert .False (t , diags .HasError ()) {
1055
- t .Logf ("got: %+v" , diags [0 ])
1056
- }
1057
- }
1058
- })
1059
- >> >> >> > c640b02 (begin adding invalid unit tests )
1060
1060
}
1061
1061
}
1062
1062
0 commit comments