@@ -14,24 +14,27 @@ import (
1414 "github.com/coder/terraform-provider-coder/v2/provider"
1515)
1616
17+ // formTypeTestCase is the config for a single test case.
1718type formTypeTestCase struct {
1819 name string
1920 config formTypeCheck
2021 assert paramAssert
2122 expectError * regexp.Regexp
2223}
2324
25+ // paramAssert is asserted on the provider's parsed terraform state.
2426type paramAssert struct {
2527 FormType provider.ParameterFormType
2628 Type provider.OptionType
2729 Styling string
2830}
2931
32+ // formTypeCheck is a struct that helps build the terraform config
3033type formTypeCheck struct {
3134 formType provider.ParameterFormType
3235 optionType provider.OptionType
33- defValue string
3436 options bool
37+ defValue string
3538 customOptions []string
3639}
3740
@@ -283,18 +286,8 @@ func TestValidateFormType(t *testing.T) {
283286 })
284287}
285288
289+ // ezconfig converts a formTypeCheck into a terraform config string.
286290func ezconfig (paramName string , cfg formTypeCheck ) (defaultValue string , tf string ) {
287- var body strings.Builder
288- if cfg .defValue != "" {
289- body .WriteString (fmt .Sprintf ("default = %q\n " , cfg .defValue ))
290- }
291- if cfg .formType != "" {
292- body .WriteString (fmt .Sprintf ("form_type = %q\n " , cfg .formType ))
293- }
294- if cfg .optionType != "" {
295- body .WriteString (fmt .Sprintf ("type = %q\n " , cfg .optionType ))
296- }
297-
298291 options := cfg .customOptions
299292 if cfg .options && len (cfg .customOptions ) == 0 {
300293 switch cfg .optionType {
@@ -309,22 +302,34 @@ func ezconfig(paramName string, cfg formTypeCheck) (defaultValue string, tf stri
309302 defaultValue = "1"
310303 case provider .OptionTypeListString :
311304 options = []string {`["red", "blue"]` }
312- defaultValue = `["red"]`
305+ defaultValue = `["red", "blue" ]`
313306 default :
314307 panic (fmt .Sprintf ("unknown option type %q when generating options" , cfg .optionType ))
315308 }
316309 }
317310
311+ if cfg .defValue == "" {
312+ cfg .defValue = defaultValue
313+ }
314+
315+ var body strings.Builder
316+ if cfg .defValue != "" {
317+ body .WriteString (fmt .Sprintf ("default = %q\n " , cfg .defValue ))
318+ }
319+ if cfg .formType != "" {
320+ body .WriteString (fmt .Sprintf ("form_type = %q\n " , cfg .formType ))
321+ }
322+ if cfg .optionType != "" {
323+ body .WriteString (fmt .Sprintf ("type = %q\n " , cfg .optionType ))
324+ }
325+
318326 for i , opt := range options {
319327 body .WriteString ("option {\n " )
320328 body .WriteString (fmt .Sprintf ("name = \" val_%d\" \n " , i ))
321329 body .WriteString (fmt .Sprintf ("value = %q\n " , opt ))
322330 body .WriteString ("}\n " )
323331 }
324332
325- if cfg .defValue == "" {
326- cfg .defValue = defaultValue
327- }
328333 return cfg .defValue , fmt .Sprintf (`
329334 provider "coder" {
330335 }
@@ -353,13 +358,6 @@ func formTypeTest(t *testing.T, c formTypeTestCase) {
353358 assert .Equal (t , string (c .assert .Type ), param .Primary .Attributes ["type" ], "type" )
354359 assert .JSONEq (t , c .assert .Styling , param .Primary .Attributes ["styling" ], "styling" )
355360
356- //ft := provider.ParameterFormType(param.Primary.Attributes["form_type"])
357- //ot := provider.OptionType(param.Primary.Attributes["type"])
358-
359- // Option blocks are not stored in a very friendly format
360- // here.
361- //options := param.Primary.Attributes["option.0.name"] != ""
362-
363361 return nil
364362 }
365363 if c .expectError != nil {
0 commit comments