Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

cmd/gb: refuse to run if -race support missing #495

Merged
merged 1 commit into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions cmd/gb/gb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ func init() {
}
}

_, err := os.Stat(filepath.Join(runtime.GOROOT(), "pkg", fmt.Sprintf("%s_%s_race", runtime.GOOS, runtime.GOARCH)))
switch {
case os.IsNotExist(err):
log.Printf("go installation at %s is missing race support", runtime.GOROOT())
case runtime.GOARCH == "amd64":
canRace = runtime.GOOS == "linux" || runtime.GOOS == "freebsd" || runtime.GOOS == "windows" || runtime.GOOS == "darwin"
}

switch runtime.GOOS {
case "windows":
exeSuffix = ".exe"
Expand All @@ -79,11 +71,13 @@ func TestMain(m *testing.M) {
os.Exit(2)
}

switch runtime.GOOS {
case "linux", "darwin", "freebsd", "windows":
canRace = canCgo && runtime.GOARCH == "amd64"
_, err = os.Stat(filepath.Join(runtime.GOROOT(), "pkg", fmt.Sprintf("%s_%s_race", runtime.GOOS, runtime.GOARCH), "runtime.a"))
switch {
case os.IsNotExist(err):
log.Printf("go installation at %s is missing race support", runtime.GOROOT())
case runtime.GOARCH == "amd64":
canRace = runtime.GOOS == "linux" || runtime.GOOS == "freebsd" || runtime.GOOS == "windows" || runtime.GOOS == "darwin"
}
defer os.RemoveAll(dir)
}

// Don't let these environment variables confuse the test.
Expand Down Expand Up @@ -1259,9 +1253,6 @@ func TestTestRaceFlag(t *testing.T) {
if !canRace {
t.Skip("skipping because race detector not supported")
}
if strings.HasPrefix(runtime.Version(), "go1.4") {
t.Skipf("skipping race test as Go version %v incorrectly marks race failures as success", runtime.Version())
}

gb := T{T: t}
defer gb.cleanup()
Expand All @@ -1274,10 +1265,6 @@ func TestTestRaceFlag(t *testing.T) {
import "testing"
func TestRaceFlag(t *testing.T) {
if !canRace {
t.Skip("skipping because race detector not supported")
}
if A != 1 || B != 2 {
t.Fatal("expected", 1, 2,"got", A, B)
}
Expand All @@ -1297,6 +1284,40 @@ func TestTestRace(t *testing.T) {
if !canRace {
t.Skip("skipping because race detector not supported")
}
if strings.HasPrefix(runtime.Version(), "go1.4") {
t.Skipf("skipping race test as Go version %v incorrectly marks race failures as success", runtime.Version())
}

gb := T{T: t}
defer gb.cleanup()

gb.tempDir("src/race")
gb.tempFile("src/race/map_test.go", `package race
import "testing"
func TestRaceMapRW(t *testing.T) {
m := make(map[int]int)
ch := make(chan bool, 1)
go func() {
_ = m[1]
ch <- true
}()
m[1] = 1
<-ch
}
`)
gb.cd(gb.tempdir)
tmpdir := gb.tempDir("tmp")
gb.setenv("TMP", tmpdir)
gb.runFail("test", "-race")
gb.mustBeEmpty(tmpdir)
}

// check that missing -race support generates error message.
func TestRaceMissing(t *testing.T) {
if canRace {
t.Skip("skipping because race detector is available")
}

gb := T{T: t}
defer gb.cleanup()
Expand All @@ -1320,5 +1341,6 @@ func TestRaceMapRW(t *testing.T) {
tmpdir := gb.tempDir("tmp")
gb.setenv("TMP", tmpdir)
gb.runFail("test", "-race")
gb.grepStderr(regexp.QuoteMeta(fmt.Sprintf("FATAL: go installation at %s is missing race support", runtime.GOROOT())), "expected missing race support message")
gb.mustBeEmpty(tmpdir)
}
14 changes: 11 additions & 3 deletions cmd/gb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/constabulary/gb"
"github.com/constabulary/gb/cmd"
Expand Down Expand Up @@ -129,10 +130,17 @@ func main() {
gb.Ldflags(ldflags...),
gb.Tags(buildtags...),
func(c *gb.Context) error {
if race {
return gb.WithRace(c)
if !race {
return nil
}
return nil

// check the race runtime is built
_, err := os.Stat(filepath.Join(runtime.GOROOT(), "pkg", fmt.Sprintf("%s_%s_race", runtime.GOOS, runtime.GOARCH), "runtime.a"))
if os.IsNotExist(err) || err != nil {
fatalf("go installation at %s is missing race support. See https://getgb.io/faq/#missing-race-support", runtime.GOROOT())
}

return gb.WithRace(c)
},
)

Expand Down