forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter_test.go
109 lines (90 loc) · 2.75 KB
/
formatter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package hcl2template
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestHCL2Formatter_Format(t *testing.T) {
tt := []struct {
Name string
Path string
FormatExpected bool
}{
{Name: "Unformatted file", Path: "testdata/format/unformatted.pkr.hcl", FormatExpected: true},
{Name: "Unformatted vars file", Path: "testdata/format/unformatted.pkrvars.hcl", FormatExpected: true},
{Name: "Formatted file", Path: "testdata/format/formatted.pkr.hcl"},
{Name: "Directory", Path: "testdata/format", FormatExpected: true},
}
for _, tc := range tt {
tc := tc
var buf bytes.Buffer
f := NewHCL2Formatter()
f.Output = &buf
_, diags := f.Format(tc.Path)
if diags.HasErrors() {
t.Fatalf("the call to Format failed unexpectedly %s", diags.Error())
}
if buf.String() != "" && tc.FormatExpected == false {
t.Errorf("Format(%q) should contain the name of the formatted file(s), but got %q", tc.Path, buf.String())
}
}
}
func TestHCL2Formatter_Format_Write(t *testing.T) {
var buf bytes.Buffer
f := NewHCL2Formatter()
f.Output = &buf
f.Write = true
unformattedData, err := ioutil.ReadFile("testdata/format/unformatted.pkr.hcl")
if err != nil {
t.Fatalf("failed to open the unformatted fixture %s", err)
}
tf, err := ioutil.TempFile("", "*.pkr.hcl")
if err != nil {
t.Fatalf("failed to create tempfile for test %s", err)
}
defer os.Remove(tf.Name())
_, _ = tf.Write(unformattedData)
tf.Close()
_, diags := f.Format(tf.Name())
if diags.HasErrors() {
t.Fatalf("the call to Format failed unexpectedly %s", diags.Error())
}
//lets re-read the tempfile which should now be formatted
data, err := ioutil.ReadFile(tf.Name())
if err != nil {
t.Fatalf("failed to open the newly formatted fixture %s", err)
}
formattedData, err := ioutil.ReadFile("testdata/format/formatted.pkr.hcl")
if err != nil {
t.Fatalf("failed to open the formatted fixture %s", err)
}
if diff := cmp.Diff(string(data), string(formattedData)); diff != "" {
t.Errorf("Unexpected format output %s", diff)
}
}
func TestHCL2Formatter_Format_ShowDiff(t *testing.T) {
if _, err := exec.LookPath("diff"); err != nil {
t.Skip("Skipping test because diff is not in the executable PATH")
}
var buf bytes.Buffer
f := HCL2Formatter{
Output: &buf,
ShowDiff: true,
}
_, diags := f.Format("testdata/format/unformatted.pkr.hcl")
if diags.HasErrors() {
t.Fatalf("the call to Format failed unexpectedly %s", diags.Error())
}
diffHeader := `
--- old/testdata/format/unformatted.pkr.hcl
+++ new/testdata/format/unformatted.pkr.hcl
@@ -1,149 +1,149 @@
`
if !strings.Contains(buf.String(), diffHeader) {
t.Errorf("expected buf to contain a file diff, but instead we got %s", buf.String())
}
}