@@ -10,6 +10,7 @@ import (
10
10
"github.com/hashicorp/hcl/v2/hclsyntax"
11
11
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
12
12
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
13
+ "github.com/zclconf/go-cty/cty"
13
14
)
14
15
15
16
func Test_GetResourceContent (t * testing.T ) {
@@ -536,7 +537,7 @@ func Test_DecodeRuleConfig_config_not_found(t *testing.T) {
536
537
}
537
538
}
538
539
539
- func Test_EvaluateExpr (t * testing.T ) {
540
+ func Test_EvaluateExpr_string (t * testing.T ) {
540
541
tests := []struct {
541
542
Name string
542
543
Src string
@@ -601,6 +602,64 @@ resource "aws_instance" "foo" {
601
602
}
602
603
}
603
604
605
+ func Test_EvaluateExpr_value (t * testing.T ) {
606
+ tests := []struct {
607
+ Name string
608
+ Src string
609
+ Want string
610
+ }{
611
+ {
612
+ Name : "sensitive variable" ,
613
+ Src : `
614
+ variable "instance_type" {
615
+ type = string
616
+ default = "secret"
617
+ sensitive = true
618
+ }
619
+
620
+ resource "aws_instance" "foo" {
621
+ instance_type = var.instance_type
622
+ }` ,
623
+ Want : `cty.StringVal("secret").Mark(marks.Sensitive)` ,
624
+ },
625
+ }
626
+
627
+ for _ , test := range tests {
628
+ t .Run (test .Name , func (t * testing.T ) {
629
+ runner := TestRunner (t , map [string ]string {"main.tf" : test .Src })
630
+
631
+ resources , err := runner .GetResourceContent ("aws_instance" , & hclext.BodySchema {
632
+ Attributes : []hclext.AttributeSchema {{Name : "instance_type" }},
633
+ }, nil )
634
+ if err != nil {
635
+ t .Fatal (err )
636
+ }
637
+
638
+ for _ , resource := range resources .Blocks {
639
+ // raw value
640
+ var instanceType cty.Value
641
+ if err := runner .EvaluateExpr (resource .Body .Attributes ["instance_type" ].Expr , & instanceType , nil ); err != nil {
642
+ t .Fatal (err )
643
+ }
644
+
645
+ if instanceType .GoString () != test .Want {
646
+ t .Fatalf (`"%s" is expected, but got "%s"` , test .Want , instanceType .GoString ())
647
+ }
648
+
649
+ // callback
650
+ if err := runner .EvaluateExpr (resource .Body .Attributes ["instance_type" ].Expr , func (val cty.Value ) error {
651
+ if instanceType .GoString () != test .Want {
652
+ t .Fatalf (`"%s" is expected, but got "%s"` , test .Want , instanceType .GoString ())
653
+ }
654
+ return nil
655
+ }, nil ); err != nil {
656
+ t .Fatal (err )
657
+ }
658
+ }
659
+ })
660
+ }
661
+ }
662
+
604
663
type dummyRule struct {
605
664
tflint.DefaultRule
606
665
}
0 commit comments