un-remove tests

This commit is contained in:
Adrien Delorme 2021-03-15 12:00:19 +01:00
parent fe21ff905d
commit e0fe579837
1 changed files with 76 additions and 0 deletions

View File

@ -2,7 +2,13 @@ package hcl2template
import ( import (
"bytes" "bytes"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing" "testing"
"github.com/google/go-cmp/cmp"
) )
func TestHCL2Formatter_Format(t *testing.T) { func TestHCL2Formatter_Format(t *testing.T) {
@ -31,3 +37,73 @@ func TestHCL2Formatter_Format(t *testing.T) {
} }
} }
} }
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())
}
}