From 40a97e29db3176b9b9897351777a875040afbe28 Mon Sep 17 00:00:00 2001 From: teddylear Date: Fri, 29 Jan 2021 22:11:41 -0500 Subject: [PATCH] Clean up recursive format tests to be more accurate --- command/fmt_test.go | 57 +++++++++++++++------------------- hcl2template/formatter_test.go | 56 ++++++++++++++++----------------- 2 files changed, 53 insertions(+), 60 deletions(-) diff --git a/command/fmt_test.go b/command/fmt_test.go index a7cf9b70c..c66777a9e 100644 --- a/command/fmt_test.go +++ b/command/fmt_test.go @@ -1,7 +1,6 @@ package command import ( - "fmt" "io/ioutil" "os" "path/filepath" @@ -105,64 +104,58 @@ ami_filter_owners = ["137112412989"] func executeRecursiveTestCase(t *testing.T, tc RecursiveTestCase, c *FormatCommand) { // 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 { - 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) - superSubDir, err := ioutil.TempDir(subDir, "super_sub_dir") + topTempFile, err := ioutil.TempFile(topDir, "*.pkrvars.hcl") 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 { - 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) - tf.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() + _, _ = subTempFile.Write(tc.LowerLevelFilePreFormat) + subTempFile.Close() var args []string if tc.Recursion { - args = []string{"-recursive=true", subDir} + args = []string{"-recursive=true", topDir} } else { - args = []string{subDir} + args = []string{topDir} } if code := c.Run(args); code != 0 { fatalCommand(t, c.Meta) } - validateFileIsFormatted(t, tc.TopLevelFilePostFormat, tf, tc) - validateFileIsFormatted(t, tc.LowerLevelFilePostFormat, subTf, tc) + validateFileIsFormatted(t, tc.TopLevelFilePostFormat, topTempFile, tc) + validateFileIsFormatted(t, tc.LowerLevelFilePostFormat, subTempFile, tc) } func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File, tc RecursiveTestCase) { data, err := ioutil.ReadFile(testFile.Name()) 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 != "" { - 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) } } diff --git a/hcl2template/formatter_test.go b/hcl2template/formatter_test.go index d30413069..469237887 100644 --- a/hcl2template/formatter_test.go +++ b/hcl2template/formatter_test.go @@ -382,55 +382,55 @@ build { func executeRecursiveTestCase(t *testing.T, tc FormatterRecursiveTestCase, f *HCL2Formatter) { f.Recursive = tc.Recursion - var subDir string - subDir, err := ioutil.TempDir("testdata/format", "sub_dir") + var topDir string + topDir, err := ioutil.TempDir("testdata/format", "top-dir") 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) - var superSubDir string - superSubDir, err = ioutil.TempDir(subDir, "super_sub_dir") + topTempFile, err := ioutil.TempFile(topDir, "*.pkr.hcl") 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 { - 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) - tf.Close() + _, _ = subTempFile.Write(tc.LowerLevelFilePreFormat) + subTempFile.Close() - subTf, err := ioutil.TempFile(superSubDir, "*.pkr.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() - - _, diags := f.Format(subDir) + _, diags := f.Format(topDir) 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.LowerLevelFilePostFormat, subTf) + validateFileIsFormatted(t, tc.TopLevelFilePostFormat, topTempFile, tc.TestCaseName) + 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()) 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 != "" { - t.Errorf("Unexpected format tfData output %s", diff) + t.Errorf("Unexpected format tfData output for test case: %s, errors: %s", testCaseName, diff) } }