command: move func to extract stdout/err in tests

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.
pull/12018/head
Lucas Bajolet 4 years ago committed by Lucas Bajolet
parent 6ee6091aa9
commit 3f6c0a5f23

@ -22,13 +22,6 @@ func fatalCommand(t *testing.T, m Meta) {
err.String())
}
func outputCommand(t *testing.T, m Meta) (string, string) {
ui := m.Ui.(*packersdk.BasicUi)
out := ui.Writer.(*bytes.Buffer)
err := ui.ErrorWriter.(*bytes.Buffer)
return out.String(), err.String()
}
func testFixtureContent(n ...string) string {
path := filepath.Join(append([]string{fixturesDir}, n...)...)
b, err := ioutil.ReadFile(path)

@ -32,6 +32,14 @@ func TestMetaFile(t *testing.T) Meta {
}
}
// GetStdoutAndErrFromTestMeta extracts stdout/stderr from a Meta created by TestMetaFile
func GetStdoutAndErrFromTestMeta(t *testing.T, m Meta) (string, string) {
ui := m.Ui.(*packersdk.BasicUi)
out := ui.Writer.(*bytes.Buffer)
err := ui.ErrorWriter.(*bytes.Buffer)
return out.String(), err.String()
}
// testCoreConfigBuilder creates a packer CoreConfig that has a file builder
// available. This allows us to test a builder that writes files to disk.
func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {

@ -137,7 +137,7 @@ func TestValidateCommandBadVersion(t *testing.T) {
t.Errorf("Expected exit code 1")
}
stdout, stderr := outputCommand(t, c.Meta)
stdout, stderr := GetStdoutAndErrFromTestMeta(t, c.Meta)
expected := `Error:
This template requires Packer version 101.0.0 or higher; using 100.0.0

Loading…
Cancel
Save