From 8f7efe0d57fb7fad2b11e13d71380b72e9e16bdf Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 7 May 2024 10:58:43 -0400 Subject: [PATCH] test: add SetWD function to packerCommand For some tests we may need Packer to run in another directory than the one we're invoking the tests from, so we add a new function to the packerCommand structure to change that. --- test/commands_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/commands_test.go b/test/commands_test.go index 0f54384be..56d2d3345 100644 --- a/test/commands_test.go +++ b/test/commands_test.go @@ -15,6 +15,7 @@ type packerCommand struct { env map[string]string stderr *strings.Builder stdout *strings.Builder + workdir string err error } @@ -42,6 +43,12 @@ func (pc *packerCommand) NoVerbose() *packerCommand { return pc } +// SetWD changes the directory Packer is invoked from +func (pc *packerCommand) SetWD(dir string) *packerCommand { + pc.workdir = dir + return pc +} + // UsePluginDir sets the plugin directory in the environment to `dir` func (pc *packerCommand) UsePluginDir(dir string) *packerCommand { return pc.AddEnv("PACKER_PLUGIN_PATH", dir) @@ -76,6 +83,10 @@ func (pc *packerCommand) doRun() { cmd.Stdout = pc.stdout cmd.Stderr = pc.stderr + if pc.workdir != "" { + cmd.Dir = pc.workdir + } + pc.err = cmd.Run() }