@@ -321,6 +321,103 @@ resource "aws_instance" "foo" {
321
321
}
322
322
}
323
323
324
+ func TestGetProviderContent (t * testing.T ) {
325
+ // default error check helper
326
+ neverHappend := func (err error ) bool { return err != nil }
327
+
328
+ // default getFileImpl function
329
+ files := map [string ][]byte {}
330
+ fileExists := func () map [string ][]byte {
331
+ return files
332
+ }
333
+
334
+ // test util functions
335
+ hclFile := func (filename string , code string ) * hcl.File {
336
+ file , diags := hclsyntax .ParseConfig ([]byte (code ), filename , hcl .InitialPos )
337
+ if diags .HasErrors () {
338
+ panic (diags )
339
+ }
340
+ files [filename ] = file .Bytes
341
+ return file
342
+ }
343
+
344
+ tests := []struct {
345
+ Name string
346
+ Args func () (string , * hclext.BodySchema , * tflint.GetModuleContentOption )
347
+ ServerImpl func (* hclext.BodySchema , tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics )
348
+ Want func (string , * hclext.BodySchema , * tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics )
349
+ ErrCheck func (error ) bool
350
+ }{
351
+ {
352
+ Name : "get HCL content" ,
353
+ Args : func () (string , * hclext.BodySchema , * tflint.GetModuleContentOption ) {
354
+ return "aws" , & hclext.BodySchema {
355
+ Attributes : []hclext.AttributeSchema {{Name : "region" }},
356
+ }, nil
357
+ },
358
+ ServerImpl : func (schema * hclext.BodySchema , opts tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics ) {
359
+ file := hclFile ("test.tf" , `
360
+ provider "aws" {
361
+ region = "us-east-1"
362
+ }
363
+
364
+ provider "google" {
365
+ region = "us-central1"
366
+ }` )
367
+ return hclext .PartialContent (file .Body , schema )
368
+ },
369
+ Want : func (resource string , schema * hclext.BodySchema , opts * tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics ) {
370
+ // Removed "google" provider
371
+ file := hclFile ("test.tf" , `
372
+ provider "aws" {
373
+ region = "us-east-1"
374
+ }` )
375
+ return hclext .Content (file .Body , & hclext.BodySchema {
376
+ Blocks : []hclext.BlockSchema {
377
+ {
378
+ Type : "provider" ,
379
+ LabelNames : []string {"name" },
380
+ Body : & hclext.BodySchema {
381
+ Attributes : []hclext.AttributeSchema {{Name : "region" }},
382
+ },
383
+ },
384
+ },
385
+ })
386
+ },
387
+ ErrCheck : neverHappend ,
388
+ },
389
+ }
390
+
391
+ for _ , test := range tests {
392
+ t .Run (test .Name , func (t * testing.T ) {
393
+ client := startTestGRPCServer (t , newMockServer (mockServerImpl {getModuleContent : test .ServerImpl , getFiles : fileExists }))
394
+
395
+ got , err := client .GetProviderContent (test .Args ())
396
+ if test .ErrCheck (err ) {
397
+ t .Fatalf ("failed to call GetProviderContent: %s" , err )
398
+ }
399
+ want , diags := test .Want (test .Args ())
400
+ if diags .HasErrors () {
401
+ t .Fatalf ("failed to get want: %d diagsnotics" , len (diags ))
402
+ for _ , diag := range diags {
403
+ t .Logf (" - %s" , diag .Error ())
404
+ }
405
+ }
406
+
407
+ opts := cmp.Options {
408
+ cmp .Comparer (func (x , y cty.Value ) bool {
409
+ return x .GoString () == y .GoString ()
410
+ }),
411
+ cmpopts .EquateEmpty (),
412
+ allowAllUnexported ,
413
+ }
414
+ if diff := cmp .Diff (got , want , opts ); diff != "" {
415
+ t .Errorf ("diff: %s" , diff )
416
+ }
417
+ })
418
+ }
419
+ }
420
+
324
421
func TestGetModuleContent (t * testing.T ) {
325
422
// default error check helper
326
423
neverHappend := func (err error ) bool { return err != nil }
0 commit comments