Updating recursive formatter tests to be cleaner and table driven
This commit is contained in:
parent
ab7e89781a
commit
d3754e3021
|
@ -1,6 +1,7 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -54,42 +55,50 @@ func TestFmt_unfomattedTemlateDirectory(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type RecursiveTestCase struct {
|
|
||||||
TestCaseName string
|
|
||||||
Recursion bool
|
|
||||||
TopLevelFilePreFormat []byte
|
|
||||||
LowerLevelFilePreFormat []byte
|
|
||||||
TopLevelFilePostFormat []byte
|
|
||||||
LowerLevelFilePostFormat []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFmt_Recursive(t *testing.T) {
|
func TestFmt_Recursive(t *testing.T) {
|
||||||
unformattedData := []byte(`ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2"
|
unformattedData := `ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2"
|
||||||
ami_filter_owners =[ "137112412989" ]
|
ami_filter_owners =[ "137112412989" ]
|
||||||
|
|
||||||
`)
|
`
|
||||||
|
|
||||||
formattedData := []byte(`ami_filter_name = "amzn2-ami-hvm-*-x86_64-gp2"
|
formattedData := `ami_filter_name = "amzn2-ami-hvm-*-x86_64-gp2"
|
||||||
ami_filter_owners = ["137112412989"]
|
ami_filter_owners = ["137112412989"]
|
||||||
|
|
||||||
`)
|
`
|
||||||
|
|
||||||
recursiveTestCases := []RecursiveTestCase{
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
formatArgs []string // arguments passed to format
|
||||||
|
alreadyPresentContent map[string]string
|
||||||
|
expectedContent map[string]string
|
||||||
|
}{
|
||||||
{
|
{
|
||||||
TestCaseName: "With Recursive flag on",
|
name: "nested formats recursively",
|
||||||
Recursion: true,
|
formatArgs: []string{"-recursive=true"},
|
||||||
TopLevelFilePreFormat: unformattedData,
|
alreadyPresentContent: map[string]string{
|
||||||
LowerLevelFilePreFormat: unformattedData,
|
"foo/bar/baz": unformattedData,
|
||||||
TopLevelFilePostFormat: formattedData,
|
"foo/bar/baz/woo": unformattedData,
|
||||||
LowerLevelFilePostFormat: formattedData,
|
"": unformattedData,
|
||||||
|
},
|
||||||
|
expectedContent: map[string]string{
|
||||||
|
"foo/bar/baz": formattedData,
|
||||||
|
"foo/bar/baz/woo": formattedData,
|
||||||
|
"": formattedData,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
TestCaseName: "With Recursive flag off",
|
name: "nested no recursive format",
|
||||||
Recursion: false,
|
formatArgs: []string{},
|
||||||
TopLevelFilePreFormat: unformattedData,
|
alreadyPresentContent: map[string]string{
|
||||||
LowerLevelFilePreFormat: unformattedData,
|
"foo/bar/baz": unformattedData,
|
||||||
TopLevelFilePostFormat: formattedData,
|
"foo/bar/baz/woo": unformattedData,
|
||||||
LowerLevelFilePostFormat: unformattedData,
|
"": unformattedData,
|
||||||
|
},
|
||||||
|
expectedContent: map[string]string{
|
||||||
|
"foo/bar/baz": unformattedData,
|
||||||
|
"foo/bar/baz/woo": unformattedData,
|
||||||
|
"": formattedData,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,65 +106,93 @@ ami_filter_owners = ["137112412989"]
|
||||||
Meta: testMeta(t),
|
Meta: testMeta(t),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range recursiveTestCases {
|
testFileName := "test.pkrvars.hcl"
|
||||||
executeRecursiveTestCase(t, tc, c)
|
|
||||||
}
|
for _, tt := range tests {
|
||||||
}
|
topDir, err := ioutil.TempDir("test-fixtures/fmt", "temp-dir")
|
||||||
|
if err != nil {
|
||||||
func executeRecursiveTestCase(t *testing.T, tc RecursiveTestCase, c *FormatCommand) {
|
t.Fatalf("Failed to create TopDir for test case: %s, error: %v", tt.name, err)
|
||||||
// Creating temp directories and files
|
}
|
||||||
topDir, err := ioutil.TempDir("test-fixtures/fmt", "top-dir")
|
defer os.Remove(topDir)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create sub level recurisve directory for test case: %s, error: %s", tc.TestCaseName, err)
|
for testDir, content := range tt.alreadyPresentContent {
|
||||||
}
|
dir := filepath.Join(topDir, testDir)
|
||||||
defer os.Remove(topDir)
|
err := os.MkdirAll(dir, 0777)
|
||||||
|
if err != nil {
|
||||||
subDir, err := ioutil.TempDir(topDir, "sub-dir")
|
os.RemoveAll(topDir)
|
||||||
if err != nil {
|
t.Fatalf(
|
||||||
t.Fatalf("failed to create sub level recurisve directory for test case: %s, error: %s", tc.TestCaseName, err)
|
"Failed to create subDir: %s\n\n, for test case: %s\n\n, error: %v",
|
||||||
}
|
testDir,
|
||||||
defer os.Remove(subDir)
|
tt.name,
|
||||||
|
err)
|
||||||
topTempFile, err := ioutil.TempFile(topDir, "*.pkrvars.hcl")
|
}
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create top level tempfile for test case: %s, error: %s", tc.TestCaseName, err)
|
file, err := os.Create(filepath.Join(dir, testFileName))
|
||||||
}
|
if err != nil {
|
||||||
defer os.Remove(topTempFile.Name())
|
os.RemoveAll(topDir)
|
||||||
|
t.Fatalf("failed to create testfile at directory: %s\n\n, for test case: %s\n\n, error: %s",
|
||||||
_, _ = topTempFile.Write(tc.TopLevelFilePreFormat)
|
testDir,
|
||||||
topTempFile.Close()
|
tt.name,
|
||||||
|
err)
|
||||||
subTempFile, err := ioutil.TempFile(subDir, "*.pkrvars.hcl")
|
}
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create sub level tempfile for test case: %s, error: %s", tc.TestCaseName, err)
|
_, err = file.Write([]byte(content))
|
||||||
}
|
if err != nil {
|
||||||
defer os.Remove(subTempFile.Name())
|
os.RemoveAll(topDir)
|
||||||
|
t.Fatalf("failed to write to testfile at directory: %s\n\n, for test case: %s\n\n, error: %s",
|
||||||
_, _ = subTempFile.Write(tc.LowerLevelFilePreFormat)
|
testDir,
|
||||||
subTempFile.Close()
|
tt.name,
|
||||||
|
err)
|
||||||
var args []string
|
}
|
||||||
if tc.Recursion {
|
|
||||||
args = []string{"-recursive=true", topDir}
|
err = file.Close()
|
||||||
} else {
|
if err != nil {
|
||||||
args = []string{topDir}
|
os.RemoveAll(topDir)
|
||||||
}
|
t.Fatalf("failed to close testfile at directory: %s\n\n, for test case: %s\n\n, error: %s",
|
||||||
|
testDir,
|
||||||
if code := c.Run(args); code != 0 {
|
tt.name,
|
||||||
fatalCommand(t, c.Meta)
|
err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
validateFileIsFormatted(t, tc.TopLevelFilePostFormat, topTempFile, tc)
|
|
||||||
validateFileIsFormatted(t, tc.LowerLevelFilePostFormat, subTempFile, tc)
|
testArgs := append(tt.formatArgs, topDir)
|
||||||
}
|
if code := c.Run(testArgs); code != 0 {
|
||||||
|
os.RemoveAll(topDir)
|
||||||
func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File, tc RecursiveTestCase) {
|
ui := c.Meta.Ui.(*packersdk.BasicUi)
|
||||||
data, err := ioutil.ReadFile(testFile.Name())
|
out := ui.Writer.(*bytes.Buffer)
|
||||||
if err != nil {
|
err := ui.ErrorWriter.(*bytes.Buffer)
|
||||||
t.Fatalf("failed to open the newly formatted fixture for test case: %s, error: %s", tc.TestCaseName, err)
|
t.Fatalf(
|
||||||
}
|
"Bad exit code for test case: %s.\n\nStdout:\n\n%s\n\nStderr:\n\n%s",
|
||||||
|
tt.name,
|
||||||
if diff := cmp.Diff(string(data), string(formattedData)); diff != "" {
|
out.String(),
|
||||||
t.Errorf("Unexpected format tfData output for test case: %v, diff: %s", tc.TestCaseName, diff)
|
err.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
for expectedPath, expectedContent := range tt.expectedContent {
|
||||||
|
b, err := ioutil.ReadFile(filepath.Join(topDir, expectedPath, testFileName))
|
||||||
|
if err != nil {
|
||||||
|
os.RemoveAll(topDir)
|
||||||
|
t.Fatalf("ReadFile failed for test case: %s, error : %v", tt.name, err)
|
||||||
|
}
|
||||||
|
got := string(b)
|
||||||
|
if diff := cmp.Diff(got, expectedContent); diff != "" {
|
||||||
|
os.RemoveAll(topDir)
|
||||||
|
t.Errorf(
|
||||||
|
"format dir, unexpected result for test case: %s, path: %s, Expected: %s, Got: %s",
|
||||||
|
tt.name,
|
||||||
|
expectedPath,
|
||||||
|
expectedContent,
|
||||||
|
got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.RemoveAll(topDir)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf(
|
||||||
|
"Failed to delete top level test directory for test case: %s, please clean before another test run. Error: %s",
|
||||||
|
tt.name,
|
||||||
|
err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
ami_filter_name = "amzn2-ami-hvm-*-x86_64-gp2"
|
|
||||||
ami_filter_owners = ["137112412989"]
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -38,17 +39,8 @@ func TestHCL2Formatter_Format(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type FormatterRecursiveTestCase struct {
|
|
||||||
TestCaseName string
|
|
||||||
Recursion bool
|
|
||||||
TopLevelFilePreFormat []byte
|
|
||||||
LowerLevelFilePreFormat []byte
|
|
||||||
TopLevelFilePostFormat []byte
|
|
||||||
LowerLevelFilePostFormat []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHCL2Formatter_Recursive(t *testing.T) {
|
func TestHCL2Formatter_Recursive(t *testing.T) {
|
||||||
unformattedData := []byte(`
|
unformattedData := `
|
||||||
// starts resources to provision them.
|
// starts resources to provision them.
|
||||||
build {
|
build {
|
||||||
sources = [
|
sources = [
|
||||||
|
@ -197,9 +189,9 @@ build {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`)
|
`
|
||||||
|
|
||||||
formattedData := []byte(`
|
formattedData := `
|
||||||
// starts resources to provision them.
|
// starts resources to provision them.
|
||||||
build {
|
build {
|
||||||
sources = [
|
sources = [
|
||||||
|
@ -348,90 +340,131 @@ build {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`)
|
`
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
recursive bool
|
||||||
|
alreadyPresentContent map[string]string
|
||||||
|
expectedContent map[string]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "nested formats recursively",
|
||||||
|
recursive: true,
|
||||||
|
alreadyPresentContent: map[string]string{
|
||||||
|
"foo/bar/baz": unformattedData,
|
||||||
|
"foo/bar/baz/woo": unformattedData,
|
||||||
|
"": unformattedData,
|
||||||
|
},
|
||||||
|
expectedContent: map[string]string{
|
||||||
|
"foo/bar/baz": formattedData,
|
||||||
|
"foo/bar/baz/woo": formattedData,
|
||||||
|
"": formattedData,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nested no recursive format",
|
||||||
|
recursive: false,
|
||||||
|
alreadyPresentContent: map[string]string{
|
||||||
|
"foo/bar/baz": unformattedData,
|
||||||
|
"foo/bar/baz/woo": unformattedData,
|
||||||
|
"": unformattedData,
|
||||||
|
},
|
||||||
|
expectedContent: map[string]string{
|
||||||
|
"foo/bar/baz": unformattedData,
|
||||||
|
"foo/bar/baz/woo": unformattedData,
|
||||||
|
"": formattedData,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
f := NewHCL2Formatter()
|
f := NewHCL2Formatter()
|
||||||
f.Output = &buf
|
f.Output = &buf
|
||||||
f.Write = true
|
f.Write = true
|
||||||
|
|
||||||
recursiveTestCases := []FormatterRecursiveTestCase{
|
testFileName := "test.pkrvars.hcl"
|
||||||
{
|
|
||||||
TestCaseName: "With Recursive flag on",
|
for _, tt := range tests {
|
||||||
Recursion: true,
|
topDir, err := ioutil.TempDir("testdata/format", "temp-dir")
|
||||||
TopLevelFilePreFormat: unformattedData,
|
if err != nil {
|
||||||
LowerLevelFilePreFormat: unformattedData,
|
t.Fatalf("Failed to create TopDir for test case: %s, error: %v", tt.name, err)
|
||||||
TopLevelFilePostFormat: formattedData,
|
}
|
||||||
LowerLevelFilePostFormat: formattedData,
|
|
||||||
},
|
for testDir, content := range tt.alreadyPresentContent {
|
||||||
{
|
dir := filepath.Join(topDir, testDir)
|
||||||
TestCaseName: "With Recursive flag off",
|
err := os.MkdirAll(dir, 0777)
|
||||||
Recursion: false,
|
if err != nil {
|
||||||
TopLevelFilePreFormat: unformattedData,
|
os.RemoveAll(topDir)
|
||||||
LowerLevelFilePreFormat: unformattedData,
|
t.Fatalf(
|
||||||
TopLevelFilePostFormat: formattedData,
|
"Failed to create subDir: %s\n\n, for test case: %s\n\n, error: %v",
|
||||||
LowerLevelFilePostFormat: unformattedData,
|
testDir,
|
||||||
},
|
tt.name,
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Create(filepath.Join(dir, testFileName))
|
||||||
|
if err != nil {
|
||||||
|
os.RemoveAll(topDir)
|
||||||
|
t.Fatalf("failed to create testfile at directory: %s\n\n, for test case: %s\n\n, error: %s",
|
||||||
|
testDir,
|
||||||
|
tt.name,
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = file.Write([]byte(content))
|
||||||
|
if err != nil {
|
||||||
|
os.RemoveAll(topDir)
|
||||||
|
t.Fatalf("failed to write to testfile at directory: %s\n\n, for test case: %s\n\n, error: %s",
|
||||||
|
testDir,
|
||||||
|
tt.name,
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = file.Close()
|
||||||
|
if err != nil {
|
||||||
|
os.RemoveAll(topDir)
|
||||||
|
t.Fatalf("failed to close testfile at directory: %s\n\n, for test case: %s\n\n, error: %s",
|
||||||
|
testDir,
|
||||||
|
tt.name,
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Recursive = tt.recursive
|
||||||
|
_, diags := f.Format(topDir)
|
||||||
|
if diags.HasErrors() {
|
||||||
|
os.RemoveAll(topDir)
|
||||||
|
t.Fatalf("the call to Format failed unexpectedly for test case: %s, errors: %s", tt.name, diags.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
for expectedPath, expectedContent := range tt.expectedContent {
|
||||||
|
b, err := ioutil.ReadFile(filepath.Join(topDir, expectedPath, testFileName))
|
||||||
|
if err != nil {
|
||||||
|
os.RemoveAll(topDir)
|
||||||
|
t.Fatalf("ReadFile failed for test case: %s, error : %v", tt.name, err)
|
||||||
|
}
|
||||||
|
got := string(b)
|
||||||
|
if diff := cmp.Diff(got, expectedContent); diff != "" {
|
||||||
|
os.RemoveAll(topDir)
|
||||||
|
t.Errorf(
|
||||||
|
"format dir, unexpected result for test case: %s, path: %s, Expected: %s, Got: %s",
|
||||||
|
tt.name,
|
||||||
|
expectedPath,
|
||||||
|
expectedContent,
|
||||||
|
got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.RemoveAll(topDir)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf(
|
||||||
|
"Failed to delete top level test directory for test case: %s, please clean before another test run. Error: %s",
|
||||||
|
tt.name,
|
||||||
|
err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range recursiveTestCases {
|
|
||||||
executeRecursiveTestCase(t, tc, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func executeRecursiveTestCase(t *testing.T, tc FormatterRecursiveTestCase, f *HCL2Formatter) {
|
|
||||||
f.Recursive = tc.Recursion
|
|
||||||
|
|
||||||
var topDir string
|
|
||||||
topDir, err := ioutil.TempDir("testdata/format", "top-dir")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create sub level recurisve directory for test case: %s, errors: %s", tc.TestCaseName, err)
|
|
||||||
}
|
|
||||||
defer os.Remove(topDir)
|
|
||||||
|
|
||||||
var subDir string
|
|
||||||
subDir, err = ioutil.TempDir(topDir, "sub-dir")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create sub level recurisve directory for test case: %s, errors: %s", tc.TestCaseName, err)
|
|
||||||
}
|
|
||||||
defer os.Remove(subDir)
|
|
||||||
|
|
||||||
topTempFile, err := ioutil.TempFile(topDir, "*.pkr.hcl")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create top level tempfile for test case: %s, errors: %s", tc.TestCaseName, err)
|
|
||||||
}
|
|
||||||
defer os.Remove(topTempFile.Name())
|
|
||||||
|
|
||||||
_, _ = topTempFile.Write(tc.TopLevelFilePreFormat)
|
|
||||||
topTempFile.Close()
|
|
||||||
|
|
||||||
subTempFile, err := ioutil.TempFile(subDir, "*.pkr.hcl")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create sub level tempfile for test case: %s, errors: %s", tc.TestCaseName, err)
|
|
||||||
}
|
|
||||||
defer os.Remove(subTempFile.Name())
|
|
||||||
|
|
||||||
_, _ = subTempFile.Write(tc.LowerLevelFilePreFormat)
|
|
||||||
subTempFile.Close()
|
|
||||||
|
|
||||||
_, diags := f.Format(topDir)
|
|
||||||
if diags.HasErrors() {
|
|
||||||
t.Fatalf("the call to Format failed unexpectedly for test case: %s, errors: %s", tc.TestCaseName, diags.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
validateFileIsFormatted(t, tc.TopLevelFilePostFormat, topTempFile, tc.TestCaseName)
|
|
||||||
validateFileIsFormatted(t, tc.LowerLevelFilePostFormat, subTempFile, tc.TestCaseName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateFileIsFormatted(t *testing.T, formattedData []byte, testFile *os.File, testCaseName string) {
|
|
||||||
data, err := ioutil.ReadFile(testFile.Name())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to open the newly formatted fixture for test case: %s, errors: %s", testCaseName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := cmp.Diff(string(data), string(formattedData)); diff != "" {
|
|
||||||
t.Errorf("Unexpected format tfData output for test case: %s, errors: %s", testCaseName, diff)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHCL2Formatter_Format_Write(t *testing.T) {
|
func TestHCL2Formatter_Format_Write(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue