1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 func sortstr(x []string) []string {
21 var buildPkgs = []struct {
28 GoFiles: []string{"pkgtest.go"},
29 SFiles: []string{"sqrt_" + runtime.GOARCH + ".s"},
31 Imports: []string{"bytes"},
32 TestImports: []string{"fmt", "pkgtest"},
33 TestGoFiles: sortstr([]string{"sqrt_test.go", "sqrt_" + runtime.GOARCH + "_test.go"}),
34 XTestGoFiles: []string{"xsqrt_test.go"},
40 GoFiles: []string{"main.go"},
42 Imports: []string{"go/build/pkgtest"},
43 TestImports: []string{},
49 CgoFiles: ifCgo([]string{"cgotest.go"}),
50 CFiles: []string{"cgotest.c"},
51 HFiles: []string{"cgotest.h"},
52 Imports: []string{"C", "unsafe"},
53 TestImports: []string{},
59 func ifCgo(x []string) []string {
60 if DefaultContext.CgoEnabled {
66 const cmdtestOutput = "3"
68 func TestBuild(t *testing.T) {
69 for _, tt := range buildPkgs {
70 tree := Path[0] // Goroot
71 dir := filepath.Join(tree.SrcDir(), tt.dir)
72 info, err := ScanDir(dir)
74 t.Errorf("ScanDir(%#q): %v", tt.dir, err)
77 if !reflect.DeepEqual(info, tt.info) {
78 t.Errorf("ScanDir(%#q) = %#v, want %#v\n", tt.dir, info, tt.info)
82 if tt.dir == "go/build/cgotest" && len(info.CgoFiles) == 0 {
86 s, err := Build(tree, tt.dir, info)
88 t.Errorf("Build(%#q): %v", tt.dir, err)
92 if err := s.Run(); err != nil {
93 t.Errorf("Run(%#q): %v", tt.dir, err)
97 if tt.dir == "go/build/cmdtest" {
99 b, err := exec.Command(bin).CombinedOutput()
101 t.Errorf("exec %s: %v", bin, err)
104 if string(b) != cmdtestOutput {
105 t.Errorf("cmdtest output: %s want: %s", b, cmdtestOutput)
109 // Deferred because cmdtest depends on pkgtest.
110 defer func(s *Script) {
111 if err := s.Nuke(); err != nil {
112 t.Errorf("nuking: %v", err)