add tests for piping fmt
This commit is contained in:
parent
7d30a5d79d
commit
be7d7313c5
|
@ -2,12 +2,14 @@ package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
@ -54,18 +56,20 @@ func TestFmt_unfomattedTemlateDirectory(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFmt_Recursive(t *testing.T) {
|
const (
|
||||||
unformattedData := `
|
unformattedHCL = `
|
||||||
ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2"
|
ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2"
|
||||||
ami_filter_owners =[ "137112412989" ]
|
ami_filter_owners =[ "137112412989" ]
|
||||||
|
|
||||||
`
|
`
|
||||||
|
formattedHCL = `
|
||||||
formattedData := `
|
|
||||||
ami_filter_name = "amzn2-ami-hvm-*-x86_64-gp2"
|
ami_filter_name = "amzn2-ami-hvm-*-x86_64-gp2"
|
||||||
ami_filter_owners = ["137112412989"]
|
ami_filter_owners = ["137112412989"]
|
||||||
|
|
||||||
`
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFmt_Recursive(t *testing.T) {
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
@ -77,34 +81,34 @@ ami_filter_owners = ["137112412989"]
|
||||||
name: "nested formats recursively",
|
name: "nested formats recursively",
|
||||||
formatArgs: []string{"-recursive=true"},
|
formatArgs: []string{"-recursive=true"},
|
||||||
alreadyPresentContent: map[string]string{
|
alreadyPresentContent: map[string]string{
|
||||||
"foo/bar/baz.pkr.hcl": unformattedData,
|
"foo/bar/baz.pkr.hcl": unformattedHCL,
|
||||||
"foo/bar/baz/woo.pkrvars.hcl": unformattedData,
|
"foo/bar/baz/woo.pkrvars.hcl": unformattedHCL,
|
||||||
"potato": unformattedData,
|
"potato": unformattedHCL,
|
||||||
"foo/bar/potato": unformattedData,
|
"foo/bar/potato": unformattedHCL,
|
||||||
"bar.pkr.hcl": unformattedData,
|
"bar.pkr.hcl": unformattedHCL,
|
||||||
},
|
},
|
||||||
fileCheck: fileCheck{
|
fileCheck: fileCheck{
|
||||||
expectedContent: map[string]string{
|
expectedContent: map[string]string{
|
||||||
"foo/bar/baz.pkr.hcl": formattedData,
|
"foo/bar/baz.pkr.hcl": formattedHCL,
|
||||||
"foo/bar/baz/woo.pkrvars.hcl": formattedData,
|
"foo/bar/baz/woo.pkrvars.hcl": formattedHCL,
|
||||||
"potato": unformattedData,
|
"potato": unformattedHCL,
|
||||||
"foo/bar/potato": unformattedData,
|
"foo/bar/potato": unformattedHCL,
|
||||||
"bar.pkr.hcl": formattedData,
|
"bar.pkr.hcl": formattedHCL,
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "nested no recursive format",
|
name: "nested no recursive format",
|
||||||
formatArgs: []string{},
|
formatArgs: []string{},
|
||||||
alreadyPresentContent: map[string]string{
|
alreadyPresentContent: map[string]string{
|
||||||
"foo/bar/baz.pkr.hcl": unformattedData,
|
"foo/bar/baz.pkr.hcl": unformattedHCL,
|
||||||
"foo/bar/baz/woo.pkrvars.hcl": unformattedData,
|
"foo/bar/baz/woo.pkrvars.hcl": unformattedHCL,
|
||||||
"bar.pkr.hcl": unformattedData,
|
"bar.pkr.hcl": unformattedHCL,
|
||||||
},
|
},
|
||||||
fileCheck: fileCheck{
|
fileCheck: fileCheck{
|
||||||
expectedContent: map[string]string{
|
expectedContent: map[string]string{
|
||||||
"foo/bar/baz.pkr.hcl": unformattedData,
|
"foo/bar/baz.pkr.hcl": unformattedHCL,
|
||||||
"foo/bar/baz/woo.pkrvars.hcl": unformattedData,
|
"foo/bar/baz/woo.pkrvars.hcl": unformattedHCL,
|
||||||
"bar.pkr.hcl": formattedData,
|
"bar.pkr.hcl": formattedHCL,
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -138,3 +142,31 @@ ami_filter_owners = ["137112412989"]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_fmt_pipe(t *testing.T) {
|
||||||
|
|
||||||
|
tc := []struct {
|
||||||
|
piped string
|
||||||
|
command []string
|
||||||
|
env []string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{unformattedHCL, []string{"fmt", "-"}, nil, formattedHCL + "\n"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tc {
|
||||||
|
t.Run(fmt.Sprintf("echo %q | packer %s", tc.piped, tc.command), func(t *testing.T) {
|
||||||
|
p := helperCommand(t, tc.command...)
|
||||||
|
p.Stdin = strings.NewReader(tc.piped)
|
||||||
|
p.Env = append(p.Env, tc.env...)
|
||||||
|
bs, err := p.Output()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%v: %s", err, bs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if diff := cmp.Diff(tc.expected, string(bs)); diff != "" {
|
||||||
|
t.Fatalf("%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue