mirror of
https://github.com/hashicorp/packer.git
synced 2026-02-24 10:21:20 -05:00
In order to test the output from a test command, the commandOutput function exists in the command_test.go file. Since its behaviour is linked to the test meta that we produce in the test_utils.go file, we move it there, and rename it to `GetStdoutAndErrFromTestMeta' to make it clearer what to expect from it.
50 lines
992 B
Go
50 lines
992 B
Go
package command
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
"github.com/hashicorp/packer/packer"
|
|
)
|
|
|
|
const fixturesDir = "./test-fixtures"
|
|
|
|
func fatalCommand(t *testing.T, m Meta) {
|
|
ui := m.Ui.(*packersdk.BasicUi)
|
|
out := ui.Writer.(*bytes.Buffer)
|
|
err := ui.ErrorWriter.(*bytes.Buffer)
|
|
t.Fatalf(
|
|
"Bad exit code.\n\nStdout:\n\n%s\n\nStderr:\n\n%s",
|
|
out.String(),
|
|
err.String())
|
|
}
|
|
|
|
func testFixtureContent(n ...string) string {
|
|
path := filepath.Join(append([]string{fixturesDir}, n...)...)
|
|
b, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func testFixture(n ...string) string {
|
|
paths := []string{fixturesDir}
|
|
paths = append(paths, n...)
|
|
return filepath.Join(paths...)
|
|
}
|
|
|
|
func testMeta(t *testing.T) Meta {
|
|
var out, err bytes.Buffer
|
|
|
|
return Meta{
|
|
CoreConfig: packer.TestCoreConfig(t),
|
|
Ui: &packersdk.BasicUi{
|
|
Writer: &out,
|
|
ErrorWriter: &err,
|
|
},
|
|
}
|
|
}
|