@@ -199,25 +199,9 @@ func (e *Examples) Validate(rName string) {
199199 if e .Name == "" {
200200 log .Fatalf ("Missing `name` for one example in resource %s" , rName )
201201 }
202- e .ValidateVariables ()
203202 e .ValidateExternalProviders ()
204203}
205204
206- func (e * Examples ) ValidateVariables () {
207- // Read file as raw text to pull variables
208- content , err := os .ReadFile (e .ConfigPath )
209- if err != nil {
210- glog .Exit (err )
211- }
212-
213- fileContentString := string (content )
214-
215- envVarRegex := regexp .MustCompile (`{{index \$\.TestEnvVars "([a-zA-Z_]*)"}}` )
216- validateRegexForContents (envVarRegex , fileContentString , e .ConfigPath , "test_env_vars" , e .TestEnvVars )
217- varRegex := regexp .MustCompile (`{{index \$\.Vars "([a-zA-Z_]*)"}}` )
218- validateRegexForContents (varRegex , fileContentString , e .ConfigPath , "vars" , e .Vars )
219- }
220-
221205func validateRegexForContents (r * regexp.Regexp , contents string , configPath string , objName string , vars map [string ]string ) {
222206 matches := r .FindAllStringSubmatch (contents , - 1 )
223207 for _ , v := range matches {
@@ -282,7 +266,7 @@ func (e *Examples) SetHCLText() {
282266 docTestEnvVars [key ] = docs_defaults [e.TestEnvVars [key ]]
283267 }
284268 e .TestEnvVars = docTestEnvVars
285- e .DocumentationHCLText = ExecuteTemplate ( e , e . ConfigPath , true )
269+ e .DocumentationHCLText = e . ExecuteTemplate ( )
286270 e .DocumentationHCLText = regexp .MustCompile (`\n\n$` ).ReplaceAllString (e .DocumentationHCLText , "\n " )
287271
288272 // Remove region tags
@@ -323,7 +307,7 @@ func (e *Examples) SetHCLText() {
323307
324308 e .Vars = testVars
325309 e .TestEnvVars = testTestEnvVars
326- e .TestHCLText = ExecuteTemplate ( e , e . ConfigPath , true )
310+ e .TestHCLText = e . ExecuteTemplate ( )
327311 e .TestHCLText = regexp .MustCompile (`\n\n$` ).ReplaceAllString (e .TestHCLText , "\n " )
328312 // Remove region tags
329313 e .TestHCLText = re1 .ReplaceAllString (e .TestHCLText , "" )
@@ -335,20 +319,23 @@ func (e *Examples) SetHCLText() {
335319 e .TestEnvVars = originalTestEnvVars
336320}
337321
338- func ExecuteTemplate (e any , templatePath string , appendNewline bool ) string {
339- templates := []string {
340- templatePath ,
341- "templates/terraform/expand_resource_ref.tmpl" ,
342- "templates/terraform/custom_flatten/bigquery_table_ref.go.tmpl" ,
343- "templates/terraform/flatten_property_method.go.tmpl" ,
344- "templates/terraform/expand_property_method.go.tmpl" ,
345- "templates/terraform/update_mask.go.tmpl" ,
346- "templates/terraform/nested_query.go.tmpl" ,
347- "templates/terraform/unordered_list_customize_diff.go.tmpl" ,
322+ func (e * Examples ) ExecuteTemplate () string {
323+ templateContent , err := os .ReadFile (e .ConfigPath )
324+ if err != nil {
325+ glog .Exit (err )
348326 }
349- templateFileName := filepath .Base (templatePath )
350327
351- tmpl , err := template .New (templateFileName ).Funcs (google .TemplateFunctions ).ParseFiles (templates ... )
328+ fileContentString := string (templateContent )
329+
330+ // Check that any variables in Vars or TestEnvVars used in the example are defined via YAML
331+ envVarRegex := regexp .MustCompile (`{{index \$\.TestEnvVars "([a-zA-Z_]*)"}}` )
332+ validateRegexForContents (envVarRegex , fileContentString , e .ConfigPath , "test_env_vars" , e .TestEnvVars )
333+ varRegex := regexp .MustCompile (`{{index \$\.Vars "([a-zA-Z_]*)"}}` )
334+ validateRegexForContents (varRegex , fileContentString , e .ConfigPath , "vars" , e .Vars )
335+
336+ templateFileName := filepath .Base (e .ConfigPath )
337+
338+ tmpl , err := template .New (templateFileName ).Funcs (google .TemplateFunctions ).Parse (fileContentString )
352339 if err != nil {
353340 glog .Exit (err )
354341 }
@@ -360,7 +347,7 @@ func ExecuteTemplate(e any, templatePath string, appendNewline bool) string {
360347
361348 rs := contents .String ()
362349
363- if ! strings .HasSuffix (rs , "\n " ) && appendNewline {
350+ if ! strings .HasSuffix (rs , "\n " ) {
364351 rs = fmt .Sprintf ("%s\n " , rs )
365352 }
366353
@@ -434,7 +421,7 @@ func (e *Examples) SetOiCSHCLText() {
434421 }
435422
436423 e .Vars = testVars
437- e .OicsHCLText = ExecuteTemplate ( e , e . ConfigPath , true )
424+ e .OicsHCLText = e . ExecuteTemplate ( )
438425 e .OicsHCLText = regexp .MustCompile (`\n\n$` ).ReplaceAllString (e .OicsHCLText , "\n " )
439426
440427 // Remove region tags
0 commit comments