From 13c52124de5c2ecc2427bb3c46292d919e710a0f Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 17 Jun 2024 12:19:35 -0400 Subject: [PATCH] packer_test: check for a panic during execution When a command is run, it is the expectation that no test should make Packer panic. If it did, something is wrong and Packer should be fixed so it doesn't panic anymore in that situation. The way we did the check before was adding a PanicCheck after the command ran, so we could make sure of that during `Assert`. However, since we introduced the possibility to have multiple runs, having this addition as part of the run loop meant that the PanicCheck would be run as many times as there were runs. While this worked, this implied that we'd do the same check multiple times on a single command output, which is not optimal. Instead, this commit moves the check to within the `Run` function, this way for each run of the command we do the check once, and then we can assert the results of the command on what output it produced. --- packer_test/commands_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packer_test/commands_test.go b/packer_test/commands_test.go index fa6520996..b8d7bc584 100644 --- a/packer_test/commands_test.go +++ b/packer_test/commands_test.go @@ -138,6 +138,12 @@ func (pc *packerCommand) Run() (string, string, error) { pc.err = cmd.Run() + // Check that the command didn't panic, and if it did, we can immediately error + panicErr := PanicCheck{}.Check(pc.stdout.String(), pc.stderr.String(), pc.err) + if panicErr != nil { + pc.t.Fatalf("Packer panicked during execution: %s", panicErr) + } + return pc.stdout.String(), pc.stderr.String(), pc.err } @@ -147,8 +153,6 @@ func (pc *packerCommand) Assert(checks ...Checker) { attempt++ stdout, stderr, err := pc.Run() - checks = append(checks, PanicCheck{}) - for _, check := range checks { checkErr := check.Check(stdout, stderr, err) if checkErr != nil {