Clean up recursive format tests to be more accurate

This commit is contained in:
teddylear 2021-01-29 22:11:41 -05:00
parent 93df53a275
commit 40a97e29db
2 changed files with 53 additions and 60 deletions

View File

@ -1,7 +1,6 @@
package command package command
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -105,64 +104,58 @@ ami_filter_owners = ["137112412989"]
func executeRecursiveTestCase(t *testing.T, tc RecursiveTestCase, c *FormatCommand) { func executeRecursiveTestCase(t *testing.T, tc RecursiveTestCase, c *FormatCommand) {
// Creating temp directories and files // Creating temp directories and files
subDir, err := ioutil.TempDir("test-fixtures/fmt", "sub_dir") topDir, err := ioutil.TempDir("test-fixtures/fmt", "top-dir")
if err != nil { if err != nil {
t.Fatalf("failed to create sub level recurisve directory for test %s", err) t.Fatalf("failed to create sub level recurisve directory for test case: %s, error: %s", tc.TestCaseName, err)
}
defer os.Remove(topDir)
subDir, err := ioutil.TempDir(topDir, "sub-dir")
if err != nil {
t.Fatalf("failed to create sub level recurisve directory for test case: %s, error: %s", tc.TestCaseName, err)
} }
defer os.Remove(subDir) defer os.Remove(subDir)
superSubDir, err := ioutil.TempDir(subDir, "super_sub_dir") topTempFile, err := ioutil.TempFile(topDir, "*.pkrvars.hcl")
if err != nil { if err != nil {
t.Fatalf("failed to create sub level recurisve directory for test %s", err) t.Fatalf("failed to create top level tempfile for test case: %s, error: %s", tc.TestCaseName, err)
} }
defer os.Remove(superSubDir) defer os.Remove(topTempFile.Name())
tf, err := ioutil.TempFile(subDir, "*.pkrvars.hcl") _, _ = topTempFile.Write(tc.TopLevelFilePreFormat)
topTempFile.Close()
subTempFile, err := ioutil.TempFile(subDir, "*.pkrvars.hcl")
if err != nil { if err != nil {
t.Fatalf("failed to create top level tempfile for test %s", err) t.Fatalf("failed to create sub level tempfile for test case: %s, error: %s", tc.TestCaseName, err)
} }
defer os.Remove(tf.Name()) defer os.Remove(subTempFile.Name())
_, _ = tf.Write(tc.TopLevelFilePreFormat) _, _ = subTempFile.Write(tc.LowerLevelFilePreFormat)
tf.Close() subTempFile.Close()
data, err := ioutil.ReadFile(tf.Name())
if err != nil {
t.Fatalf("failed to open the newly formatted fixture %s", err)
}
fmt.Println(fmt.Sprintf("top level data: %v", data))
subTf, err := ioutil.TempFile(superSubDir, "*.pkrvars.hcl")
if err != nil {
t.Fatalf("failed to create sub level tempfile for test %s", err)
}
defer os.Remove(subTf.Name())
_, _ = subTf.Write(tc.LowerLevelFilePreFormat)
subTf.Close()
var args []string var args []string
if tc.Recursion { if tc.Recursion {
args = []string{"-recursive=true", subDir} args = []string{"-recursive=true", topDir}
} else { } else {
args = []string{subDir} args = []string{topDir}
} }
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
fatalCommand(t, c.Meta) fatalCommand(t, c.Meta)
} }
validateFileIsFormatted(t, tc.TopLevelFilePostFormat, tf, tc) validateFileIsFormatted(t, tc.TopLevelFilePostFormat, topTempFile, tc)
validateFileIsFormatted(t, tc.LowerLevelFilePostFormat, subTf, tc) validateFileIsFormatted(t, tc.LowerLevelFilePostFormat, subTempFile, tc)
} }
func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File, tc RecursiveTestCase) { func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File, tc RecursiveTestCase) {
data, err := ioutil.ReadFile(testFile.Name()) data, err := ioutil.ReadFile(testFile.Name())
if err != nil { if err != nil {
t.Fatalf("failed to open the newly formatted fixture %s", err) t.Fatalf("failed to open the newly formatted fixture for test case: %s, error: %s", tc.TestCaseName, err)
} }
if diff := cmp.Diff(string(data), string(formattedData)); diff != "" { if diff := cmp.Diff(string(data), string(formattedData)); diff != "" {
t.Errorf("Unexpected format tfData output on tc: %v, diff: %s", tc.TestCaseName, diff) t.Errorf("Unexpected format tfData output for test case: %v, diff: %s", tc.TestCaseName, diff)
} }
} }

