mirror of
https://github.com/golang/go
synced 2024-11-12 05:30:21 -07:00
go/build: add test support & use in gotest
A side-effect is that, just like foo_386.go is only built on 386, foo_386_test.go is only built for testing on 386. R=adg, r, mattn.jp CC=golang-dev https://golang.org/cl/4942050
This commit is contained in:
parent
74fc7d8f12
commit
66bedf8221
@ -9,12 +9,13 @@ import (
|
|||||||
"exec"
|
"exec"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
"go/build"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
@ -159,17 +160,19 @@ func setEnvironment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getTestFileNames gets the set of files we're looking at.
|
// getTestFileNames gets the set of files we're looking at.
|
||||||
// If gotest has no arguments, it scans for file names matching "[^.]*_test.go".
|
// If gotest has no arguments, it scans the current directory
|
||||||
|
// for test files.
|
||||||
func getTestFileNames() {
|
func getTestFileNames() {
|
||||||
names := fileNames
|
names := fileNames
|
||||||
if len(names) == 0 {
|
if len(names) == 0 {
|
||||||
var err os.Error
|
info, err := build.ScanDir(".", true)
|
||||||
names, err = filepath.Glob("[^.]*_test.go")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatalf("Glob pattern error: %s", err)
|
Fatalf("scanning directory: %v", err)
|
||||||
}
|
}
|
||||||
|
names = append(info.TestGoFiles, info.XTestGoFiles...)
|
||||||
|
sort.Strings(names)
|
||||||
if len(names) == 0 {
|
if len(names) == 0 {
|
||||||
Fatalf(`no test files found: no match for "[^.]*_test.go"`)
|
Fatalf("no test files found in current directory")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, n := range names {
|
for _, n := range names {
|
||||||
|
@ -7,44 +7,83 @@ package build
|
|||||||
import (
|
import (
|
||||||
"exec"
|
"exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var buildPkgs = []string{
|
func sortstr(x []string) []string {
|
||||||
"go/build/pkgtest",
|
sort.Strings(x)
|
||||||
"go/build/cmdtest",
|
return x
|
||||||
"go/build/cgotest",
|
}
|
||||||
|
|
||||||
|
var buildPkgs = []struct {
|
||||||
|
dir string
|
||||||
|
info *DirInfo
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"go/build/pkgtest",
|
||||||
|
&DirInfo{
|
||||||
|
GoFiles: []string{"pkgtest.go"},
|
||||||
|
SFiles: []string{"sqrt_" + runtime.GOARCH + ".s"},
|
||||||
|
PkgName: "pkgtest",
|
||||||
|
TestGoFiles: sortstr([]string{"sqrt_test.go", "sqrt_" + runtime.GOARCH + "_test.go"}),
|
||||||
|
XTestGoFiles: []string{"xsqrt_test.go"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"go/build/cmdtest",
|
||||||
|
&DirInfo{
|
||||||
|
GoFiles: []string{"main.go"},
|
||||||
|
PkgName: "main",
|
||||||
|
Imports: []string{"go/build/pkgtest"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"go/build/cgotest",
|
||||||
|
&DirInfo{
|
||||||
|
CgoFiles: []string{"cgotest.go"},
|
||||||
|
CFiles: []string{"cgotest.c"},
|
||||||
|
Imports: []string{"C", "unsafe"},
|
||||||
|
PkgName: "cgotest",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const cmdtestOutput = "3"
|
const cmdtestOutput = "3"
|
||||||
|
|
||||||
func TestBuild(t *testing.T) {
|
func TestBuild(t *testing.T) {
|
||||||
for _, pkg := range buildPkgs {
|
for _, tt := range buildPkgs {
|
||||||
tree := Path[0] // Goroot
|
tree := Path[0] // Goroot
|
||||||
dir := filepath.Join(tree.SrcDir(), pkg)
|
dir := filepath.Join(tree.SrcDir(), tt.dir)
|
||||||
|
|
||||||
info, err := ScanDir(dir, true)
|
info, err := ScanDir(dir, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("ScanDir:", err)
|
t.Errorf("ScanDir(%#q): %v", tt.dir, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(info, tt.info) {
|
||||||
|
t.Errorf("ScanDir(%#q) = %#v, want %#v\n", tt.dir, info, tt.info)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err := Build(tree, pkg, info)
|
s, err := Build(tree, tt.dir, info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Build:", err)
|
t.Errorf("Build(%#q): %v", tt.dir, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.Run(); err != nil {
|
if err := s.Run(); err != nil {
|
||||||
t.Error("Run:", err)
|
t.Errorf("Run(%#q): %v", tt.dir, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if pkg == "go/build/cmdtest" {
|
if tt.dir == "go/build/cmdtest" {
|
||||||
bin := s.Output[0]
|
bin := s.Output[0]
|
||||||
b, err := exec.Command(bin).CombinedOutput()
|
b, err := exec.Command(bin).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("exec: %s: %v", bin, err)
|
t.Errorf("exec %s: %v", bin, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if string(b) != cmdtestOutput {
|
if string(b) != cmdtestOutput {
|
||||||
@ -52,6 +91,7 @@ func TestBuild(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deferred because cmdtest depends on pkgtest.
|
||||||
defer func(s *Script) {
|
defer func(s *Script) {
|
||||||
if err := s.Nuke(); err != nil {
|
if err := s.Nuke(); err != nil {
|
||||||
t.Errorf("nuking: %v", err)
|
t.Errorf("nuking: %v", err)
|
||||||
|
@ -7,27 +7,59 @@ package build
|
|||||||
import (
|
import (
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A Context specifies the supporting context for a build.
|
||||||
|
type Context struct {
|
||||||
|
GOARCH string // target architecture
|
||||||
|
GOOS string // target operating system
|
||||||
|
// TODO(rsc,adg): GOPATH
|
||||||
|
}
|
||||||
|
|
||||||
|
// The DefaultContext is the default Context for builds.
|
||||||
|
// It uses the GOARCH and GOOS environment variables
|
||||||
|
// if set, or else the compiled code's GOARCH and GOOS.
|
||||||
|
var DefaultContext = Context{
|
||||||
|
envOr("GOARCH", runtime.GOARCH),
|
||||||
|
envOr("GOOS", runtime.GOOS),
|
||||||
|
}
|
||||||
|
|
||||||
|
func envOr(name, def string) string {
|
||||||
|
s := os.Getenv(name)
|
||||||
|
if s == "" {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
type DirInfo struct {
|
type DirInfo struct {
|
||||||
GoFiles []string // .go files in dir (excluding CgoFiles)
|
GoFiles []string // .go files in dir (excluding CgoFiles)
|
||||||
CgoFiles []string // .go files that import "C"
|
CgoFiles []string // .go files that import "C"
|
||||||
CFiles []string // .c files in dir
|
CFiles []string // .c files in dir
|
||||||
SFiles []string // .s files in dir
|
SFiles []string // .s files in dir
|
||||||
Imports []string // All packages imported by goFiles
|
Imports []string // All packages imported by goFiles
|
||||||
PkgName string // Name of package in dir
|
PkgName string // Name of package in dir
|
||||||
|
TestGoFiles []string // _test.go files in package
|
||||||
|
XTestGoFiles []string // _test.go files outside package
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DirInfo) IsCommand() bool {
|
func (d *DirInfo) IsCommand() bool {
|
||||||
return d.PkgName == "main"
|
return d.PkgName == "main"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ScanDir calls DefaultContext.ScanDir.
|
||||||
|
func ScanDir(dir string, allowMain bool) (info *DirInfo, err os.Error) {
|
||||||
|
return DefaultContext.ScanDir(dir, allowMain)
|
||||||
|
}
|
||||||
|
|
||||||
// ScanDir returns a structure with details about the Go content found
|
// ScanDir returns a structure with details about the Go content found
|
||||||
// in the given directory. The file lists exclude:
|
// in the given directory. The file lists exclude:
|
||||||
//
|
//
|
||||||
@ -36,14 +68,8 @@ func (d *DirInfo) IsCommand() bool {
|
|||||||
// - files ending in _test.go
|
// - files ending in _test.go
|
||||||
// - files starting with _ or .
|
// - files starting with _ or .
|
||||||
//
|
//
|
||||||
// Only files that satisfy the goodOSArch function are included.
|
func (ctxt *Context) ScanDir(dir string, allowMain bool) (info *DirInfo, err os.Error) {
|
||||||
func ScanDir(dir string, allowMain bool) (info *DirInfo, err os.Error) {
|
dirs, err := ioutil.ReadDir(dir)
|
||||||
f, err := os.Open(dir)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
dirs, err := f.Readdir(-1)
|
|
||||||
f.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -51,21 +77,19 @@ func ScanDir(dir string, allowMain bool) (info *DirInfo, err os.Error) {
|
|||||||
var di DirInfo
|
var di DirInfo
|
||||||
imported := make(map[string]bool)
|
imported := make(map[string]bool)
|
||||||
fset := token.NewFileSet()
|
fset := token.NewFileSet()
|
||||||
for i := range dirs {
|
for _, d := range dirs {
|
||||||
d := &dirs[i]
|
|
||||||
if strings.HasPrefix(d.Name, "_") ||
|
if strings.HasPrefix(d.Name, "_") ||
|
||||||
strings.HasPrefix(d.Name, ".") {
|
strings.HasPrefix(d.Name, ".") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !goodOSArch(d.Name) {
|
if !ctxt.goodOSArch(d.Name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isTest := false
|
||||||
switch filepath.Ext(d.Name) {
|
switch filepath.Ext(d.Name) {
|
||||||
case ".go":
|
case ".go":
|
||||||
if strings.HasSuffix(d.Name, "_test.go") {
|
isTest = strings.HasSuffix(d.Name, "_test.go")
|
||||||
continue
|
|
||||||
}
|
|
||||||
case ".c":
|
case ".c":
|
||||||
di.CFiles = append(di.CFiles, d.Name)
|
di.CFiles = append(di.CFiles, d.Name)
|
||||||
continue
|
continue
|
||||||
@ -81,21 +105,24 @@ func ScanDir(dir string, allowMain bool) (info *DirInfo, err os.Error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s := string(pf.Name.Name)
|
pkg := string(pf.Name.Name)
|
||||||
if s == "main" && !allowMain {
|
if pkg == "main" && !allowMain {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if s == "documentation" {
|
if pkg == "documentation" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if isTest && strings.HasSuffix(pkg, "_test") {
|
||||||
|
pkg = pkg[:len(pkg)-len("_test")]
|
||||||
|
}
|
||||||
if di.PkgName == "" {
|
if di.PkgName == "" {
|
||||||
di.PkgName = s
|
di.PkgName = pkg
|
||||||
} else if di.PkgName != s {
|
} else if di.PkgName != pkg {
|
||||||
// Only if all files in the directory are in package main
|
// Only if all files in the directory are in package main
|
||||||
// do we return PkgName=="main".
|
// do we return PkgName=="main".
|
||||||
// A mix of main and another package reverts
|
// A mix of main and another package reverts
|
||||||
// to the original (allowMain=false) behaviour.
|
// to the original (allowMain=false) behaviour.
|
||||||
if s == "main" || di.PkgName == "main" {
|
if pkg == "main" || di.PkgName == "main" {
|
||||||
return ScanDir(dir, false)
|
return ScanDir(dir, false)
|
||||||
}
|
}
|
||||||
return nil, os.NewError("multiple package names in " + dir)
|
return nil, os.NewError("multiple package names in " + dir)
|
||||||
@ -109,11 +136,20 @@ func ScanDir(dir string, allowMain bool) (info *DirInfo, err os.Error) {
|
|||||||
}
|
}
|
||||||
imported[path] = true
|
imported[path] = true
|
||||||
if path == "C" {
|
if path == "C" {
|
||||||
|
if isTest {
|
||||||
|
return nil, os.NewError("use of cgo in test " + filename)
|
||||||
|
}
|
||||||
isCgo = true
|
isCgo = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if isCgo {
|
if isCgo {
|
||||||
di.CgoFiles = append(di.CgoFiles, d.Name)
|
di.CgoFiles = append(di.CgoFiles, d.Name)
|
||||||
|
} else if isTest {
|
||||||
|
if pkg == string(pf.Name.Name) {
|
||||||
|
di.TestGoFiles = append(di.TestGoFiles, d.Name)
|
||||||
|
} else {
|
||||||
|
di.XTestGoFiles = append(di.XTestGoFiles, d.Name)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
di.GoFiles = append(di.GoFiles, d.Name)
|
di.GoFiles = append(di.GoFiles, d.Name)
|
||||||
}
|
}
|
||||||
@ -124,49 +160,51 @@ func ScanDir(dir string, allowMain bool) (info *DirInfo, err os.Error) {
|
|||||||
di.Imports[i] = p
|
di.Imports[i] = p
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
// File name lists are sorted because ioutil.ReadDir sorts.
|
||||||
|
sort.Strings(di.Imports)
|
||||||
return &di, nil
|
return &di, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// goodOSArch returns false if the filename contains a $GOOS or $GOARCH
|
// goodOSArch returns false if the name contains a $GOOS or $GOARCH
|
||||||
// suffix which does not match the current system.
|
// suffix which does not match the current system.
|
||||||
// The recognized filename formats are:
|
// The recognized name formats are:
|
||||||
//
|
//
|
||||||
// name_$(GOOS).*
|
// name_$(GOOS).*
|
||||||
// name_$(GOARCH).*
|
// name_$(GOARCH).*
|
||||||
// name_$(GOOS)_$(GOARCH).*
|
// name_$(GOOS)_$(GOARCH).*
|
||||||
|
// name_$(GOOS)_test.*
|
||||||
|
// name_$(GOARCH)_test.*
|
||||||
|
// name_$(GOOS)_$(GOARCH)_test.*
|
||||||
//
|
//
|
||||||
func goodOSArch(filename string) bool {
|
func (ctxt *Context) goodOSArch(name string) bool {
|
||||||
if dot := strings.Index(filename, "."); dot != -1 {
|
if dot := strings.Index(name, "."); dot != -1 {
|
||||||
filename = filename[:dot]
|
name = name[:dot]
|
||||||
|
}
|
||||||
|
l := strings.Split(name, "_")
|
||||||
|
if n := len(l); n > 0 && l[n-1] == "test" {
|
||||||
|
l = l[:n-1]
|
||||||
}
|
}
|
||||||
l := strings.Split(filename, "_")
|
|
||||||
n := len(l)
|
n := len(l)
|
||||||
if n == 0 {
|
if n >= 2 && knownOS[l[n-2]] && knownArch[l[n-1]] {
|
||||||
return true
|
return l[n-2] == ctxt.GOOS && l[n-1] == ctxt.GOARCH
|
||||||
}
|
}
|
||||||
if good, known := goodOS[l[n-1]]; known {
|
if n >= 1 && knownOS[l[n-1]] {
|
||||||
return good
|
return l[n-1] == ctxt.GOOS
|
||||||
}
|
}
|
||||||
if good, known := goodArch[l[n-1]]; known {
|
if n >= 1 && knownArch[l[n-1]] {
|
||||||
if !good || n < 2 {
|
return l[n-1] == ctxt.GOARCH
|
||||||
return false
|
|
||||||
}
|
|
||||||
good, known = goodOS[l[n-2]]
|
|
||||||
return good || !known
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
var goodOS = make(map[string]bool)
|
var knownOS = make(map[string]bool)
|
||||||
var goodArch = make(map[string]bool)
|
var knownArch = make(map[string]bool)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
goodOS = make(map[string]bool)
|
|
||||||
goodArch = make(map[string]bool)
|
|
||||||
for _, v := range strings.Fields(goosList) {
|
for _, v := range strings.Fields(goosList) {
|
||||||
goodOS[v] = v == runtime.GOOS
|
knownOS[v] = true
|
||||||
}
|
}
|
||||||
for _, v := range strings.Fields(goarchList) {
|
for _, v := range strings.Fields(goarchList) {
|
||||||
goodArch[v] = v == runtime.GOARCH
|
knownArch[v] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
src/pkg/go/build/pkgtest/sqrt_386_test.go
Normal file
1
src/pkg/go/build/pkgtest/sqrt_386_test.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package pkgtest
|
1
src/pkg/go/build/pkgtest/sqrt_amd64_test.go
Normal file
1
src/pkg/go/build/pkgtest/sqrt_amd64_test.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package pkgtest
|
1
src/pkg/go/build/pkgtest/sqrt_arm_test.go
Normal file
1
src/pkg/go/build/pkgtest/sqrt_arm_test.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package pkgtest
|
1
src/pkg/go/build/pkgtest/sqrt_test.go
Normal file
1
src/pkg/go/build/pkgtest/sqrt_test.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package pkgtest
|
1
src/pkg/go/build/pkgtest/xsqrt_test.go
Normal file
1
src/pkg/go/build/pkgtest/xsqrt_test.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package pkgtest_test
|
@ -55,7 +55,7 @@ var tests = []GoodFileTest{
|
|||||||
|
|
||||||
func TestGoodOSArch(t *testing.T) {
|
func TestGoodOSArch(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
if goodOSArch(test.name) != test.result {
|
if DefaultContext.goodOSArch(test.name) != test.result {
|
||||||
t.Fatalf("goodOSArch(%q) != %v", test.name, test.result)
|
t.Fatalf("goodOSArch(%q) != %v", test.name, test.result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user