From 307cc640b62fa7c4c5fae6c57a255e315fbb43fa Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Wed, 27 Feb 2019 10:25:28 +0200 Subject: [PATCH] add testcontext.Compile (#1356) --- internal/testcontext/compile.go | 31 ++++++++++++++++++++++++++ internal/testcontext/compile_norace.go | 8 +++++++ internal/testcontext/compile_race.go | 8 +++++++ internal/testcontext/compile_test.go | 20 +++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 internal/testcontext/compile.go create mode 100644 internal/testcontext/compile_norace.go create mode 100644 internal/testcontext/compile_race.go create mode 100644 internal/testcontext/compile_test.go 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) +}