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

     1  package terraformrules
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  	"github.com/terraform-linters/tflint/tflint"
     8  )
     9  
    10  func Test_TerraformCommentSyntaxRule(t *testing.T) {
    11  	cases := []struct {
    12  		Name     string
    13  		Content  string
    14  		JSON     bool
    15  		Expected tflint.Issues
    16  	}{
    17  		{
    18  			Name:     "hash comment",
    19  			Content:  `# foo`,
    20  			Expected: tflint.Issues{},
    21  		},
    22  		{
    23  			Name: "multi-line comment",
    24  			Content: `
    25  /*
    26  	This comment spans multiple lines
    27  */			
    28  `,
    29  			Expected: tflint.Issues{},
    30  		},
    31  		{
    32  			Name:    "double-slash comment",
    33  			Content: `// foo`,
    34  			Expected: tflint.Issues{
    35  				{
    36  					Rule:    NewTerraformCommentSyntaxRule(),
    37  					Message: "Single line comments should begin with #",
    38  					Range: hcl.Range{
    39  						Filename: "variables.tf",
    40  						Start: hcl.Pos{
    41  							Line:   1,
    42  							Column: 1,
    43  						},
    44  						End: hcl.Pos{
    45  							Line:   1,
    46  							Column: 7,
    47  						},
    48  					},
    49  				},
    50  			},
    51  		},
    52  		{
    53  			Name: "end-of-line hash comment",
    54  			Content: `
    55  variable "foo" {
    56  	type = string # a string
    57  }
    58  `,
    59  			Expected: tflint.Issues{},
    60  		},
    61  		{
    62  			Name:     "JSON",
    63  			Content:  `{"variable": {"foo": {"type": "string"}}}`,
    64  			JSON:     true,
    65  			Expected: tflint.Issues{},
    66  		},
    67  	}
    68  
    69  	rule := NewTerraformCommentSyntaxRule()
    70  
    71  	for _, tc := range cases {
    72  		filename := "variables.tf"
    73  		if tc.JSON {
    74  			filename += ".json"
    75  		}
    76  
    77  		runner := tflint.TestRunner(t, map[string]string{filename: tc.Content})
    78  
    79  		if err := rule.Check(runner); err != nil {
    80  			t.Fatalf("Unexpected error occurred: %s", err)
    81  		}
    82  
    83  		tflint.AssertIssues(t, tc.Expected, runner.Issues)
    84  	}
    85  }