add testcontext.Compile (#1356)

This commit is contained in:
Egon Elbre 2019-02-27 10:25:28 +02:00 committed by Michal Niewrzal
parent fde0020c68
commit 307cc640b6
4 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,31 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package testcontext
import (
"os/exec"
"path"
)
// Compile compiles the specified package and returns the executable name.
func (ctx *Context) Compile(pkg string) string {
ctx.test.Helper()
exe := ctx.File("build", path.Base(pkg)+".exe")
var cmd *exec.Cmd
if raceEnabled {
cmd = exec.Command("go", "build", "-race", "-o", exe, pkg)
} else {
cmd = exec.Command("go", "build", "-o", exe, pkg)
}
out, err := cmd.CombinedOutput()
if err != nil {
ctx.test.Error(string(out))
ctx.test.Fatal(err)
}
return exe
}

View File

@ -0,0 +1,8 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
// +build !race
package testcontext
const raceEnabled = false

View File

@ -0,0 +1,8 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
// +build race
package testcontext
const raceEnabled = true

View File

@ -0,0 +1,20 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package testcontext_test
import (
"testing"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testcontext"
)
func TestCompile(t *testing.T) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
exe := ctx.Compile("storj.io/storj/examples/pointerdb-client")
assert.NotEmpty(t, exe)
}