github.com/hashicorp/packer@v1.14.3/command/fix_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package command 5 6 import ( 7 "path/filepath" 8 "strings" 9 "testing" 10 11 packersdk "github.com/hashicorp/packer-plugin-sdk/packer" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestFix(t *testing.T) { 16 s := &strings.Builder{} 17 ui := &packersdk.BasicUi{ 18 Writer: s, 19 } 20 c := &FixCommand{ 21 Meta: testMeta(t), 22 } 23 24 c.Ui = ui 25 26 args := []string{filepath.Join(testFixture("fix"), "template.json")} 27 if code := c.Run(args); code != 0 { 28 fatalCommand(t, c.Meta) 29 } 30 expected := `{ 31 "builders": [ 32 { 33 "type": "dummy" 34 } 35 ], 36 "push": { 37 "name": "foo/bar" 38 } 39 }` 40 assert.Equal(t, expected, strings.TrimSpace(s.String())) 41 } 42 43 func TestFix_invalidTemplate(t *testing.T) { 44 c := &FixCommand{ 45 Meta: testMeta(t), 46 } 47 48 args := []string{filepath.Join(testFixture("fix-invalid"), "template.json")} 49 if code := c.Run(args); code != 1 { 50 fatalCommand(t, c.Meta) 51 } 52 } 53 54 func TestFix_invalidTemplateDisableValidation(t *testing.T) { 55 c := &FixCommand{ 56 Meta: testMeta(t), 57 } 58 59 args := []string{ 60 "-validate=false", 61 filepath.Join(testFixture("fix-invalid"), "template.json"), 62 } 63 if code := c.Run(args); code != 0 { 64 fatalCommand(t, c.Meta) 65 } 66 }