From 3f6c0a5f23d7c0653409a9094675649b3f4ebfb8 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Thu, 22 Sep 2022 10:06:56 -0400 Subject: [PATCH] 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. --- command/command_test.go | 7 ------- command/test_utils.go | 8 ++++++++ command/validate_test.go | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/command/command_test.go b/command/command_test.go index d931d7708..a4505c611 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -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) diff --git a/command/test_utils.go b/command/test_utils.go index ab901bef9..9ad5f6111 100644 --- a/command/test_utils.go +++ b/command/test_utils.go @@ -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 { diff --git a/command/validate_test.go b/command/validate_test.go index 051b9c3ee..eb63d7306 100644 --- a/command/validate_test.go +++ b/command/validate_test.go @@ -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