Setting recursive fmt to false, updatting recursive fmt test to validate

formatted files
This commit is contained in:
teddylear 2021-01-11 21:53:27 -05:00
parent ab4b3a8465
commit 261abe0cae
6 changed files with 139 additions and 156 deletions

View File

@ -147,7 +147,7 @@ func (va *FormatArgs) AddFlagSets(flags *flag.FlagSet) {
flags.BoolVar(&va.Check, "check", false, "check if the input is formatted")
flags.BoolVar(&va.Diff, "diff", false, "display the diff of formatting changes")
flags.BoolVar(&va.Write, "write", true, "overwrite source files instead of writing to stdout")
flags.BoolVar(&va.Recursive, "recursive", true, "Also process files in subdirectories")
flags.BoolVar(&va.Recursive, "recursive", false, "Also process files in subdirectories")
va.MetaArgs.AddFlagSets(flags)
}

View File

@ -1,10 +1,14 @@
package command
import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/stretchr/testify/assert"
)
@ -56,9 +60,76 @@ func TestFmt_Recursive(t *testing.T) {
Meta: testMeta(t),
}
args := []string{"-check=true", "-recursive=true", filepath.Join(testFixture("fmt"), "")}
unformattedData, err := ioutil.ReadFile("test-fixtures/fmt/unformatted.pkrvars.hcl")
if err != nil {
t.Fatalf("failed to open the unformatted fixture %s", err)
}
if code := c.Run(args); code != 3 {
var subDir string
subDir, err = ioutil.TempDir("test-fixtures/fmt", "sub_dir")
if err != nil {
t.Fatalf("failed to create sub level recurisve directory for test %s", err)
}
defer os.Remove(subDir)
var superSubDir string
superSubDir, err = ioutil.TempDir(subDir, "super_sub_dir")
if err != nil {
t.Fatalf("failed to create sub level recurisve directory for test %s", err)
}
defer os.Remove(superSubDir)
tf, err := ioutil.TempFile(subDir, "*.pkrvars.hcl")
if err != nil {
t.Fatalf("failed to create top level tempfile for test %s", err)
}
defer os.Remove(tf.Name())
_, _ = tf.Write(unformattedData)
tf.Close()
subTf, err := ioutil.TempFile(superSubDir, "*.pkrvars.hcl")
if err != nil {
t.Fatalf("failed to create sub level tempfile for test %s", err)
}
defer os.Remove(subTf.Name())
_, _ = subTf.Write(unformattedData)
subTf.Close()
var directoryDelimiter string
if runtime.GOOS == "windows" {
directoryDelimiter = "\\"
} else {
directoryDelimiter = "/"
}
dirSplit := strings.Split(subDir, directoryDelimiter)
// Need just last bit to of top level temp directory to call command
subDirIsolated := dirSplit[len(dirSplit)-1]
args := []string{"-recursive=true", filepath.Join(testFixture("fmt"), subDirIsolated)}
if code := c.Run(args); code != 0 {
fatalCommand(t, c.Meta)
}
formattedData, err := ioutil.ReadFile("test-fixtures/fmt/formatted.pkrvars.hcl")
if err != nil {
t.Fatalf("failed to open the formatted fixture %s", err)
}
validateFileIsFormatted(t, formattedData, tf)
validateFileIsFormatted(t, formattedData, subTf)
}
func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File) {
//lets re-read the tempfile which should now be formatted
data, err := ioutil.ReadFile(testFile.Name())
if err != nil {
t.Fatalf("failed to open the newly formatted fixture %s", err)
}
if diff := cmp.Diff(string(data), string(formattedData)); diff != "" {
t.Errorf("Unexpected format tfData output %s", diff)
}
}

View File

@ -0,0 +1,3 @@
ami_filter_name = "amzn2-ami-hvm-*-x86_64-gp2"
ami_filter_owners = ["137112412989"]

View File

