github.com/mcuckson/tflint@v0.8.0/integration_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io/ioutil" 7 "log" 8 "os" 9 "runtime" 10 "sort" 11 "strings" 12 "testing" 13 14 "github.com/google/go-cmp/cmp" 15 "github.com/wata727/tflint/cmd" 16 "github.com/wata727/tflint/issue" 17 ) 18 19 func TestMain(m *testing.M) { 20 log.SetOutput(ioutil.Discard) 21 os.Exit(m.Run()) 22 } 23 24 func TestIntegration(t *testing.T) { 25 cases := []struct { 26 Name string 27 Command string 28 Dir string 29 }{ 30 { 31 Name: "basic", 32 Command: "./tflint --format json", 33 Dir: "basic", 34 }, 35 { 36 Name: "override", 37 Command: "./tflint --format json", 38 Dir: "override", 39 }, 40 { 41 Name: "variables", 42 Command: "./tflint --format json --var-file variables.tfvars", 43 Dir: "variables", 44 }, 45 { 46 Name: "module", 47 Command: "./tflint --format json", 48 Dir: "module", 49 }, 50 } 51 52 dir, _ := os.Getwd() 53 defer os.Chdir(dir) 54 55 for _, tc := range cases { 56 testDir := dir + "/integration/" + tc.Dir 57 os.Chdir(testDir) 58 59 outStream, errStream := new(bytes.Buffer), new(bytes.Buffer) 60 cli := cmd.NewCLI(outStream, errStream) 61 args := strings.Split(tc.Command, " ") 62 cli.Run(args) 63 64 var b []byte 65 var err error 66 if runtime.GOOS == "windows" && IsWindowsResultExist() { 67 b, err = ioutil.ReadFile("result_windows.json") 68 } else { 69 b, err = ioutil.ReadFile("result.json") 70 } 71 if err != nil { 72 t.Fatal(err) 73 } 74 75 var expectedIssues []*issue.Issue 76 if err := json.Unmarshal(b, &expectedIssues); err != nil { 77 t.Fatal(err) 78 } 79 sort.Sort(issue.ByFileLine{Issues: issue.Issues(expectedIssues)}) 80 81 var resultIssues []*issue.Issue 82 if err := json.Unmarshal(outStream.Bytes(), &resultIssues); err != nil { 83 t.Fatal(err) 84 } 85 sort.Sort(issue.ByFileLine{Issues: issue.Issues(resultIssues)}) 86 87 if !cmp.Equal(resultIssues, expectedIssues) { 88 t.Fatalf("Failed `%s` test: diff=%s", tc.Name, cmp.Diff(expectedIssues, resultIssues)) 89 } 90 } 91 } 92 93 func IsWindowsResultExist() bool { 94 _, err := os.Stat("result_windows.json") 95 return !os.IsNotExist(err) 96 }