View File

@ -382,55 +382,55 @@ build {
func executeRecursiveTestCase(t *testing.T, tc FormatterRecursiveTestCase, f *HCL2Formatter) { func executeRecursiveTestCase(t *testing.T, tc FormatterRecursiveTestCase, f *HCL2Formatter) {
f.Recursive = tc.Recursion f.Recursive = tc.Recursion
var subDir string var topDir string
subDir, err := ioutil.TempDir("testdata/format", "sub_dir") topDir, err := ioutil.TempDir("testdata/format", "top-dir")
if err != nil { if err != nil {
t.Fatalf("failed to create sub level recurisve directory for test %s", err) t.Fatalf("failed to create sub level recurisve directory for test case: %s, errors: %s", tc.TestCaseName, err)
}
defer os.Remove(topDir)
var subDir string
subDir, err = ioutil.TempDir(topDir, "sub-dir")
if err != nil {
t.Fatalf("failed to create sub level recurisve directory for test case: %s, errors: %s", tc.TestCaseName, err)
} }
defer os.Remove(subDir) defer os.Remove(subDir)
var superSubDir string topTempFile, err := ioutil.TempFile(topDir, "*.pkr.hcl")
superSubDir, err = ioutil.TempDir(subDir, "super_sub_dir")
if err != nil { if err != nil {
t.Fatalf("failed to create sub level recurisve directory for test %s", err) t.Fatalf("failed to create top level tempfile for test case: %s, errors: %s", tc.TestCaseName, err)
} }
defer os.Remove(superSubDir) defer os.Remove(topTempFile.Name())
tf, err := ioutil.TempFile(subDir, "*.pkr.hcl") _, _ = topTempFile.Write(tc.TopLevelFilePreFormat)
topTempFile.Close()
subTempFile, err := ioutil.TempFile(subDir, "*.pkr.hcl")
if err != nil { if err != nil {
t.Fatalf("failed to create top level tempfile for test %s", err) t.Fatalf("failed to create sub level tempfile for test case: %s, errors: %s", tc.TestCaseName, err)
} }
defer os.Remove(tf.Name()) defer os.Remove(subTempFile.Name())
_, _ = tf.Write(tc.TopLevelFilePreFormat) _, _ = subTempFile.Write(tc.LowerLevelFilePreFormat)
tf.Close() subTempFile.Close()
subTf, err := ioutil.TempFile(superSubDir, "*.pkr.hcl") _, diags := f.Format(topDir)
if err != nil {
t.Fatalf("failed to create sub level tempfile for test %s", err)
}
defer os.Remove(subTf.Name())
_, _ = subTf.Write(tc.LowerLevelFilePreFormat)
subTf.Close()
_, diags := f.Format(subDir)
if diags.HasErrors() { if diags.HasErrors() {
t.Fatalf("the call to Format failed unexpectedly %s", diags.Error()) t.Fatalf("the call to Format failed unexpectedly for test case: %s, errors: %s", tc.TestCaseName, diags.Error())
} }
validateFileIsFormatted(t, tc.TopLevelFilePostFormat, tf) validateFileIsFormatted(t, tc.TopLevelFilePostFormat, topTempFile, tc.TestCaseName)
validateFileIsFormatted(t, tc.LowerLevelFilePostFormat, subTf) validateFileIsFormatted(t, tc.LowerLevelFilePostFormat, subTempFile, tc.TestCaseName)
} }
func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File) { func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File, testCaseName string) {
data, err := ioutil.ReadFile(testFile.Name()) data, err := ioutil.ReadFile(testFile.Name())
if err != nil { if err != nil {
t.Fatalf("failed to open the newly formatted fixture %s", err) t.Fatalf("failed to open the newly formatted fixture for test case: %s, errors: %s", testCaseName, err)
} }
if diff := cmp.Diff(string(data), string(formattedData)); diff != "" { if diff := cmp.Diff(string(data), string(formattedData)); diff != "" {
t.Errorf("Unexpected format tfData output %s", diff) t.Errorf("Unexpected format tfData output for test case: %s, errors: %s", testCaseName, diff)
} }
} }