github.com/hashicorp/packer@v1.14.3/hcl2template/formatter_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package hcl2template
     5  
     6  import (
     7  	"bytes"
     8  	"os"
     9  	"os/exec"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/google/go-cmp/cmp"
    14  )
    15  
    16  func TestHCL2Formatter_Format(t *testing.T) {
    17  	tt := []struct {
    18  		Name           string
    19  		Paths          []string
    20  		FormatExpected bool
    21  	}{
    22  		{Name: "Unformatted file", Paths: []string{"testdata/format/unformatted.pkr.hcl"}, FormatExpected: true},
    23  		{Name: "Unformatted vars file", Paths: []string{"testdata/format/unformatted.pkrvars.hcl"}, FormatExpected: true},
    24  		{Name: "Formatted file", Paths: []string{"testdata/format/formatted.pkr.hcl"}},
    25  		{Name: "Directory", Paths: []string{"testdata/format"}, FormatExpected: true},
    26  		{Name: "No file", Paths: []string{}, FormatExpected: false},
    27  		{Name: "Multi File", Paths: []string{"testdata/format/unformatted.pkr.hcl", "testdata/format/unformatted.pkrvars.hcl"}, FormatExpected: true},
    28  	}
    29  
    30  	for _, tc := range tt {
    31  		tc := tc
    32  		var buf bytes.Buffer
    33  		f := NewHCL2Formatter()
    34  		f.Output = &buf
    35  		_, diags := f.Format(tc.Paths)
    36  		if diags.HasErrors() {
    37  			t.Fatalf("the call to Format failed unexpectedly %s", diags.Error())
    38  		}
    39  		if buf.String() != "" && tc.FormatExpected == false {
    40  			t.Errorf("Format(%q) should contain the name of the formatted file(s), but got %q", tc.Paths, buf.String())
    41  		}
    42  	}
    43  }
    44  
    45  func TestHCL2Formatter_Format_Write(t *testing.T) {
    46  
    47  	var buf bytes.Buffer
    48  	f := NewHCL2Formatter()
    49  	f.Output = &buf
    50  	f.Write = true
    51  
    52  	unformattedData, err := os.ReadFile("testdata/format/unformatted.pkr.hcl")
    53  	if err != nil {
    54  		t.Fatalf("failed to open the unformatted fixture %s", err)
    55  	}
    56  
    57  	tf, err := os.CreateTemp("", "*.pkr.hcl")
    58  	if err != nil {
    59  		t.Fatalf("failed to create tempfile for test %s", err)
    60  	}
    61  	defer os.Remove(tf.Name())
    62  
    63  	_, _ = tf.Write(unformattedData)
    64  	tf.Close()
    65  
    66  	var paths []string
    67  	paths = append(paths, tf.Name())
    68  	_, diags := f.Format(paths)
    69  	if diags.HasErrors() {
    70  		t.Fatalf("the call to Format failed unexpectedly %s", diags.Error())
    71  	}
    72  
    73  	//lets re-read the tempfile which should now be formatted
    74  	data, err := os.ReadFile(tf.Name())
    75  	if err != nil {
    76  		t.Fatalf("failed to open the newly formatted fixture %s", err)
    77  	}
    78  
    79  	formattedData, err := os.ReadFile("testdata/format/formatted.pkr.hcl")
    80  	if err != nil {
    81  		t.Fatalf("failed to open the formatted fixture %s", err)
    82  	}
    83  
    84  	if diff := cmp.Diff(string(data), string(formattedData)); diff != "" {
    85  		t.Errorf("Unexpected format output %s", diff)
    86  	}
    87  }
    88  
    89  func TestHCL2Formatter_Format_ShowDiff(t *testing.T) {
    90  
    91  	if _, err := exec.LookPath("diff"); err != nil {
    92  		t.Skip("Skipping test because diff is not in the executable PATH")
    93  	}
    94  
    95  	var buf bytes.Buffer
    96  	f := HCL2Formatter{
    97  		Output:   &buf,
    98  		ShowDiff: true,
    99  	}
   100  
   101  	var paths []string
   102  	paths = append(paths, "testdata/format/unformatted.pkr.hcl")
   103  	_, diags := f.Format(paths)
   104  	if diags.HasErrors() {
   105  		t.Fatalf("the call to Format failed unexpectedly %s", diags.Error())
   106  	}
   107  
   108  	diffHeader := `
   109  --- old/testdata/format/unformatted.pkr.hcl
   110  +++ new/testdata/format/unformatted.pkr.hcl
   111  @@ -1,149 +1,149 @@
   112  `
   113  	if !strings.Contains(buf.String(), diffHeader) {
   114  		t.Errorf("expected buf to contain a file diff, but instead we got %s", buf.String())
   115  	}
   116  
   117  }
   118  
   119  func TestHCL2Formatter_FormatNegativeCases(t *testing.T) {
   120  	tt := []struct {
   121  		Name        string
   122  		Paths       []string
   123  		errExpected bool
   124  	}{
   125  		{Name: "Unformatted file", Paths: []string{"testdata/format/test.json"}, errExpected: true},
   126  	}
   127  
   128  	for _, tc := range tt {
   129  		tc := tc
   130  		var buf bytes.Buffer
   131  		f := NewHCL2Formatter()
   132  		f.Output = &buf
   133  		_, diags := f.Format(tc.Paths)
   134  		if tc.errExpected && !diags.HasErrors() {
   135  			t.Fatalf("Expected error but got none")
   136  		}
   137  
   138  		if diags[0].Detail != "file testdata/format/test.json is not a HCL file" {
   139  			t.Fatalf("Expected error messge did not received")
   140  		}
   141  	}
   142  }