@@ -18,6 +18,7 @@ import (
1818 "fmt"
1919 "log"
2020 "net/url"
21+ "os"
2122 "path/filepath"
2223 "regexp"
2324 "slices"
@@ -201,6 +202,22 @@ func (e *Examples) Validate(rName string) {
201202 e .ValidateExternalProviders ()
202203}
203204
205+ func validateRegexForContents (r * regexp.Regexp , contents string , configPath string , objName string , vars map [string ]string ) {
206+ matches := r .FindAllStringSubmatch (contents , - 1 )
207+ for _ , v := range matches {
208+ found := false
209+ for k , _ := range vars {
210+ if k == v [1 ] {
211+ found = true
212+ break
213+ }
214+ }
215+ if ! found {
216+ log .Fatalf ("Failed to find %s environment variable defined in YAML file when validating the file %s. Please define this in %s" , v [1 ], configPath , objName )
217+ }
218+ }
219+ }
220+
204221func (e * Examples ) ValidateExternalProviders () {
205222 // Official providers supported by HashiCorp
206223 // https://registry.terraform.io/search/providers?namespace=hashicorp&tier=official
@@ -249,7 +266,7 @@ func (e *Examples) SetHCLText() {
249266 docTestEnvVars [key ] = docs_defaults [e.TestEnvVars [key ]]
250267 }
251268 e .TestEnvVars = docTestEnvVars
252- e .DocumentationHCLText = ExecuteTemplate ( e , e . ConfigPath , true )
269+ e .DocumentationHCLText = e . ExecuteTemplate ( )
253270 e .DocumentationHCLText = regexp .MustCompile (`\n\n$` ).ReplaceAllString (e .DocumentationHCLText , "\n " )
254271
255272 // Remove region tags
@@ -290,7 +307,7 @@ func (e *Examples) SetHCLText() {
290307
291308 e .Vars = testVars
292309 e .TestEnvVars = testTestEnvVars
293- e .TestHCLText = ExecuteTemplate ( e , e . ConfigPath , true )
310+ e .TestHCLText = e . ExecuteTemplate ( )
294311 e .TestHCLText = regexp .MustCompile (`\n\n$` ).ReplaceAllString (e .TestHCLText , "\n " )
295312 // Remove region tags
296313 e .TestHCLText = re1 .ReplaceAllString (e .TestHCLText , "" )
@@ -302,20 +319,23 @@ func (e *Examples) SetHCLText() {
302319 e .TestEnvVars = originalTestEnvVars
303320}
304321
305- func ExecuteTemplate (e any , templatePath string , appendNewline bool ) string {
306- templates := []string {
307- templatePath ,
308- "templates/terraform/expand_resource_ref.tmpl" ,
309- "templates/terraform/custom_flatten/bigquery_table_ref.go.tmpl" ,
310- "templates/terraform/flatten_property_method.go.tmpl" ,
311- "templates/terraform/expand_property_method.go.tmpl" ,
312- "templates/terraform/update_mask.go.tmpl" ,
313- "templates/terraform/nested_query.go.tmpl" ,
314- "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 )
315326 }
316- templateFileName := filepath .Base (templatePath )
317327
318- 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 )
319339 if err != nil {
320340 glog .Exit (err )
321341 }
@@ -327,7 +347,7 @@ func ExecuteTemplate(e any, templatePath string, appendNewline bool) string {
327347
328348 rs := contents .String ()
329349
330- if ! strings .HasSuffix (rs , "\n " ) && appendNewline {
350+ if ! strings .HasSuffix (rs , "\n " ) {
331351 rs = fmt .Sprintf ("%s\n " , rs )
332352 }
333353
@@ -401,7 +421,7 @@ func (e *Examples) SetOiCSHCLText() {
401421 }
402422
403423 e .Vars = testVars
404- e .OicsHCLText = ExecuteTemplate ( e , e . ConfigPath , true )
424+ e .OicsHCLText = e . ExecuteTemplate ( )
405425 e .OicsHCLText = regexp .MustCompile (`\n\n$` ).ReplaceAllString (e .OicsHCLText , "\n " )
406426
407427 // Remove region tags
0 commit comments