cleanup builder, support older go versions

Этот коммит содержится в:
gedi 2016-06-14 17:03:57 +03:00
родитель 4610466f82
коммит f43583e36e

Просмотреть файл

@ -12,6 +12,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"text/template" "text/template"
"time"
"unicode" "unicode"
) )
@ -69,17 +70,25 @@ func Build() (string, error) {
return bin, fmt.Errorf("failed to compile package %s deps - %v, output - %s", pkg.Name, err, string(out)) return bin, fmt.Errorf("failed to compile package %s deps - %v, output - %s", pkg.Name, err, string(out))
} }
// let go do the dirty work and compile it. // let go do the dirty work and compile test
// package with it's dependencies. Older go
// versions does not accept existing file output
// so we create a temporary executable which will
// removed.
temp := fmt.Sprintf(filepath.Join("%s", "temp-%d.test"), os.TempDir(), time.Now().UnixNano())
// builds and compile the tested package. // builds and compile the tested package.
// test executable will be piped to /dev/null // generated test executable will be removed
// since we do not need it for godog suite // since we do not need it for godog suite.
// we also print back the temp WORK directory // we also print back the temp WORK directory
// go has build to test this package. We will // go has built. We will reuse it for our suite workdir.
// reuse it for our suite. out, err = exec.Command("go", "test", "-c", "-work", "-o", temp).CombinedOutput()
out, err = exec.Command("go", "test", "-c", "-work", "-o", os.DevNull).CombinedOutput()
if err != nil { if err != nil {
return bin, fmt.Errorf("failed to compile tested package %s - %v, output - %s", pkg.Name, err, string(out)) return bin, fmt.Errorf("failed to compile tested package %s - %v, output - %s", pkg.Name, err, string(out))
} }
defer os.Remove(temp)
// extract go-build temporary directory as our workdir
workdir := strings.TrimSpace(string(out)) workdir := strings.TrimSpace(string(out))
if !strings.HasPrefix(workdir, "WORK=") { if !strings.HasPrefix(workdir, "WORK=") {
return bin, fmt.Errorf("expected WORK dir path, but got: %s", workdir) return bin, fmt.Errorf("expected WORK dir path, but got: %s", workdir)
@ -88,7 +97,7 @@ func Build() (string, error) {
testdir := filepath.Join(workdir, pkg.ImportPath, "_test") testdir := filepath.Join(workdir, pkg.ImportPath, "_test")
defer os.RemoveAll(workdir) defer os.RemoveAll(workdir)
// replace testmain.go file with our own // replace _testmain.go file with our own
testmain := filepath.Join(testdir, "_testmain.go") testmain := filepath.Join(testdir, "_testmain.go")
err = ioutil.WriteFile(testmain, src, 0644) err = ioutil.WriteFile(testmain, src, 0644)
if err != nil { if err != nil {
@ -107,15 +116,16 @@ func Build() (string, error) {
return bin, err return bin, err
} }
var buf bytes.Buffer
pkgDir := filepath.Join(godogPkg.PkgRoot, build.Default.GOOS+"_"+build.Default.GOARCH) pkgDir := filepath.Join(godogPkg.PkgRoot, build.Default.GOOS+"_"+build.Default.GOARCH)
pkgDirs := []string{testdir, workdir, pkgDir} pkgDirs := []string{testdir, workdir, pkgDir}
// build godog testmain package archive
// compile godog testmain package archive
var buf bytes.Buffer
testMainPkgOut := filepath.Join(testdir, "main.a") testMainPkgOut := filepath.Join(testdir, "main.a")
args := []string{ args := []string{
"tool", "compile", "tool", "compile",
"-o", testMainPkgOut, "-o", testMainPkgOut,
"-trimpath", workdir, // "-trimpath", workdir,
"-p", "main", "-p", "main",
"-complete", "-complete",
} }
@ -128,7 +138,6 @@ func Build() (string, error) {
args = append(args, "-pack", testmain) args = append(args, "-pack", testmain)
cmd := exec.Command("go", args...) cmd := exec.Command("go", args...)
cmd.Env = os.Environ() cmd.Env = os.Environ()
// cmd.Dir = testdir
cmd.Stdout = &buf cmd.Stdout = &buf
cmd.Stderr = &buf cmd.Stderr = &buf
err = cmd.Run() err = cmd.Run()
@ -137,7 +146,7 @@ func Build() (string, error) {
return bin, fmt.Errorf("failed to compile testmain package %v, output - %s", err, buf.String()) return bin, fmt.Errorf("failed to compile testmain package %v, output - %s", err, buf.String())
} }
// build test suite executable // link test suite executable
args = []string{ args = []string{
"tool", "link", "tool", "link",
"-o", bin, "-o", bin,
@ -150,7 +159,6 @@ func Build() (string, error) {
args = append(args, testMainPkgOut) args = append(args, testMainPkgOut)
cmd = exec.Command("go", args...) cmd = exec.Command("go", args...)
cmd.Env = os.Environ() cmd.Env = os.Environ()
// cmd.Dir = testdir
out, err = cmd.CombinedOutput() out, err = cmd.CombinedOutput()
if err != nil { if err != nil {
return bin, fmt.Errorf("failed to compile testmain package %v, output - %s", err, string(out)) return bin, fmt.Errorf("failed to compile testmain package %v, output - %s", err, string(out))