github.com/hashicorp/packer@v1.14.3/command/command_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package command 5 6 import ( 7 "bytes" 8 "os" 9 "path/filepath" 10 "testing" 11 12 packersdk "github.com/hashicorp/packer-plugin-sdk/packer" 13 "github.com/hashicorp/packer/packer" 14 ) 15 16 const fixturesDir = "./test-fixtures" 17 18 func fatalCommand(t *testing.T, m Meta) { 19 ui := m.Ui.(*packersdk.BasicUi) 20 out := ui.Writer.(*bytes.Buffer) 21 err := ui.ErrorWriter.(*bytes.Buffer) 22 t.Fatalf( 23 "Bad exit code.\n\nStdout:\n\n%s\n\nStderr:\n\n%s", 24 out.String(), 25 err.String()) 26 } 27 28 func testFixtureContent(n ...string) string { 29 path := filepath.Join(append([]string{fixturesDir}, n...)...) 30 b, err := os.ReadFile(path) 31 if err != nil { 32 panic(err) 33 } 34 return string(b) 35 } 36 37 func testFixture(n ...string) string { 38 paths := []string{fixturesDir} 39 paths = append(paths, n...) 40 return filepath.Join(paths...) 41 } 42 43 func testMeta(t *testing.T) Meta { 44 var out, err bytes.Buffer 45 46 return Meta{ 47 CoreConfig: packer.TestCoreConfig(t), 48 Ui: &packersdk.BasicUi{ 49 Writer: &out, 50 ErrorWriter: &err, 51 }, 52 } 53 }