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.
pull/12983/head
Lucas Bajolet 2 years ago
parent 56c2b5ad8b
commit 0e2695d3e2

@ -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 {

@ -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)
}
}
}

Loading…
Cancel
Save