From 99248cdcc27d5f715ec641717a9ab12c5c39b06e Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 10 May 2024 14:48:10 -0400 Subject: [PATCH] packer_test: remove testing.T arg on Assert Since the command object is created using the TestSuite as argument, we can keep a reference to the test suite's scoped T() instance for the command's lifecycle, and reuse it later during `Assert`, instead of needing to pass it as argument when invoking the function. --- packer_test/commands_test.go | 6 ++++-- packer_test/install_test.go | 12 ++++++------ packer_test/loading_test.go | 16 ++++++++-------- packer_test/plugin_test.go | 2 +- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packer_test/commands_test.go b/packer_test/commands_test.go index 60f2003c3..a49f289d8 100644 --- a/packer_test/commands_test.go +++ b/packer_test/commands_test.go @@ -17,6 +17,7 @@ type packerCommand struct { stdout *strings.Builder workdir string err error + t *testing.T } // PackerCommand creates a skeleton of packer command with the ability to execute gadgets on the outputs of the command. @@ -31,6 +32,7 @@ func (ts *PackerTestSuite) PackerCommand() *packerCommand { }, stderr: stderr, stdout: stdout, + t: ts.T(), } } @@ -90,7 +92,7 @@ func (pc *packerCommand) doRun() { pc.err = cmd.Run() } -func (pc *packerCommand) Assert(t *testing.T, checks ...Checker) { +func (pc *packerCommand) Assert(checks ...Checker) { stdout, stderr, err := pc.Run() checks = append(checks, PanicCheck{}) @@ -99,7 +101,7 @@ func (pc *packerCommand) Assert(t *testing.T, checks ...Checker) { checkErr := check.Check(stdout, stderr, err) if checkErr != nil { checkerName := InferName(check) - t.Errorf("check %q failed: %s", checkerName, checkErr) + pc.t.Errorf("check %q failed: %s", checkerName, checkErr) } } } diff --git a/packer_test/install_test.go b/packer_test/install_test.go index 31ccb436b..b063ed2a5 100644 --- a/packer_test/install_test.go +++ b/packer_test/install_test.go @@ -7,19 +7,19 @@ func (ts *PackerTestSuite) TestInstallPluginWithMetadata() { ts.Run("metadata plugin installed must not have metadata in its path", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). SetArgs("plugins", "installed"). - Assert(ts.T(), MustSucceed(), Grep("packer-plugin-tester_v1.0.0[^+]", grepStdout)) + Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.0[^+]", grepStdout)) }) ts.Run("plugin with metadata should work with validate", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). SetArgs("validate", "./templates/simple.pkr.hcl"). - Assert(ts.T(), MustSucceed(), Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", grepStderr)) + Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", grepStderr)) }) ts.Run("plugin with metadata should work with build", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). SetArgs("build", "./templates/simple.pkr.hcl"). - Assert(ts.T(), MustSucceed(), Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", grepStderr)) + Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", grepStderr)) }) } @@ -32,7 +32,7 @@ func (ts *PackerTestSuite) TestInstallPluginPrerelease() { ts.Run("try install plugin with alpha1 prerelease - should fail", func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("plugins", "install", "--path", pluginPath, "github.com/hashicorp/tester"). - Assert(ts.T(), MustFail(), Grep("Packer can only install plugin releases with this command", grepStdout)) + Assert(MustFail(), Grep("Packer can only install plugin releases with this command", grepStdout)) }) } @@ -43,12 +43,12 @@ func (ts *PackerTestSuite) TestRemoteInstallWithPluginsInstall() { ts.Run("install latest version of a remote plugin with packer plugins install", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "install", "github.com/hashicorp/hashicups"). - Assert(ts.T(), MustSucceed()) + Assert(MustSucceed()) }) ts.Run("install dev version of a remote plugin with packer plugins install - must fail", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "install", "github.com/hashicorp/hashicups", "v1.0.2-dev"). - Assert(ts.T(), MustFail(), Grep("Remote installation of pre-release plugin versions is unsupported.", grepStdout)) + Assert(MustFail(), Grep("Remote installation of pre-release plugin versions is unsupported.", grepStdout)) }) } diff --git a/packer_test/loading_test.go b/packer_test/loading_test.go index b8def2454..97fa6d8a7 100644 --- a/packer_test/loading_test.go +++ b/packer_test/loading_test.go @@ -41,7 +41,7 @@ func (ts *PackerTestSuite) TestLoadingOrder() { ts.PackerCommand(). SetArgs(command, tt.templatePath). UsePluginDir(pluginDir). - Assert(ts.T(), MustSucceed(), Grep(tt.grepStr)) + Assert(MustSucceed(), Grep(tt.grepStr)) }) } } @@ -59,7 +59,7 @@ func (ts *PackerTestSuite) TestLoadWithLegacyPluginName() { ts.Run("with required_plugins - expect prompt for packer init", func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("build", "templates/simple.pkr.hcl"). - Assert(ts.T(), MustFail(), + Assert(MustFail(), Grep("Did you run packer init for this project", grepStdout), Grep("following plugins are required", grepStdout)) }) @@ -67,7 +67,7 @@ func (ts *PackerTestSuite) TestLoadWithLegacyPluginName() { ts.Run("JSON template, without required_plugins: should say the component is unknown", func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("build", "templates/simple.json"). - Assert(ts.T(), MustFail(), + Assert(MustFail(), Grep("The builder tester-dynamic is unknown by Packer", grepStdout)) }) }) @@ -80,7 +80,7 @@ func (ts *PackerTestSuite) TestLoadWithLegacyPluginName() { ts.Run("multiple plugins installed: one with no version in path, one with qualified name. Should pick-up the qualified one only.", func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("build", "templates/simple.pkr.hcl"). - Assert(ts.T(), MustSucceed(), Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", grepStderr)) + Assert(MustSucceed(), Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", grepStderr)) }) wd, cleanup := TempWorkdir(ts.T(), "./templates/simple.pkr.hcl") @@ -91,7 +91,7 @@ func (ts *PackerTestSuite) TestLoadWithLegacyPluginName() { ts.Run("multiple plugins installed: 1.0.0 in plugin dir with sum, one in workdir (no version). Should load 1.0.0", func() { ts.PackerCommand().UsePluginDir(pluginDir).SetWD(wd). SetArgs("build", "simple.pkr.hcl"). - Assert(ts.T(), MustSucceed(), Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", grepStderr)) + Assert(MustSucceed(), Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", grepStderr)) }) } @@ -108,7 +108,7 @@ func (ts *PackerTestSuite) TestLoadWithSHAMismatches() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("plugins", "installed"). - Assert(ts.T(), MustSucceed(), + Assert(MustSucceed(), Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", grepStdout), Grep("packer-plugin-tester_v1.0.10", grepStdout, grepInvert), Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", grepStderr)) @@ -131,7 +131,7 @@ func (ts *PackerTestSuite) TestLoadWithSHAMismatches() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("plugins", "installed"). - Assert(ts.T(), MustSucceed(), + Assert(MustSucceed(), Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", grepStdout), Grep("packer-plugin-tester_v1.0.10", grepInvert, grepStdout), Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", grepStderr), @@ -150,7 +150,7 @@ func (ts *PackerTestSuite) TestPluginPathEnvvarWithMultiplePaths() { ts.Run("load plugin with two dirs - not supported anymore, should error", func() { ts.PackerCommand().UsePluginDir(pluginDirVal). SetArgs("plugins", "installed"). - Assert(ts.T(), MustFail(), + Assert(MustFail(), Grep("Multiple paths are no longer supported for PACKER_PLUGIN_PATH"), MkPipeCheck("All envvars are suggested", PipeGrep(`\* PACKER_PLUGIN_PATH=`), diff --git a/packer_test/plugin_test.go b/packer_test/plugin_test.go index 2a618c201..58d9435b2 100644 --- a/packer_test/plugin_test.go +++ b/packer_test/plugin_test.go @@ -193,7 +193,7 @@ func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDi err = fmt.Errorf("failed to get path to version %q, was it compiled?", pluginVersion) } cmd := ts.PackerCommand().SetArgs("plugins", "install", "--path", path, "github.com/hashicorp/tester").AddEnv("PACKER_PLUGIN_PATH", pluginTempDir) - cmd.Assert(t, MustSucceed()) + cmd.Assert(MustSucceed()) out, stderr, cmdErr := cmd.Run() if cmdErr != nil { err = fmt.Errorf("failed to install tester plugin version %q: %s\nCommand stdout: %s\nCommand stderr: %s", pluginVersion, err, out, stderr)