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

     1  package terraformrules
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/hcl/v2"
     8  	"github.com/terraform-linters/tflint/tflint"
     9  )
    10  
    11  func Test_TerraformStandardModuleStructureRule(t *testing.T) {
    12  	cases := []struct {
    13  		Name     string
    14  		Content  map[string]string
    15  		Expected tflint.Issues
    16  	}{
    17  		{
    18  			Name:    "empty module",
    19  			Content: map[string]string{},
    20  			Expected: tflint.Issues{
    21  				{
    22  					Rule:    NewTerraformStandardModuleStructureRule(),
    23  					Message: "Module should include a main.tf file as the primary entrypoint",
    24  					Range: hcl.Range{
    25  						Filename: "main.tf",
    26  						Start:    hcl.InitialPos,
    27  					},
    28  				},
    29  				{
    30  					Rule:    NewTerraformStandardModuleStructureRule(),
    31  					Message: "Module should include an empty variables.tf file",
    32  					Range: hcl.Range{
    33  						Filename: "variables.tf",
    34  						Start:    hcl.InitialPos,
    35  					},
    36  				},
    37  				{
    38  					Rule:    NewTerraformStandardModuleStructureRule(),
    39  					Message: "Module should include an empty outputs.tf file",
    40  					Range: hcl.Range{
    41  						Filename: "outputs.tf",
    42  						Start:    hcl.InitialPos,
    43  					},
    44  				},
    45  			},
    46  		},
    47  		{
    48  			Name: "directory in path",
    49  			Content: map[string]string{
    50  				"foo/main.tf": "",
    51  				"foo/variables.tf": `
    52  variable "v" {}				
    53  				`,
    54  			},
    55  			Expected: tflint.Issues{
    56  				{
    57  					Rule:    NewTerraformStandardModuleStructureRule(),
    58  					Message: "Module should include an empty outputs.tf file",
    59  					Range: hcl.Range{
    60  						Filename: filepath.Join("foo", "outputs.tf"),
    61  						Start:    hcl.InitialPos,
    62  					},
    63  				},
    64  			},
    65  		},
    66  		{
    67  			Name: "move variable",
    68  			Content: map[string]string{
    69  				"main.tf": `
    70  variable "v" {}
    71  `,
    72  				"variables.tf": "",
    73  				"outputs.tf":   "",
    74  			},
    75  			Expected: tflint.Issues{
    76  				{
    77  					Rule:    NewTerraformStandardModuleStructureRule(),
    78  					Message: `variable "v" should be moved from main.tf to variables.tf`,
    79  					Range: hcl.Range{
    80  						Filename: "main.tf",
    81  						Start: hcl.Pos{
    82  							Line:   2,
    83  							Column: 1,
    84  						},
    85  						End: hcl.Pos{
    86  							Line:   2,
    87  							Column: 13,
    88  						},
    89  					},
    90  				},
    91  			},
    92  		},
    93  		{
    94  			Name: "move output",
    95  			Content: map[string]string{
    96  				"main.tf": `
    97  output "o" { value = null }
    98  `,
    99  				"variables.tf": "",
   100  				"outputs.tf":   "",
   101  			},
   102  			Expected: tflint.Issues{
   103  				{
   104  					Rule:    NewTerraformStandardModuleStructureRule(),
   105  					Message: `output "o" should be moved from main.tf to outputs.tf`,
   106  					Range: hcl.Range{
   107  						Filename: "main.tf",
   108  						Start: hcl.Pos{
   109  							Line:   2,
   110  							Column: 1,
   111  						},
   112  						End: hcl.Pos{
   113  							Line:   2,
   114  							Column: 11,
   115  						},
   116  					},
   117  				},
   118  			},
   119  		},
   120  		{
   121  			Name: "json only",
   122  			Content: map[string]string{
   123  				"main.tf.json": "{}",
   124  			},
   125  			Expected: tflint.Issues{},
   126  		},
   127  		{
   128  			Name: "json variable",
   129  			Content: map[string]string{
   130  				"main.tf.json": `{"variable": {"v": {}}}`,
   131  			},
   132  			Expected: tflint.Issues{},
   133  		},
   134  		{
   135  			Name: "json output",
   136  			Content: map[string]string{
   137  				"main.tf.json": `{"output": {"o": {"value": null}}}`,
   138  			},
   139  			Expected: tflint.Issues{},
   140  		},
   141  	}
   142  
   143  	rule := NewTerraformStandardModuleStructureRule()
   144  
   145  	for _, tc := range cases {
   146  		tc := tc
   147  		t.Run(tc.Name, func(t *testing.T) {
   148  			runner := tflint.TestRunnerWithConfig(t, tc.Content, &tflint.Config{
   149  				Module: true,
   150  			})
   151  
   152  			if err := rule.Check(runner); err != nil {
   153  				t.Fatalf("Unexpected error occurred: %s", err)
   154  			}
   155  
   156  			tflint.AssertIssues(t, tc.Expected, runner.Issues)
   157  		})
   158  	}
   159  }