From 0e2695d3e2e1d6eedb74dcea951cef30bc47691f Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 3 May 2024 11:07:12 -0400 Subject: [PATCH] test: return cleanup function for temp plugin dirs When creating a temporary plugin directory, we had to build a cleanup function, which could be as simple as `os.Mkdirall` without any kind of warning that a directory failed to be cleaned-up, or we could do some more work in order to report the possible issues around this. That code would quickly be redundant, as there's not a ton of variability in the code that can be written for this step, so we abstract it through a pre-defined cancellation function which can be safely defer invoked. --- test/loading_test.go | 10 ++-------- test/plugin_test.go | 10 ++++++++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/loading_test.go b/test/loading_test.go index c12f019d2..258d8b5f5 100644 --- a/test/loading_test.go +++ b/test/loading_test.go @@ -1,20 +1,14 @@ package test import ( - "os" "testing" ) func (ts *PackerTestSuite) TestLoadingOrder() { t := ts.T() - pluginDir := ts.MakePluginDir(t, "1.0.9", "1.0.10") - defer func() { - err := os.RemoveAll(pluginDir) - if err != nil { - t.Logf("failed to remove temporary plugin directory %q: %s. This may need manual intervention.", pluginDir, err) - } - }() + pluginDir, cleanup := ts.MakePluginDir(t, "1.0.9", "1.0.10") + defer cleanup() for _, command := range []string{"build", "validate"} { tests := []struct { diff --git a/test/plugin_test.go b/test/plugin_test.go index 67af25c26..53d5193a6 100644 --- a/test/plugin_test.go +++ b/test/plugin_test.go @@ -2,6 +2,7 @@ package test import ( "fmt" + "io" "os" "os/exec" "path/filepath" @@ -160,7 +161,7 @@ func currentDir() (string, error) { // packer will be able to use that directory for running its functions. // // Deletion of the directory is the caller's responsibility. -func (ts *PackerTestSuite) MakePluginDir(t *testing.T, pluginVersions ...string) (pluginTempDir string) { +func (ts *PackerTestSuite) MakePluginDir(t *testing.T, pluginVersions ...string) (pluginTempDir string, cleanup func()) { var err error defer func() { @@ -188,5 +189,10 @@ func (ts *PackerTestSuite) MakePluginDir(t *testing.T, pluginVersions ...string) } } - return pluginTempDir + return pluginTempDir, func() { + err := os.RemoveAll(pluginTempDir) + if err != nil { + t.Logf("failed to remove temporary plugin directory %q: %s. This may need manual intervention.", pluginTempDir, err) + } + } }