From 6adf1f6659fbb4522be557eec0ca4313eea74cd7 Mon Sep 17 00:00:00 2001 From: teddylear Date: Mon, 18 Jan 2021 15:05:18 -0500 Subject: [PATCH] Fixing recursive fmt tests syntax and adding test case when recursive option is off --- command/fmt_test.go | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/command/fmt_test.go b/command/fmt_test.go index a57383999..cb20c5a9d 100644 --- a/command/fmt_test.go +++ b/command/fmt_test.go @@ -4,7 +4,6 @@ import ( "io/ioutil" "os" "path/filepath" - "runtime" "strings" "testing" @@ -97,17 +96,7 @@ func TestFmt_Recursive(t *testing.T) { _, _ = subTf.Write(unformattedData) subTf.Close() - var directoryDelimiter string - if runtime.GOOS == "windows" { - directoryDelimiter = "\\" - } else { - directoryDelimiter = "/" - } - - dirSplit := strings.Split(subDir, directoryDelimiter) - // Need just last bit to of top level temp directory to call command - subDirIsolated := dirSplit[len(dirSplit)-1] - args := []string{"-recursive=true", filepath.Join(testFixture("fmt"), subDirIsolated)} + args := []string{"-recursive=true", subDir} if code := c.Run(args); code != 0 { fatalCommand(t, c.Meta) @@ -120,6 +109,34 @@ func TestFmt_Recursive(t *testing.T) { validateFileIsFormatted(t, formattedData, tf) validateFileIsFormatted(t, formattedData, subTf) + + //Testing with recursive flag off that sub directories are not formatted + tf, err = ioutil.TempFile(subDir, "*.pkrvars.hcl") + if err != nil { + t.Fatalf("failed to create top level tempfile for test %s", err) + } + defer os.Remove(tf.Name()) + + _, _ = tf.Write(unformattedData) + tf.Close() + + 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(unformattedData) + subTf.Close() + + args = []string{subDir} + + if code := c.Run(args); code != 0 { + fatalCommand(t, c.Meta) + } + + validateFileIsFormatted(t, formattedData, tf) + validateFileIsFormatted(t, unformattedData, subTf) } func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File) {