Skip to content

Commit

Permalink
Remove hard-coded versions from integration tests (#1799)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored Jul 5, 2023
1 parent a6c4188 commit e2204a7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"issues": [],
"errors": [
{
"message": "Failed to satisfy version constraints; tflint-ruleset-incompatiblehost requires >= 1.0, but TFLint version is 0.47.0",
"message": "Failed to satisfy version constraints; tflint-ruleset-incompatiblehost requires >= 1.0, but TFLint version is {{.Version}}",
"severity": "error"
}
]
Expand Down
44 changes: 31 additions & 13 deletions integrationtest/inspection/inspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"text/template"

"github.com/google/go-cmp/cmp"
"github.com/terraform-linters/tflint/cmd"
Expand All @@ -22,6 +24,10 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

type meta struct {
Version string
}

func TestIntegration(t *testing.T) {
cases := []struct {
Name string
Expand Down Expand Up @@ -216,11 +222,6 @@ func TestIntegration(t *testing.T) {
t.Fatal(err)
}

resultFile := "result.json"
if runtime.GOOS == "windows" && IsWindowsResultExist() {
resultFile = "result_windows.json"
}

if tc.Env != nil {
for k, v := range tc.Env {
t.Setenv(k, v)
Expand All @@ -236,13 +237,12 @@ func TestIntegration(t *testing.T) {

cli.Run(args)

b, err := os.ReadFile(filepath.Join(testDir, resultFile))
rawWant, err := readResultFile(testDir)
if err != nil {
t.Fatal(err)
}

var expected *formatter.JSONOutput
if err := json.Unmarshal(b, &expected); err != nil {
var want *formatter.JSONOutput
if err := json.Unmarshal(rawWant, &want); err != nil {
t.Fatal(err)
}

Expand All @@ -251,14 +251,32 @@ func TestIntegration(t *testing.T) {
t.Fatal(err)
}

if diff := cmp.Diff(got, expected); diff != "" {
if diff := cmp.Diff(got, want); diff != "" {
t.Fatal(diff)
}
})
}
}

func IsWindowsResultExist() bool {
_, err := os.Stat("result_windows.json")
return !os.IsNotExist(err)
func readResultFile(dir string) ([]byte, error) {
resultFile := "result.json"
if runtime.GOOS == "windows" {
if _, err := os.Stat(filepath.Join(dir, "result_windows.json")); !os.IsNotExist(err) {
resultFile = "result_windows.json"
}
}
if _, err := os.Stat(fmt.Sprintf("%s.tmpl", resultFile)); !os.IsNotExist(err) {
resultFile = fmt.Sprintf("%s.tmpl", resultFile)
}

if !strings.HasSuffix(resultFile, ".tmpl") {
return os.ReadFile(filepath.Join(dir, resultFile))
}

want := new(bytes.Buffer)
tmpl := template.Must(template.ParseFiles(filepath.Join(dir, resultFile)))
if err := tmpl.Execute(want, meta{Version: tflint.Version.String()}); err != nil {
return nil, err
}
return want.Bytes(), nil
}

0 comments on commit e2204a7

Please sign in to comment.