simplify fmt test case a little
This commit is contained in:
parent
4e22147909
commit
a115b428ac
|
@ -389,7 +389,7 @@ func TestBuild(t *testing.T) {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
defer tt.cleanup(t)
|
defer tt.cleanup(t)
|
||||||
run(t, tt.args, tt.expectedCode)
|
run(t, tt.args, tt.expectedCode)
|
||||||
tt.fileCheck.verify(t)
|
tt.fileCheck.verify(t, "")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -732,7 +732,7 @@ func TestHCL2PostProcessorForceFlag(t *testing.T) {
|
||||||
if code := c.Run(args); code != 0 {
|
if code := c.Run(args); code != 0 {
|
||||||
fatalCommand(t, c.Meta)
|
fatalCommand(t, c.Meta)
|
||||||
}
|
}
|
||||||
fCheck.verify(t)
|
fCheck.verify(t, "")
|
||||||
|
|
||||||
// Second build should override previous manifest
|
// Second build should override previous manifest
|
||||||
UUID, _ = uuid.GenerateUUID()
|
UUID, _ = uuid.GenerateUUID()
|
||||||
|
@ -766,7 +766,7 @@ func TestHCL2PostProcessorForceFlag(t *testing.T) {
|
||||||
if code := c.Run(args); code != 0 {
|
if code := c.Run(args); code != 0 {
|
||||||
fatalCommand(t, c.Meta)
|
fatalCommand(t, c.Meta)
|
||||||
}
|
}
|
||||||
fCheck.verify(t)
|
fCheck.verify(t, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildCommand_HCLOnlyExceptOptions(t *testing.T) {
|
func TestBuildCommand_HCLOnlyExceptOptions(t *testing.T) {
|
||||||
|
@ -887,19 +887,19 @@ func (fc fileCheck) expectedFiles() []string {
|
||||||
return expected
|
return expected
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fc fileCheck) verify(t *testing.T) {
|
func (fc fileCheck) verify(t *testing.T, dir string) {
|
||||||
for _, f := range fc.expectedFiles() {
|
for _, f := range fc.expectedFiles() {
|
||||||
if !fileExists(f) {
|
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
|
||||||
t.Errorf("Expected to find %s", f)
|
t.Errorf("Expected to find %s: %v", f, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, f := range fc.notExpected {
|
for _, f := range fc.notExpected {
|
||||||
if fileExists(f) {
|
if _, err := os.Stat(filepath.Join(dir, f)); err == nil {
|
||||||
t.Errorf("Expected to not find %s", f)
|
t.Errorf("Expected to not find %s", f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for file, expectedContent := range fc.expectedContent {
|
for file, expectedContent := range fc.expectedContent {
|
||||||
content, err := ioutil.ReadFile(file)
|
content, err := ioutil.ReadFile(filepath.Join(dir, file))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("ioutil.ReadFile: %v", err)
|
t.Fatalf("ioutil.ReadFile: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -909,14 +909,6 @@ func (fc fileCheck) verify(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fileExists returns true if the filename is found
|
|
||||||
func fileExists(filename string) bool {
|
|
||||||
if _, err := os.Stat(filename); err == nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// testCoreConfigBuilder creates a packer CoreConfig that has a file builder
|
// testCoreConfigBuilder creates a packer CoreConfig that has a file builder
|
||||||
// available. This allows us to test a builder that writes files to disk.
|
// available. This allows us to test a builder that writes files to disk.
|
||||||
func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {
|
func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
@ -56,12 +55,14 @@ func TestFmt_unfomattedTemlateDirectory(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFmt_Recursive(t *testing.T) {
|
func TestFmt_Recursive(t *testing.T) {
|
||||||
unformattedData := `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 := `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"]
|
||||||
|
|
||||||
`
|
`
|
||||||
|
@ -70,35 +71,41 @@ ami_filter_owners = ["137112412989"]
|
||||||
name string
|
name string
|
||||||
formatArgs []string // arguments passed to format
|
formatArgs []string // arguments passed to format
|
||||||
alreadyPresentContent map[string]string
|
alreadyPresentContent map[string]string
|
||||||
expectedContent map[string]string
|
fileCheck
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
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": unformattedData,
|
"foo/bar/baz.pkr.hcl": unformattedData,
|
||||||
"foo/bar/baz/woo": unformattedData,
|
"foo/bar/baz/woo.pkrvars.hcl": unformattedData,
|
||||||
"": unformattedData,
|
"potato": unformattedData,
|
||||||
},
|
"foo/bar/potato": unformattedData,
|
||||||
expectedContent: map[string]string{
|
"bar.pkr.hcl": unformattedData,
|
||||||
"foo/bar/baz": formattedData,
|
|
||||||
"foo/bar/baz/woo": formattedData,
|
|
||||||
"": formattedData,
|
|
||||||
},
|
},
|
||||||
|
fileCheck: fileCheck{
|
||||||
|
expectedContent: map[string]string{
|
||||||
|
"foo/bar/baz.pkr.hcl": formattedData,
|
||||||
|
"foo/bar/baz/woo.pkrvars.hcl": formattedData,
|
||||||
|
"potato": unformattedData,
|
||||||
|
"foo/bar/potato": unformattedData,
|
||||||
|
"bar.pkr.hcl": formattedData,
|
||||||
|
}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
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": unformattedData,
|
"foo/bar/baz.pkr.hcl": unformattedData,
|
||||||
"foo/bar/baz/woo": unformattedData,
|
"foo/bar/baz/woo.pkrvars.hcl": unformattedData,
|
||||||
"": unformattedData,
|
"bar.pkr.hcl": unformattedData,
|
||||||
},
|
|
||||||
expectedContent: map[string]string{
|
|
||||||
"foo/bar/baz": unformattedData,
|
|
||||||
"foo/bar/baz/woo": unformattedData,
|
|
||||||
"": formattedData,
|
|
||||||
},
|
},
|
||||||
|
fileCheck: fileCheck{
|
||||||
|
expectedContent: map[string]string{
|
||||||
|
"foo/bar/baz.pkr.hcl": unformattedData,
|
||||||
|
"foo/bar/baz/woo.pkrvars.hcl": unformattedData,
|
||||||
|
"bar.pkr.hcl": formattedData,
|
||||||
|
}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,62 +116,25 @@ ami_filter_owners = ["137112412989"]
|
||||||
testDir := "test-fixtures/fmt"
|
testDir := "test-fixtures/fmt"
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
tempFileNames := make(map[string]string)
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
tempDirectory := mustString(ioutil.TempDir(testDir, "test-dir-*"))
|
||||||
|
defer os.RemoveAll(tempDirectory)
|
||||||
|
|
||||||
tempDirectory, err := ioutil.TempDir(testDir, "test-dir-*")
|
createFiles(tempDirectory, tt.alreadyPresentContent)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create temp dir for test case: %s, error: %v", tt.name, err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDirectory)
|
|
||||||
|
|
||||||
for subDir, content := range tt.alreadyPresentContent {
|
testArgs := append(tt.formatArgs, tempDirectory)
|
||||||
dir := filepath.Join(tempDirectory, subDir)
|
if code := c.Run(testArgs); code != 0 {
|
||||||
err = os.MkdirAll(dir, 0700)
|
ui := c.Meta.Ui.(*packersdk.BasicUi)
|
||||||
if err != nil {
|
out := ui.Writer.(*bytes.Buffer)
|
||||||
t.Fatalf("Failed to create directory for test case: %s, error: %v", tt.name, err)
|
err := ui.ErrorWriter.(*bytes.Buffer)
|
||||||
}
|
t.Fatalf(
|
||||||
|
"Bad exit code for test case: %s.\n\nStdout:\n\n%s\n\nStderr:\n\n%s",
|
||||||
tempFile, err := ioutil.TempFile(dir, "*.pkrvars.hcl")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create temp file for test case: %s, error: %v", tt.name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tempFile.Write([]byte(content))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to write temp file for test case: %s, error: %v", tt.name, err)
|
|
||||||
}
|
|
||||||
tempFileNames[subDir] = tempFile.Name()
|
|
||||||
tempFile.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
testArgs := append(tt.formatArgs, tempDirectory)
|
|
||||||
if code := c.Run(testArgs); code != 0 {
|
|
||||||
os.RemoveAll(tempDirectory)
|
|
||||||
ui := c.Meta.Ui.(*packersdk.BasicUi)
|
|
||||||
out := ui.Writer.(*bytes.Buffer)
|
|
||||||
err := ui.ErrorWriter.(*bytes.Buffer)
|
|
||||||
t.Fatalf(
|
|
||||||
"Bad exit code for test case: %s.\n\nStdout:\n\n%s\n\nStderr:\n\n%s",
|
|
||||||
tt.name,
|
|
||||||
out.String(),
|
|
||||||
err.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
for expectedPath, expectedContent := range tt.expectedContent {
|
|
||||||
b, err := ioutil.ReadFile(tempFileNames[expectedPath])
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("ReadFile failed for test case: %s, error : %v", tt.name, err)
|
|
||||||
}
|
|
||||||
got := string(b)
|
|
||||||
if diff := cmp.Diff(got, expectedContent); diff != "" {
|
|
||||||
t.Errorf(
|
|
||||||
"format dir, unexpected result for test case: %s, path: %s, Expected: %s, Got: %s",
|
|
||||||
tt.name,
|
tt.name,
|
||||||
expectedPath,
|
out.String(),
|
||||||
expectedContent,
|
err.String())
|
||||||
got)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
tt.fileCheck.verify(t, tempDirectory)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,3 +39,11 @@ func (c *configDirSingleton) dir(key string) string {
|
||||||
c.dirs[key] = mustString(ioutil.TempDir("", "pkr-test-cfg-dir-"+key))
|
c.dirs[key] = mustString(ioutil.TempDir("", "pkr-test-cfg-dir-"+key))
|
||||||
return c.dirs[key]
|
return c.dirs[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fileExists returns true if the filename is found
|
||||||
|
func fileExists(filename string) bool {
|
||||||
|
if _, err := os.Stat(filename); err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue