Commit bf3bf092 authored by Ian Lance Taylor's avatar Ian Lance Taylor

debug/gosym: clean up and modernize pclntab_test

The self tests do not need to build the binary; they won't read it.  The
self tests should work on any ELF system.

Use t.Skip instead of panic.  Use internal/testenv.  Don't worry about a
space in the temporary directory name.

Change-Id: I66ef0af90520d330820afa7b6c6b3a132ab27454
Reviewed-on: https://go-review.googlesource.com/15495
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 449cc06e
...@@ -6,6 +6,7 @@ package gosym ...@@ -6,6 +6,7 @@ package gosym
import ( import (
"debug/elf" "debug/elf"
"internal/testenv"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
...@@ -20,25 +21,16 @@ var ( ...@@ -20,25 +21,16 @@ var (
pclinetestBinary string pclinetestBinary string
) )
func dotest(self bool) bool { func dotest(t *testing.T) {
testenv.MustHaveGoBuild(t)
// For now, only works on amd64 platforms. // For now, only works on amd64 platforms.
if runtime.GOARCH != "amd64" { if runtime.GOARCH != "amd64" {
return false t.Skipf("skipping on non-AMD64 system %s", runtime.GOARCH)
}
// Self test reads test binary; only works on Linux.
if self && runtime.GOOS != "linux" {
return false
}
if pclinetestBinary != "" {
return true
} }
var err error var err error
pclineTempDir, err = ioutil.TempDir("", "pclinetest") pclineTempDir, err = ioutil.TempDir("", "pclinetest")
if err != nil { if err != nil {
panic(err) t.Fatal(err)
}
if strings.Contains(pclineTempDir, " ") {
panic("unexpected space in tempdir")
} }
// This command builds pclinetest from pclinetest.asm; // This command builds pclinetest from pclinetest.asm;
// the resulting binary looks like it was built from pclinetest.s, // the resulting binary looks like it was built from pclinetest.s,
...@@ -48,16 +40,15 @@ func dotest(self bool) bool { ...@@ -48,16 +40,15 @@ func dotest(self bool) bool {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
panic(err) t.Fatal(err)
} }
cmd = exec.Command("go", "tool", "link", "-H", "linux", "-E", "main", cmd = exec.Command("go", "tool", "link", "-H", "linux", "-E", "main",
"-o", pclinetestBinary, pclinetestBinary+".o") "-o", pclinetestBinary, pclinetestBinary+".o")
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
panic(err) t.Fatal(err)
} }
return true
} }
func endtest() { func endtest() {
...@@ -68,6 +59,17 @@ func endtest() { ...@@ -68,6 +59,17 @@ func endtest() {
} }
} }
// skipIfNotELF skips the test if we are not running on an ELF system.
// These tests open and examine the test binary, and use elf.Open to do so.
func skipIfNotELF(t *testing.T) {
switch runtime.GOOS {
case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris":
// OK.
default:
t.Skipf("skipping on non-ELF system %s", runtime.GOOS)
}
}
func getTable(t *testing.T) *Table { func getTable(t *testing.T) *Table {
f, tab := crack(os.Args[0], t) f, tab := crack(os.Args[0], t)
f.Close() f.Close()
...@@ -112,10 +114,7 @@ func parse(file string, f *elf.File, t *testing.T) (*elf.File, *Table) { ...@@ -112,10 +114,7 @@ func parse(file string, f *elf.File, t *testing.T) (*elf.File, *Table) {
var goarch = os.Getenv("O") var goarch = os.Getenv("O")
func TestLineFromAline(t *testing.T) { func TestLineFromAline(t *testing.T) {
if !dotest(true) { skipIfNotELF(t)
return
}
defer endtest()
tab := getTable(t) tab := getTable(t)
if tab.go12line != nil { if tab.go12line != nil {
...@@ -164,10 +163,7 @@ func TestLineFromAline(t *testing.T) { ...@@ -164,10 +163,7 @@ func TestLineFromAline(t *testing.T) {
} }
func TestLineAline(t *testing.T) { func TestLineAline(t *testing.T) {
if !dotest(true) { skipIfNotELF(t)
return
}
defer endtest()
tab := getTable(t) tab := getTable(t)
if tab.go12line != nil { if tab.go12line != nil {
...@@ -210,9 +206,7 @@ func TestLineAline(t *testing.T) { ...@@ -210,9 +206,7 @@ func TestLineAline(t *testing.T) {
} }
func TestPCLine(t *testing.T) { func TestPCLine(t *testing.T) {
if !dotest(false) { dotest(t)
return
}
defer endtest() defer endtest()
f, tab := crack(pclinetestBinary, t) f, tab := crack(pclinetestBinary, t)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment