github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/cmd/option_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	hcl "github.com/hashicorp/hcl/v2"
     9  	flags "github.com/jessevdk/go-flags"
    10  	"github.com/terraform-linters/tflint/client"
    11  	"github.com/terraform-linters/tflint/tflint"
    12  )
    13  
    14  func Test_toConfig(t *testing.T) {
    15  	cases := []struct {
    16  		Name     string
    17  		Command  string
    18  		Expected *tflint.Config
    19  	}{
    20  		{
    21  			Name:     "default",
    22  			Command:  "./tflint",
    23  			Expected: tflint.EmptyConfig(),
    24  		},
    25  		{
    26  			Name:    "--module",
    27  			Command: "./tflint --module",
    28  			Expected: &tflint.Config{
    29  				Module:            true,
    30  				DeepCheck:         false,
    31  				Force:             false,
    32  				AwsCredentials:    client.AwsCredentials{},
    33  				IgnoreModules:     map[string]bool{},
    34  				Varfiles:          []string{},
    35  				Variables:         []string{},
    36  				DisabledByDefault: false,
    37  				Rules:             map[string]*tflint.RuleConfig{},
    38  				Plugins:           map[string]*tflint.PluginConfig{},
    39  			},
    40  		},
    41  		{
    42  			Name:    "--deep",
    43  			Command: "./tflint --deep",
    44  			Expected: &tflint.Config{
    45  				Module:            false,
    46  				DeepCheck:         true,
    47  				Force:             false,
    48  				AwsCredentials:    client.AwsCredentials{},
    49  				IgnoreModules:     map[string]bool{},
    50  				Varfiles:          []string{},
    51  				Variables:         []string{},
    52  				DisabledByDefault: false,
    53  				Rules:             map[string]*tflint.RuleConfig{},
    54  				Plugins:           map[string]*tflint.PluginConfig{},
    55  			},
    56  		},
    57  		{
    58  			Name:    "--force",
    59  			Command: "./tflint --force",
    60  			Expected: &tflint.Config{
    61  				Module:            false,
    62  				DeepCheck:         false,
    63  				Force:             true,
    64  				AwsCredentials:    client.AwsCredentials{},
    65  				IgnoreModules:     map[string]bool{},
    66  				Varfiles:          []string{},
    67  				Variables:         []string{},
    68  				DisabledByDefault: false,
    69  				Rules:             map[string]*tflint.RuleConfig{},
    70  				Plugins:           map[string]*tflint.PluginConfig{},
    71  			},
    72  		},
    73  		{
    74  			Name:    "AWS static credentials",
    75  			Command: "./tflint --aws-access-key AWS_ACCESS_KEY_ID --aws-secret-key AWS_SECRET_ACCESS_KEY --aws-region us-east-1",
    76  			Expected: &tflint.Config{
    77  				Module:    false,
    78  				DeepCheck: false,
    79  				Force:     false,
    80  				AwsCredentials: client.AwsCredentials{
    81  					AccessKey: "AWS_ACCESS_KEY_ID",
    82  					SecretKey: "AWS_SECRET_ACCESS_KEY",
    83  					Region:    "us-east-1",
    84  				},
    85  				IgnoreModules:     map[string]bool{},
    86  				Varfiles:          []string{},
    87  				Variables:         []string{},
    88  				DisabledByDefault: false,
    89  				Rules:             map[string]*tflint.RuleConfig{},
    90  				Plugins:           map[string]*tflint.PluginConfig{},
    91  			},
    92  		},
    93  		{
    94  			Name:    "AWS shared credentials",
    95  			Command: "./tflint --aws-profile production --aws-region us-east-1",
    96  			Expected: &tflint.Config{
    97  				Module:    false,
    98  				DeepCheck: false,
    99  				Force:     false,
   100  				AwsCredentials: client.AwsCredentials{
   101  					Profile: "production",
   102  					Region:  "us-east-1",
   103  				},
   104  				IgnoreModules:     map[string]bool{},
   105  				Varfiles:          []string{},
   106  				Variables:         []string{},
   107  				DisabledByDefault: false,
   108  				Rules:             map[string]*tflint.RuleConfig{},
   109  				Plugins:           map[string]*tflint.PluginConfig{},
   110  			},
   111  		},
   112  		{
   113  			Name:    "AWS shared credentials in another file",
   114  			Command: "./tflint --aws-creds-file ~/.aws/myapp --aws-profile production --aws-region us-east-1",
   115  			Expected: &tflint.Config{
   116  				Module:    false,
   117  				DeepCheck: false,
   118  				Force:     false,
   119  				AwsCredentials: client.AwsCredentials{
   120  					CredsFile: "~/.aws/myapp",
   121  					Profile:   "production",
   122  					Region:    "us-east-1",
   123  				},
   124  				IgnoreModules:     map[string]bool{},
   125  				Varfiles:          []string{},
   126  				Variables:         []string{},
   127  				DisabledByDefault: false,
   128  				Rules:             map[string]*tflint.RuleConfig{},
   129  				Plugins:           map[string]*tflint.PluginConfig{},
   130  			},
   131  		},
   132  		{
   133  			Name:    "--ignore-module",
   134  			Command: "./tflint --ignore-module module1,module2",
   135  			Expected: &tflint.Config{
   136  				Module:            false,
   137  				DeepCheck:         false,
   138  				Force:             false,
   139  				AwsCredentials:    client.AwsCredentials{},
   140  				IgnoreModules:     map[string]bool{"module1": true, "module2": true},
   141  				Varfiles:          []string{},
   142  				Variables:         []string{},
   143  				DisabledByDefault: false,
   144  				Rules:             map[string]*tflint.RuleConfig{},
   145  				Plugins:           map[string]*tflint.PluginConfig{},
   146  			},
   147  		},
   148  		{
   149  			Name:    "multiple `--ignore-module`",
   150  			Command: "./tflint --ignore-module module1 --ignore-module module2",
   151  			Expected: &tflint.Config{
   152  				Module:            false,
   153  				DeepCheck:         false,
   154  				Force:             false,
   155  				AwsCredentials:    client.AwsCredentials{},
   156  				IgnoreModules:     map[string]bool{"module1": true, "module2": true},
   157  				Varfiles:          []string{},
   158  				Variables:         []string{},
   159  				DisabledByDefault: false,
   160  				Rules:             map[string]*tflint.RuleConfig{},
   161  				Plugins:           map[string]*tflint.PluginConfig{},
   162  			},
   163  		},
   164  		{
   165  			Name:    "--var-file",
   166  			Command: "./tflint --var-file example1.tfvars,example2.tfvars",
   167  			Expected: &tflint.Config{
   168  				Module:            false,
   169  				DeepCheck:         false,
   170  				Force:             false,
   171  				AwsCredentials:    client.AwsCredentials{},
   172  				IgnoreModules:     map[string]bool{},
   173  				Varfiles:          []string{"example1.tfvars", "example2.tfvars"},
   174  				Variables:         []string{},
   175  				DisabledByDefault: false,
   176  				Rules:             map[string]*tflint.RuleConfig{},
   177  				Plugins:           map[string]*tflint.PluginConfig{},
   178  			},
   179  		},
   180  		{
   181  			Name:    "multiple `--var-file`",
   182  			Command: "./tflint --var-file example1.tfvars --var-file example2.tfvars",
   183  			Expected: &tflint.Config{
   184  				Module:            false,
   185  				DeepCheck:         false,
   186  				Force:             false,
   187  				AwsCredentials:    client.AwsCredentials{},
   188  				IgnoreModules:     map[string]bool{},
   189  				Varfiles:          []string{"example1.tfvars", "example2.tfvars"},
   190  				Variables:         []string{},
   191  				DisabledByDefault: false,
   192  				Rules:             map[string]*tflint.RuleConfig{},
   193  				Plugins:           map[string]*tflint.PluginConfig{},
   194  			},
   195  		},
   196  		{
   197  			Name:    "--var",
   198  			Command: "./tflint --var foo=bar --var bar=baz",
   199  			Expected: &tflint.Config{
   200  				Module:            false,
   201  				DeepCheck:         false,
   202  				Force:             false,
   203  				AwsCredentials:    client.AwsCredentials{},
   204  				IgnoreModules:     map[string]bool{},
   205  				Varfiles:          []string{},
   206  				Variables:         []string{"foo=bar", "bar=baz"},
   207  				DisabledByDefault: false,
   208  				Rules:             map[string]*tflint.RuleConfig{},
   209  				Plugins:           map[string]*tflint.PluginConfig{},
   210  			},
   211  		},
   212  		{
   213  			Name:    "--enable-rule",
   214  			Command: "./tflint --enable-rule aws_instance_invalid_type --enable-rule aws_instance_previous_type",
   215  			Expected: &tflint.Config{
   216  				Module:            false,
   217  				DeepCheck:         false,
   218  				Force:             false,
   219  				AwsCredentials:    client.AwsCredentials{},
   220  				IgnoreModules:     map[string]bool{},
   221  				Varfiles:          []string{},
   222  				Variables:         []string{},
   223  				DisabledByDefault: false,
   224  				Rules: map[string]*tflint.RuleConfig{
   225  					"aws_instance_invalid_type": {
   226  						Name:    "aws_instance_invalid_type",
   227  						Enabled: true,
   228  						Body:    hcl.EmptyBody(),
   229  					},
   230  					"aws_instance_previous_type": {
   231  						Name:    "aws_instance_previous_type",
   232  						Enabled: true,
   233  						Body:    hcl.EmptyBody(),
   234  					},
   235  				},
   236  				Plugins: map[string]*tflint.PluginConfig{},
   237  			},
   238  		},
   239  		{
   240  			Name:    "--disable-rule",
   241  			Command: "./tflint --disable-rule aws_instance_invalid_type --disable-rule aws_instance_previous_type",
   242  			Expected: &tflint.Config{
   243  				Module:            false,
   244  				DeepCheck:         false,
   245  				Force:             false,
   246  				AwsCredentials:    client.AwsCredentials{},
   247  				IgnoreModules:     map[string]bool{},
   248  				Varfiles:          []string{},
   249  				Variables:         []string{},
   250  				DisabledByDefault: false,
   251  				Rules: map[string]*tflint.RuleConfig{
   252  					"aws_instance_invalid_type": {
   253  						Name:    "aws_instance_invalid_type",
   254  						Enabled: false,
   255  						Body:    hcl.EmptyBody(),
   256  					},
   257  					"aws_instance_previous_type": {
   258  						Name:    "aws_instance_previous_type",
   259  						Enabled: false,
   260  						Body:    hcl.EmptyBody(),
   261  					},
   262  				},
   263  				Plugins: map[string]*tflint.PluginConfig{},
   264  			},
   265  		},
   266  		{
   267  			Name:    "--only",
   268  			Command: "./tflint --only aws_instance_invalid_type",
   269  			Expected: &tflint.Config{
   270  				Module:            false,
   271  				DeepCheck:         false,
   272  				Force:             false,
   273  				AwsCredentials:    client.AwsCredentials{},
   274  				IgnoreModules:     map[string]bool{},
   275  				Varfiles:          []string{},
   276  				Variables:         []string{},
   277  				DisabledByDefault: true,
   278  				Rules: map[string]*tflint.RuleConfig{
   279  					"aws_instance_invalid_type": {
   280  						Name:    "aws_instance_invalid_type",
   281  						Enabled: true,
   282  						Body:    hcl.EmptyBody(),
   283  					},
   284  				},
   285  				Plugins: map[string]*tflint.PluginConfig{},
   286  			},
   287  		},
   288  	}
   289  
   290  	for _, tc := range cases {
   291  		var opts Options
   292  		parser := flags.NewParser(&opts, flags.HelpFlag)
   293  
   294  		_, err := parser.ParseArgs(strings.Split(tc.Command, " "))
   295  		if err != nil {
   296  			t.Fatalf("Failed `%s` test: %s", tc.Name, err)
   297  		}
   298  
   299  		ret := opts.toConfig()
   300  		if !cmp.Equal(tc.Expected, ret) {
   301  			t.Fatalf("Failed `%s` test: diff=%s", tc.Name, cmp.Diff(tc.Expected, ret))
   302  		}
   303  	}
   304  }