@@ -16,6 +16,7 @@ import (
1616 "github.com/zclconf/go-cty/cty"
1717
1818 "github.com/coder/preview"
19+ "github.com/coder/preview/hclext"
1920 "github.com/coder/preview/types"
2021 "github.com/coder/terraform-provider-coder/v2/provider"
2122)
@@ -43,6 +44,7 @@ func Test_Extract(t *testing.T) {
4344 expTags map [string ]string
4445 unknownTags []string
4546 params map [string ]assertParam
47+ variables map [string ]assertVariable
4648 presets func (t * testing.T , presets []types.Preset )
4749 warnings []* regexp.Regexp
4850 }{
@@ -77,6 +79,17 @@ func Test_Extract(t *testing.T) {
7779 formType (provider .ParameterFormTypeRadio ),
7880 "numerical" : ap ().value ("5" ),
7981 },
82+ variables : map [string ]assertVariable {
83+ "string" : av ().def (cty .StringVal ("Hello, world!" )).typeEq (cty .String ),
84+ "number" : av ().def (cty .NumberIntVal (7 )).typeEq (cty .Number ),
85+ "boolean" : av ().def (cty .BoolVal (true )).typeEq (cty .Bool ),
86+ "coerce_string" : av ().def (cty .StringVal ("5" )).typeEq (cty .String ),
87+ "complex" : av ().typeEq (cty .Object (map [string ]cty.Type {
88+ "list" : cty .List (cty .String ),
89+ "name" : cty .String ,
90+ "age" : cty .Number ,
91+ })),
92+ },
8093 },
8194 {
8295 name : "conditional-no-inputs" ,
@@ -637,10 +650,68 @@ func Test_Extract(t *testing.T) {
637650 if tc .presets != nil {
638651 tc .presets (t , output .Presets )
639652 }
653+
654+ // Assert variables
655+ require .Len (t , output .Variables , len (tc .variables ), "wrong number of variables expected" )
656+ for _ , variable := range output .Variables {
657+ check , ok := tc .variables [variable .Name ]
658+ require .True (t , ok , "unknown variable %s" , variable .Name )
659+ check (t , variable )
660+ }
640661 })
641662 }
642663}
643664
665+ type assertVariable func (t * testing.T , variable types.Variable )
666+
667+ func av () assertVariable {
668+ return func (t * testing.T , v types.Variable ) {
669+ t .Helper ()
670+ assert .Empty (t , v .Diagnostics , "variable should have no diagnostics" )
671+ }
672+ }
673+
674+ func (a assertVariable ) nullable (n bool ) assertVariable {
675+ return a .extend (func (t * testing.T , v types.Variable ) {
676+ assert .Equal (t , v .Nullable , n , "variable nullable check" )
677+ })
678+ }
679+
680+ func (a assertVariable ) typeEq (ty cty.Type ) assertVariable {
681+ return a .extend (func (t * testing.T , v types.Variable ) {
682+ assert .Truef (t , ty .Equals (v .Type ), "%q variable type equality check" , v .Name )
683+ })
684+ }
685+
686+ func (a assertVariable ) def (def cty.Value ) assertVariable {
687+ return a .extend (func (t * testing.T , v types.Variable ) {
688+ if ! assert .Truef (t , def .Equals (v .Default ).True (), "%q variable default equality check" , v .Name ) {
689+ exp , _ := hclext .AsString (def )
690+ got , _ := hclext .AsString (v .Default )
691+ t .Logf ("Expected: %s, Value: %s" , exp , got )
692+ }
693+
694+ })
695+ }
696+
697+ func (a assertVariable ) sensitive (s bool ) assertVariable {
698+ return a .extend (func (t * testing.T , v types.Variable ) {
699+ assert .Equal (t , v .Sensitive , s , "variable sensitive check" )
700+ })
701+ }
702+
703+ func (a assertVariable ) ephemeral (e bool ) assertVariable {
704+ return a .extend (func (t * testing.T , v types.Variable ) {
705+ assert .Equal (t , v .Ephemeral , e , "variable ephemeral check" )
706+ })
707+ }
708+
709+ func (a assertVariable ) description (d string ) assertVariable {
710+ return a .extend (func (t * testing.T , v types.Variable ) {
711+ assert .Equal (t , v .Description , d , "variable description check" )
712+ })
713+ }
714+
644715type assertParam func (t * testing.T , parameter types.Parameter )
645716
646717func ap () assertParam {
@@ -771,3 +842,16 @@ func (a assertParam) extend(f assertParam) assertParam {
771842 f (t , parameter )
772843 }
773844}
845+
846+ //nolint:revive
847+ func (a assertVariable ) extend (f assertVariable ) assertVariable {
848+ if a == nil {
849+ a = func (t * testing.T , v types.Variable ) {}
850+ }
851+
852+ return func (t * testing.T , v types.Variable ) {
853+ t .Helper ()
854+ (a )(t , v )
855+ f (t , v )
856+ }
857+ }
0 commit comments