diff --git a/command/fmt_test.go b/command/fmt_test.go index cb20c5a9d..a7cf9b70c 100644 --- a/command/fmt_test.go +++ b/command/fmt_test.go @@ -1,6 +1,7 @@ package command import ( + "fmt" "io/ioutil" "os" "path/filepath" @@ -54,25 +55,63 @@ func TestFmt_unfomattedTemlateDirectory(t *testing.T) { } } +type RecursiveTestCase struct { + TestCaseName string + Recursion bool + TopLevelFilePreFormat []byte + LowerLevelFilePreFormat []byte + TopLevelFilePostFormat []byte + LowerLevelFilePostFormat []byte +} + func TestFmt_Recursive(t *testing.T) { + unformattedData := []byte(`ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2" +ami_filter_owners =[ "137112412989" ] + +`) + + formattedData := []byte(`ami_filter_name = "amzn2-ami-hvm-*-x86_64-gp2" +ami_filter_owners = ["137112412989"] + +`) + + recursiveTestCases := []RecursiveTestCase{ + { + TestCaseName: "With Recursive flag on", + Recursion: true, + TopLevelFilePreFormat: unformattedData, + LowerLevelFilePreFormat: unformattedData, + TopLevelFilePostFormat: formattedData, + LowerLevelFilePostFormat: formattedData, + }, + { + TestCaseName: "With Recursive flag off", + Recursion: false, + TopLevelFilePreFormat: unformattedData, + LowerLevelFilePreFormat: unformattedData, + TopLevelFilePostFormat: formattedData, + LowerLevelFilePostFormat: unformattedData, + }, + } + c := &FormatCommand{ Meta: testMeta(t), } - unformattedData, err := ioutil.ReadFile("test-fixtures/fmt/unformatted.pkrvars.hcl") - if err != nil { - t.Fatalf("failed to open the unformatted fixture %s", err) + for _, tc := range recursiveTestCases { + executeRecursiveTestCase(t, tc, c) } +} - var subDir string - subDir, err = ioutil.TempDir("test-fixtures/fmt", "sub_dir") +func executeRecursiveTestCase(t *testing.T, tc RecursiveTestCase, c *FormatCommand) { + // Creating temp directories and files + subDir, err := ioutil.TempDir("test-fixtures/fmt", "sub_dir") if err != nil { t.Fatalf("failed to create sub level recurisve directory for test %s", err) } defer os.Remove(subDir) - var superSubDir string - superSubDir, err = ioutil.TempDir(subDir, "super_sub_dir") + superSubDir, err := ioutil.TempDir(subDir, "super_sub_dir") if err != nil { t.Fatalf("failed to create sub level recurisve directory for test %s", err) } @@ -84,69 +123,46 @@ func TestFmt_Recursive(t *testing.T) { } defer os.Remove(tf.Name()) - _, _ = tf.Write(unformattedData) + _, _ = 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(unformattedData) + _, _ = subTf.Write(tc.LowerLevelFilePreFormat) subTf.Close() - args := []string{"-recursive=true", subDir} + var args []string + if tc.Recursion { + args = []string{"-recursive=true", subDir} + } else { + args = []string{subDir} + } if code := c.Run(args); code != 0 { fatalCommand(t, c.Meta) } - formattedData, err := ioutil.ReadFile("test-fixtures/fmt/formatted.pkrvars.hcl") - if err != nil { - t.Fatalf("failed to open the formatted fixture %s", err) - } - - 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) + validateFileIsFormatted(t, tc.TopLevelFilePostFormat, tf, tc) + validateFileIsFormatted(t, tc.LowerLevelFilePostFormat, subTf, tc) } -func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File) { - //lets re-read the tempfile which should now be formatted +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) } if diff := cmp.Diff(string(data), string(formattedData)); diff != "" { - t.Errorf("Unexpected format tfData output %s", diff) + t.Errorf("Unexpected format tfData output on tc: %v, diff: %s", tc.TestCaseName, diff) } } diff --git a/hcl2template/formatter.go b/hcl2template/formatter.go index aa275a35e..42534805d 100644 --- a/hcl2template/formatter.go +++ b/hcl2template/formatter.go @@ -102,9 +102,8 @@ func (f *HCL2Formatter) Format(path string) (int, hcl.Diagnostics) { tempBytesModified, tempDiags = f.Format(filename) bytesModified += tempBytesModified diags = diags.Extend(tempDiags) - } else { - continue } + continue } if isHcl2FileOrVarFile(filename) { bytesModified, diags = f.formatFile(filename, diags, bytesModified) diff --git a/hcl2template/formatter_test.go b/hcl2template/formatter_test.go index b084f634a..d30413069 100644 --- a/hcl2template/formatter_test.go +++ b/hcl2template/formatter_test.go @@ -38,20 +38,352 @@ func TestHCL2Formatter_Format(t *testing.T) { } } +type FormatterRecursiveTestCase struct { + TestCaseName string + Recursion bool + TopLevelFilePreFormat []byte + LowerLevelFilePreFormat []byte + TopLevelFilePostFormat []byte + LowerLevelFilePostFormat []byte +} + func TestHCL2Formatter_Recursive(t *testing.T) { + unformattedData := []byte(` +// starts resources to provision them. +build { + sources = [ + "source.amazon-ebs.ubuntu-1604", + "source.virtualbox-iso.ubuntu-1204", + ] + + provisioner "shell" { + string = coalesce(null, "", "string") + int = "${41 + 1}" + int64 = "${42 + 1}" + bool = "true" + trilean = true + duration = "${9 + 1}s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a","b"], + ["c","d"] + ] + + nested { + string = "string" + int = 42 + int64 = 43 + bool = true + trilean = true + duration = "10s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a","b"], + ["c","d"] + ] + } + + nested_slice { + } + } + + provisioner "file" { + string = "string" + int = 42 + int64 = 43 + bool = true + trilean = true + duration = "10s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a","b"], + ["c","d"] + ] + + nested { + string = "string" + int = 42 + int64 = 43 + bool = true + trilean = true + duration = "10s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a","b"], + ["c","d"] + ] + } + + nested_slice { + } + } + + post-processor "amazon-import" { + string = "string" + int = 42 + int64 = 43 + bool = true + trilean = true + duration = "10s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a","b"], + ["c","d"] + ] + + nested { + string = "string" + int = 42 + int64 = 43 + bool = true + trilean = true + duration = "10s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a","b"], + ["c","d"] + ] + } + + nested_slice { + } + } +} +`) + + formattedData := []byte(` +// starts resources to provision them. +build { + sources = [ + "source.amazon-ebs.ubuntu-1604", + "source.virtualbox-iso.ubuntu-1204", + ] + + provisioner "shell" { + string = coalesce(null, "", "string") + int = "${41 + 1}" + int64 = "${42 + 1}" + bool = "true" + trilean = true + duration = "${9 + 1}s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a", "b"], + ["c", "d"] + ] + + nested { + string = "string" + int = 42 + int64 = 43 + bool = true + trilean = true + duration = "10s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a", "b"], + ["c", "d"] + ] + } + + nested_slice { + } + } + + provisioner "file" { + string = "string" + int = 42 + int64 = 43 + bool = true + trilean = true + duration = "10s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a", "b"], + ["c", "d"] + ] + + nested { + string = "string" + int = 42 + int64 = 43 + bool = true + trilean = true + duration = "10s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a", "b"], + ["c", "d"] + ] + } + + nested_slice { + } + } + + post-processor "amazon-import" { + string = "string" + int = 42 + int64 = 43 + bool = true + trilean = true + duration = "10s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a", "b"], + ["c", "d"] + ] + + nested { + string = "string" + int = 42 + int64 = 43 + bool = true + trilean = true + duration = "10s" + map_string_string = { + a = "b" + c = "d" + } + slice_string = [ + "a", + "b", + "c", + ] + slice_slice_string = [ + ["a", "b"], + ["c", "d"] + ] + } + + nested_slice { + } + } +} +`) + var buf bytes.Buffer f := NewHCL2Formatter() f.Output = &buf f.Write = true - f.Recursive = true - unformattedData, err := ioutil.ReadFile("testdata/format/unformatted.pkr.hcl") - if err != nil { - t.Fatalf("failed to open the unformatted fixture %s", err) + recursiveTestCases := []FormatterRecursiveTestCase{ + { + TestCaseName: "With Recursive flag on", + Recursion: true, + TopLevelFilePreFormat: unformattedData, + LowerLevelFilePreFormat: unformattedData, + TopLevelFilePostFormat: formattedData, + LowerLevelFilePostFormat: formattedData, + }, + { + TestCaseName: "With Recursive flag off", + Recursion: false, + TopLevelFilePreFormat: unformattedData, + LowerLevelFilePreFormat: unformattedData, + TopLevelFilePostFormat: formattedData, + LowerLevelFilePostFormat: unformattedData, + }, } + for _, tc := range recursiveTestCases { + executeRecursiveTestCase(t, tc, f) + } +} + +func executeRecursiveTestCase(t *testing.T, tc FormatterRecursiveTestCase, f *HCL2Formatter) { + f.Recursive = tc.Recursion + var subDir string - subDir, err = ioutil.TempDir("testdata/format", "sub_dir") + subDir, err := ioutil.TempDir("testdata/format", "sub_dir") if err != nil { t.Fatalf("failed to create sub level recurisve directory for test %s", err) } @@ -70,7 +402,7 @@ func TestHCL2Formatter_Recursive(t *testing.T) { } defer os.Remove(tf.Name()) - _, _ = tf.Write(unformattedData) + _, _ = tf.Write(tc.TopLevelFilePreFormat) tf.Close() subTf, err := ioutil.TempFile(superSubDir, "*.pkr.hcl") @@ -79,7 +411,7 @@ func TestHCL2Formatter_Recursive(t *testing.T) { } defer os.Remove(subTf.Name()) - _, _ = subTf.Write(unformattedData) + _, _ = subTf.Write(tc.LowerLevelFilePreFormat) subTf.Close() _, diags := f.Format(subDir) @@ -87,17 +419,11 @@ func TestHCL2Formatter_Recursive(t *testing.T) { t.Fatalf("the call to Format failed unexpectedly %s", diags.Error()) } - formattedData, err := ioutil.ReadFile("testdata/format/formatted.pkr.hcl") - if err != nil { - t.Fatalf("failed to open the formatted fixture %s", err) - } - - validateFileIsFormatted(t, formattedData, tf) - validateFileIsFormatted(t, formattedData, subTf) + validateFileIsFormatted(t, tc.TopLevelFilePostFormat, tf) + validateFileIsFormatted(t, tc.LowerLevelFilePostFormat, subTf) } func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File) { - //lets re-read the tempfile which should now be formatted data, err := ioutil.ReadFile(testFile.Name()) if err != nil { t.Fatalf("failed to open the newly formatted fixture %s", err)