github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/rules/terraformrules/terraform_documented_outputs_test.go (about)

     1  package terraformrules
     2  
     3  import (
     4  	"testing"
     5  
     6  	hcl "github.com/hashicorp/hcl/v2"
     7  	"github.com/terraform-linters/tflint/tflint"
     8  )
     9  
    10  func Test_TerraformDocumentedOutputsRule(t *testing.T) {
    11  	cases := []struct {
    12  		Name     string
    13  		Content  string
    14  		Expected tflint.Issues
    15  	}{
    16  		{
    17  			Name: "no description",
    18  			Content: `
    19  output "endpoint" {
    20    value = aws_alb.main.dns_name
    21  }`,
    22  			Expected: tflint.Issues{
    23  				{
    24  					Rule:    NewTerraformDocumentedOutputsRule(),
    25  					Message: "`endpoint` output has no description",
    26  					Range: hcl.Range{
    27  						Filename: "outputs.tf",
    28  						Start:    hcl.Pos{Line: 2, Column: 1},
    29  						End:      hcl.Pos{Line: 2, Column: 18},
    30  					},
    31  				},
    32  			},
    33  		},
    34  		{
    35  			Name: "empty description",
    36  			Content: `
    37  output "endpoint" {
    38    value = aws_alb.main.dns_name
    39    description = ""
    40  }`,
    41  			Expected: tflint.Issues{
    42  				{
    43  					Rule:    NewTerraformDocumentedOutputsRule(),
    44  					Message: "`endpoint` output has no description",
    45  					Range: hcl.Range{
    46  						Filename: "outputs.tf",
    47  						Start:    hcl.Pos{Line: 2, Column: 1},
    48  						End:      hcl.Pos{Line: 2, Column: 18},
    49  					},
    50  				},
    51  			},
    52  		},
    53  		{
    54  			Name: "with description",
    55  			Content: `
    56  output "endpoint" {
    57    value = aws_alb.main.dns_name
    58    description = "DNS Endpoint"
    59  }`,
    60  			Expected: tflint.Issues{},
    61  		},
    62  	}
    63  
    64  	rule := NewTerraformDocumentedOutputsRule()
    65  
    66  	for _, tc := range cases {
    67  		runner := tflint.TestRunner(t, map[string]string{"outputs.tf": tc.Content})
    68  
    69  		if err := rule.Check(runner); err != nil {
    70  			t.Fatalf("Unexpected error occurred: %s", err)
    71  		}
    72  
    73  		tflint.AssertIssues(t, tc.Expected, runner.Issues)
    74  	}
    75  }