2024-08-15 10:05:27 -04:00
|
|
|
package common
|
2024-05-01 15:36:39 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path/filepath"
|
2024-05-15 17:33:08 -04:00
|
|
|
"runtime"
|
2024-05-01 15:36:39 -04:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// BuildTestPacker builds a new Packer binary based on the current state of the repository.
|
|
|
|
|
//
|
|
|
|
|
// If for some reason the binary cannot be built, we will immediately exit with an error.
|
|
|
|
|
func BuildTestPacker(t *testing.T) (string, error) {
|
|
|
|
|
testDir, err := currentDir()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to compile packer binary: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 15:38:21 -04:00
|
|
|
packerCoreDir := filepath.Dir(filepath.Dir(testDir))
|
2024-05-01 15:36:39 -04:00
|
|
|
|
|
|
|
|
outBin := filepath.Join(os.TempDir(), fmt.Sprintf("packer_core-%d", rand.Int()))
|
2024-05-15 17:33:08 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
outBin = fmt.Sprintf("%s.exe", outBin)
|
|
|
|
|
}
|
2024-05-01 15:36:39 -04:00
|
|
|
|
|
|
|
|
compileCommand := exec.Command("go", "build", "-C", packerCoreDir, "-o", outBin)
|
|
|
|
|
logs, err := compileCommand.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to compile Packer core: %s\ncompilation logs: %s", err, logs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return outBin, nil
|
|
|
|
|
}
|