From 87dae58662c78c772cb210d685b354ae6916ef2d Mon Sep 17 00:00:00 2001 From: Hiroshi Ioka Date: Wed, 30 Aug 2017 11:14:40 +0900 Subject: [PATCH] cmd/internal/goobj: add tests Change-Id: I4a0fe1c8625e7e9adfd84ac6910da83d0268f928 Reviewed-on: https://go-review.googlesource.com/60130 Reviewed-by: David Crawshaw Run-TryBot: David Crawshaw TryBot-Result: Gobot Gobot --- src/cmd/internal/goobj/goobj_test.go | 142 +++++++++++++++++++++++++ src/cmd/internal/goobj/testdata/go1.go | 11 ++ src/cmd/internal/goobj/testdata/go2.go | 11 ++ 3 files changed, 164 insertions(+) create mode 100644 src/cmd/internal/goobj/goobj_test.go create mode 100644 src/cmd/internal/goobj/testdata/go1.go create mode 100644 src/cmd/internal/goobj/testdata/go2.go diff --git a/src/cmd/internal/goobj/goobj_test.go b/src/cmd/internal/goobj/goobj_test.go new file mode 100644 index 00000000000..37fdcbb5cc3 --- /dev/null +++ b/src/cmd/internal/goobj/goobj_test.go @@ -0,0 +1,142 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package goobj + +import ( + "fmt" + "internal/testenv" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" +) + +var ( + buildDir string + go1obj string + go2obj string + goarchive string +) + +func TestMain(m *testing.M) { + if !testenv.HasGoBuild() { + return + } + + if runtime.GOOS == "linux" && runtime.GOARCH == "arm" { + return // skip tests due to #19811 + } + + if err := buildGoobj(); err != nil { + fmt.Println(err) + os.RemoveAll(buildDir) + os.Exit(1) + } + + exit := m.Run() + + os.RemoveAll(buildDir) + os.Exit(exit) +} + +func buildGoobj() error { + var err error + + buildDir, err = ioutil.TempDir("", "TestGoobj") + if err != nil { + return err + } + + go1obj = filepath.Join(buildDir, "go1.o") + go2obj = filepath.Join(buildDir, "go2.o") + goarchive = filepath.Join(buildDir, "go.a") + + gotool, err := testenv.GoTool() + if err != nil { + return err + } + + go1src := filepath.Join("testdata", "go1.go") + go2src := filepath.Join("testdata", "go2.go") + + out, err := exec.Command(gotool, "tool", "compile", "-o", go1obj, go1src).CombinedOutput() + if err != nil { + return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go1obj, go1src, err, out) + } + out, err = exec.Command(gotool, "tool", "compile", "-o", go2obj, go2src).CombinedOutput() + if err != nil { + return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go2obj, go2src, err, out) + } + out, err = exec.Command(gotool, "tool", "pack", "c", goarchive, go1obj, go2obj).CombinedOutput() + if err != nil { + return fmt.Errorf("go tool pack c %s %s %s: %v\n%s", goarchive, go1obj, go2obj, err, out) + } + + return nil +} + +func TestParseGoobj(t *testing.T) { + path := go1obj + + f, err := os.Open(path) + if err != nil { + t.Fatal(err) + } + defer f.Close() + + p, err := Parse(f, "mypkg") + if err != nil { + t.Fatal(err) + } + if p.Arch != runtime.GOARCH { + t.Errorf("%s: got %v, want %v", path, p.Arch, runtime.GOARCH) + } + var found bool + for _, s := range p.Syms { + if s.Name == "mypkg.go1" { + found = true + break + } + } + if !found { + t.Errorf(`%s: symbol "mypkg.go1" not found`, path) + } +} + +func TestParseArchive(t *testing.T) { + path := goarchive + + f, err := os.Open(path) + if err != nil { + t.Fatal(err) + } + defer f.Close() + + p, err := Parse(f, "mypkg") + if err != nil { + t.Fatal(err) + } + if p.Arch != runtime.GOARCH { + t.Errorf("%s: got %v, want %v", path, p.Arch, runtime.GOARCH) + } + var found1 bool + var found2 bool + for _, s := range p.Syms { + if s.Name == "mypkg.go1" { + found1 = true + } + if s.Name == "mypkg.go2" { + found2 = true + } + } + if !found1 { + t.Errorf(`%s: symbol "mypkg.go1" not found`, path) + } + if !found2 { + t.Errorf(`%s: symbol "mypkg.go2" not found`, path) + } +} diff --git a/src/cmd/internal/goobj/testdata/go1.go b/src/cmd/internal/goobj/testdata/go1.go new file mode 100644 index 00000000000..37d1ec19bbc --- /dev/null +++ b/src/cmd/internal/goobj/testdata/go1.go @@ -0,0 +1,11 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package mypkg + +import "fmt" + +func go1() { + fmt.Println("go1") +} diff --git a/src/cmd/internal/goobj/testdata/go2.go b/src/cmd/internal/goobj/testdata/go2.go new file mode 100644 index 00000000000..0e9c0d7338c --- /dev/null +++ b/src/cmd/internal/goobj/testdata/go2.go @@ -0,0 +1,11 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package mypkg + +import "fmt" + +func go2() { + fmt.Println("go2") +}