diff --git a/internal/testcontext/compile.go b/internal/testcontext/compile.go new file mode 100644 index 000000000..70b552145 --- /dev/null +++ b/internal/testcontext/compile.go @@ -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 +} diff --git a/internal/testcontext/compile_norace.go b/internal/testcontext/compile_norace.go new file mode 100644 index 000000000..ca833f8d5 --- /dev/null +++ b/internal/testcontext/compile_norace.go @@ -0,0 +1,8 @@ +// Copyright (C) 2019 Storj Labs, Inc. +// See LICENSE for copying information. + +// +build !race + +package testcontext + +const raceEnabled = false diff --git a/internal/testcontext/compile_race.go b/internal/testcontext/compile_race.go new file mode 100644 index 000000000..97fea6f76 --- /dev/null +++ b/internal/testcontext/compile_race.go @@ -0,0 +1,8 @@ +// Copyright (C) 2019 Storj Labs, Inc. +// See LICENSE for copying information. + +// +build race + +package testcontext + +const raceEnabled = true diff --git a/internal/testcontext/compile_test.go b/internal/testcontext/compile_test.go new file mode 100644 index 000000000..6cd542284 --- /dev/null +++ b/internal/testcontext/compile_test.go @@ -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) +}