MCPcopy
hub / github.com/hashicorp/packer / TestHCL2Formatter_Format_Write

Function TestHCL2Formatter_Format_Write

hcl2template/formatter_test.go:45–87  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

43}
44
45func 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
89func TestHCL2Formatter_Format_ShowDiff(t *testing.T) {
90

Callers

nothing calls this directly

Calls 7

FormatMethod · 0.95
NewHCL2FormatterFunction · 0.85
NameMethod · 0.65
WriteMethod · 0.65
ErrorMethod · 0.65
CloseMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…