@ -39,12 +39,73 @@ func TestHCL2Formatter_Format(t *testing.T) {
}
func TestHCL2Formatter_Recursive(t *testing.T) {
var buf bytes.Buffer
f := NewHCL2Formatter()
f.Output = &buf
f.Write = true
f.Recursive = true
_, diags := f.Format("testdata/format")
unformattedData, err := ioutil.ReadFile("testdata/format/unformatted.pkr.hcl")
if err != nil {
t.Fatalf("failed to open the unformatted fixture %s", err)
}
var subDir string
subDir, err = ioutil.TempDir("testdata/format", "sub_dir")
if err != nil {
t.Fatalf("failed to create sub level recurisve directory for test %s", err)
}
defer os.Remove(subDir)
var superSubDir string
superSubDir, err = ioutil.TempDir(subDir, "super_sub_dir")
if err != nil {
t.Fatalf("failed to create sub level recurisve directory for test %s", err)
}
defer os.Remove(superSubDir)
tf, err := ioutil.TempFile(subDir, "*.pkr.hcl")
if err != nil {
t.Fatalf("failed to create top level tempfile for test %s", err)
}
defer os.Remove(tf.Name())
_, _ = tf.Write(unformattedData)
tf.Close()
subTf, err := ioutil.TempFile(superSubDir, "*.pkr.hcl")
if err != nil {
t.Fatalf("failed to create sub level tempfile for test %s", err)
}
defer os.Remove(subTf.Name())
_, _ = subTf.Write(unformattedData)
subTf.Close()
_, diags := f.Format(subDir)
if diags.HasErrors() {
t.Fatalf("the call to Format failed unexpectedly %s", diags.Error())
}
formattedData, err := ioutil.ReadFile("testdata/format/formatted.pkr.hcl")
if err != nil {
t.Fatalf("failed to open the formatted fixture %s", err)
}
validateFileIsFormatted(t, formattedData, tf)
validateFileIsFormatted(t, formattedData, subTf)
}
func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File) {
//lets re-read the tempfile which should now be formatted
data, err := ioutil.ReadFile(testFile.Name())
if err != nil {
t.Fatalf("failed to open the newly formatted fixture %s", err)
}
if diff := cmp.Diff(string(data), string(formattedData)); diff != "" {
t.Errorf("Unexpected format tfData output %s", diff)
}
}
func TestHCL2Formatter_Format_Write(t *testing.T) {

View File

@ -1,149 +0,0 @@
// starts resources to provision them.
build {
sources = [
"source.amazon-ebs.ubuntu-1604",
"source.virtualbox-iso.ubuntu-1204",
]
provisioner "shell" {
string = coalesce(null, "", "string")
int = "${41 + 1}"
int64 = "${42 + 1}"
bool = "true"
trilean = true
duration = "${9 + 1}s"
map_string_string = {
a = "b"
c = "d"
}
slice_string = [
"a",
"b",
"c",
]
slice_slice_string = [
["a","b"],
["c","d"]
]
nested {
string = "string"
int = 42
int64 = 43
bool = true
trilean = true
duration = "10s"
map_string_string = {
a = "b"
c = "d"
}
slice_string = [
"a",
"b",
"c",
]
slice_slice_string = [
["a","b"],
["c","d"]
]
}
nested_slice {
}
}
provisioner "file" {
string = "string"
int = 42
int64 = 43
bool = true
trilean = true
duration = "10s"
map_string_string = {
a = "b"
c = "d"
}
slice_string = [
"a",
"b",
"c",
]
slice_slice_string = [
["a","b"],
["c","d"]
]
nested {
string = "string"
int = 42
int64 = 43
bool = true
trilean = true
duration = "10s"
map_string_string = {
a = "b"
c = "d"
}
slice_string = [
"a",
"b",
"c",
]
slice_slice_string = [
["a","b"],
["c","d"]
]
}
nested_slice {
}
}
post-processor "amazon-import" {
string = "string"
int = 42
int64 = 43
bool = true
trilean = true
duration = "10s"
map_string_string = {
a = "b"
c = "d"
}
slice_string = [
"a",
"b",
"c",
]
slice_slice_string = [
["a","b"],
["c","d"]
]
nested {
string = "string"
int = 42
int64 = 43
bool = true
trilean = true
duration = "10s"
map_string_string = {
a = "b"
c = "d"
}
slice_string = [
"a",
"b",
"c",
]
slice_slice_string = [
["a","b"],
["c","d"]
]
}
nested_slice {
}
}
}

View File

@ -1,3 +0,0 @@
ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2"
ami_filter_owners =[ "137112412989" ]