github.com/hashicorp/packer@v1.14.3/packer_test/common/base.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "math/rand" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "runtime" 10 "testing" 11 ) 12 13 // BuildTestPacker builds a new Packer binary based on the current state of the repository. 14 // 15 // If for some reason the binary cannot be built, we will immediately exit with an error. 16 func BuildTestPacker(t *testing.T) (string, error) { 17 testDir, err := currentDir() 18 if err != nil { 19 return "", fmt.Errorf("failed to compile packer binary: %s", err) 20 } 21 22 packerCoreDir := filepath.Dir(filepath.Dir(testDir)) 23 24 outBin := filepath.Join(os.TempDir(), fmt.Sprintf("packer_core-%d", rand.Int())) 25 if runtime.GOOS == "windows" { 26 outBin = fmt.Sprintf("%s.exe", outBin) 27 } 28 29 compileCommand := exec.Command("go", "build", "-C", packerCoreDir, "-o", outBin) 30 logs, err := compileCommand.CombinedOutput() 31 if err != nil { 32 t.Fatalf("failed to compile Packer core: %s\ncompilation logs: %s", err, logs) 33 } 34 35 return outBin, nil 36 }