From 3289540c902148a9f285aa182bfff92a990184dc Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 28 Mar 2025 10:26:50 -0400 Subject: [PATCH] packer_test: add `Dump' function to packerCommand Previous versions of the packerCommand implementation for wrapping a freshly compiled Packer binary to run tests with were a bit opaque, and printing out the logs from a run was defined in `Assert` as a plain checker, using a bit of a clumsy call that repeats already known information like `testing.T`. This commit changes the implementation so that `Dump` becomes a function call akin to the others, which doesn't need any parameter, instead it leverages what if already kept within the packerCommand structure. --- packer_test/common/commands.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packer_test/common/commands.go b/packer_test/common/commands.go index 869a6b278..c0c043674 100644 --- a/packer_test/common/commands.go +++ b/packer_test/common/commands.go @@ -22,6 +22,7 @@ type packerCommand struct { err error t *testing.T fatalfAssert bool + dump bool } // PackerCommand creates a skeleton of packer command with the ability to execute gadgets on the outputs of the command. @@ -125,6 +126,15 @@ func (pc *packerCommand) SetAssertFatal() *packerCommand { return pc } +// Dump enables a verbose mode for the test, when Assert is called, the test +// proceeds normally, and the command-line that was invoked, along with the +// contents of stdout/stderr will also be output in addition to the test +// results. This is mostly useful for debugging a test. +func (pc *packerCommand) Dump() *packerCommand { + pc.dump = true + return pc +} + // Run executes the packer command with the args/env requested and returns the // output streams (stdout, stderr) // @@ -182,6 +192,14 @@ func (pc *packerCommand) Output() (string, string, error) { } func (pc *packerCommand) Assert(checks ...check.Checker) { + if pc.dump { + tmpChecks := []check.Checker{ + check.Dump(pc.t), + } + + checks = append(tmpChecks, checks...) + } + attempt := 0 for pc.runs > 0 { attempt++