github.com/tetrafolium/tflint@v0.8.0/cmd/option_test.go (about) 1 package cmd 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 flags "github.com/jessevdk/go-flags" 9 "github.com/wata727/tflint/client" 10 "github.com/wata727/tflint/tflint" 11 ) 12 13 func Test_toConfig(t *testing.T) { 14 cases := []struct { 15 Name string 16 Command string 17 Expected *tflint.Config 18 }{ 19 { 20 Name: "default", 21 Command: "./tflint", 22 Expected: tflint.EmptyConfig(), 23 }, 24 { 25 Name: "--deep", 26 Command: "./tflint --deep", 27 Expected: &tflint.Config{ 28 DeepCheck: true, 29 AwsCredentials: client.AwsCredentials{}, 30 IgnoreModule: map[string]bool{}, 31 IgnoreRule: map[string]bool{}, 32 Varfile: []string{}, 33 TerraformVersion: "", 34 Rules: map[string]*tflint.RuleConfig{}, 35 }, 36 }, 37 { 38 Name: "AWS static credentials", 39 Command: "./tflint --aws-access-key AWS_ACCESS_KEY_ID --aws-secret-key AWS_SECRET_ACCESS_KEY --aws-region us-east-1", 40 Expected: &tflint.Config{ 41 DeepCheck: false, 42 AwsCredentials: client.AwsCredentials{ 43 AccessKey: "AWS_ACCESS_KEY_ID", 44 SecretKey: "AWS_SECRET_ACCESS_KEY", 45 Region: "us-east-1", 46 }, 47 IgnoreModule: map[string]bool{}, 48 IgnoreRule: map[string]bool{}, 49 Varfile: []string{}, 50 TerraformVersion: "", 51 Rules: map[string]*tflint.RuleConfig{}, 52 }, 53 }, 54 { 55 Name: "AWS shared credentials", 56 Command: "./tflint --aws-profile production --aws-region us-east-1", 57 Expected: &tflint.Config{ 58 DeepCheck: false, 59 AwsCredentials: client.AwsCredentials{ 60 Profile: "production", 61 Region: "us-east-1", 62 }, 63 IgnoreModule: map[string]bool{}, 64 IgnoreRule: map[string]bool{}, 65 Varfile: []string{}, 66 TerraformVersion: "", 67 Rules: map[string]*tflint.RuleConfig{}, 68 }, 69 }, 70 { 71 Name: "--ignore-module", 72 Command: "./tflint --ignore-module module1,module2", 73 Expected: &tflint.Config{ 74 DeepCheck: false, 75 AwsCredentials: client.AwsCredentials{}, 76 IgnoreModule: map[string]bool{"module1": true, "module2": true}, 77 IgnoreRule: map[string]bool{}, 78 Varfile: []string{}, 79 TerraformVersion: "", 80 Rules: map[string]*tflint.RuleConfig{}, 81 }, 82 }, 83 { 84 Name: "--ignore-rule", 85 Command: "./tflint --ignore-rule rule1,rule2", 86 Expected: &tflint.Config{ 87 DeepCheck: false, 88 AwsCredentials: client.AwsCredentials{}, 89 IgnoreModule: map[string]bool{}, 90 IgnoreRule: map[string]bool{"rule1": true, "rule2": true}, 91 Varfile: []string{}, 92 TerraformVersion: "", 93 Rules: map[string]*tflint.RuleConfig{}, 94 }, 95 }, 96 { 97 Name: "--var-file", 98 Command: "./tflint --var-file example1.tfvars,example2.tfvars", 99 Expected: &tflint.Config{ 100 DeepCheck: false, 101 AwsCredentials: client.AwsCredentials{}, 102 IgnoreModule: map[string]bool{}, 103 IgnoreRule: map[string]bool{}, 104 Varfile: []string{"example1.tfvars", "example2.tfvars"}, 105 TerraformVersion: "", 106 Rules: map[string]*tflint.RuleConfig{}, 107 }, 108 }, 109 { 110 Name: "--fast", 111 Command: "./tflint --fast", 112 Expected: &tflint.Config{ 113 DeepCheck: false, 114 AwsCredentials: client.AwsCredentials{}, 115 IgnoreModule: map[string]bool{}, 116 IgnoreRule: map[string]bool{"aws_instance_invalid_ami": true}, 117 Varfile: []string{}, 118 TerraformVersion: "", 119 Rules: map[string]*tflint.RuleConfig{}, 120 }, 121 }, 122 } 123 124 for _, tc := range cases { 125 var opts Options 126 parser := flags.NewParser(&opts, flags.HelpFlag) 127 128 _, err := parser.ParseArgs(strings.Split(tc.Command, " ")) 129 if err != nil { 130 t.Fatalf("Failed `%s` test: %s", tc.Name, err) 131 } 132 133 ret := opts.toConfig() 134 if !cmp.Equal(tc.Expected, ret) { 135 t.Fatalf("Failed `%s` test: diff=%s", tc.Name, cmp.Diff(tc.Expected, ret)) 136 } 137 } 138 }