2015-06-13 13:56:09 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2018-09-06 14:55:11 -04:00
|
|
|
"strings"
|
2015-06-13 13:56:09 -04:00
|
|
|
"testing"
|
2018-09-06 14:55:11 -04:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2018-09-06 14:55:11 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-06-13 13:56:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFix(t *testing.T) {
|
2018-09-06 14:55:11 -04:00
|
|
|
s := &strings.Builder{}
|
2020-11-20 13:21:29 -05:00
|
|
|
ui := &packersdk.BasicUi{
|
2018-09-06 14:55:11 -04:00
|
|
|
Writer: s,
|
|
|
|
}
|
2015-06-13 13:56:09 -04:00
|
|
|
c := &FixCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:55:11 -04:00
|
|
|
c.Ui = ui
|
|
|
|
|
2015-06-13 13:56:09 -04:00
|
|
|
args := []string{filepath.Join(testFixture("fix"), "template.json")}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
2018-09-06 14:55:11 -04:00
|
|
|
expected := `{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"type": "dummy"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"push": {
|
|
|
|
"name": "foo/bar"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
assert.Equal(t, expected, strings.TrimSpace(s.String()))
|
2015-06-13 13:56:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFix_invalidTemplate(t *testing.T) {
|
|
|
|
c := &FixCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{filepath.Join(testFixture("fix-invalid"), "template.json")}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFix_invalidTemplateDisableValidation(t *testing.T) {
|
|
|
|
c := &FixCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-validate=false",
|
|
|
|
filepath.Join(testFixture("fix-invalid"), "template.json"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
}
|