github.com/shijuvar/go@v0.0.0-20141209052335-e8f13700b70c/src/go/build/build_test.go (about)

     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.
     4  
     5  package build
     6  
     7  import (
     8  	"io"
     9  	"os"
    10  	"path/filepath"
    11  	"reflect"
    12  	"runtime"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func TestMatch(t *testing.T) {
    18  	ctxt := Default
    19  	what := "default"
    20  	match := func(tag string, want map[string]bool) {
    21  		m := make(map[string]bool)
    22  		if !ctxt.match(tag, m) {
    23  			t.Errorf("%s context should match %s, does not", what, tag)
    24  		}
    25  		if !reflect.DeepEqual(m, want) {
    26  			t.Errorf("%s tags = %v, want %v", tag, m, want)
    27  		}
    28  	}
    29  	nomatch := func(tag string, want map[string]bool) {
    30  		m := make(map[string]bool)
    31  		if ctxt.match(tag, m) {
    32  			t.Errorf("%s context should NOT match %s, does", what, tag)
    33  		}
    34  		if !reflect.DeepEqual(m, want) {
    35  			t.Errorf("%s tags = %v, want %v", tag, m, want)
    36  		}
    37  	}
    38  
    39  	match(runtime.GOOS+","+runtime.GOARCH, map[string]bool{runtime.GOOS: true, runtime.GOARCH: true})
    40  	match(runtime.GOOS+","+runtime.GOARCH+",!foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true})
    41  	nomatch(runtime.GOOS+","+runtime.GOARCH+",foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true})
    42  
    43  	what = "modified"
    44  	ctxt.BuildTags = []string{"foo"}
    45  	match(runtime.GOOS+","+runtime.GOARCH, map[string]bool{runtime.GOOS: true, runtime.GOARCH: true})
    46  	match(runtime.GOOS+","+runtime.GOARCH+",foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true})
    47  	nomatch(runtime.GOOS+","+runtime.GOARCH+",!foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true})
    48  	match(runtime.GOOS+","+runtime.GOARCH+",!bar", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "bar": true})
    49  	nomatch(runtime.GOOS+","+runtime.GOARCH+",bar", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "bar": true})
    50  	nomatch("!", map[string]bool{})
    51  }
    52  
    53  func TestDotSlashImport(t *testing.T) {
    54  	p, err := ImportDir("testdata/other", 0)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if len(p.Imports) != 1 || p.Imports[0] != "./file" {
    59  		t.Fatalf("testdata/other: Imports=%v, want [./file]", p.Imports)
    60  	}
    61  
    62  	p1, err := Import("./file", "testdata/other", 0)
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	if p1.Name != "file" {
    67  		t.Fatalf("./file: Name=%q, want %q", p1.Name, "file")
    68  	}
    69  	dir := filepath.Clean("testdata/other/file") // Clean to use \ on Windows
    70  	if p1.Dir != dir {
    71  		t.Fatalf("./file: Dir=%q, want %q", p1.Name, dir)
    72  	}
    73  }
    74  
    75  func TestEmptyImport(t *testing.T) {
    76  	p, err := Import("", Default.GOROOT, FindOnly)
    77  	if err == nil {
    78  		t.Fatal(`Import("") returned nil error.`)
    79  	}
    80  	if p == nil {
    81  		t.Fatal(`Import("") returned nil package.`)
    82  	}
    83  	if p.ImportPath != "" {
    84  		t.Fatalf("ImportPath=%q, want %q.", p.ImportPath, "")
    85  	}
    86  }
    87  
    88  func TestEmptyFolderImport(t *testing.T) {
    89  	_, err := Import(".", "testdata/empty", 0)
    90  	if _, ok := err.(*NoGoError); !ok {
    91  		t.Fatal(`Import("testdata/empty") did not return NoGoError.`)
    92  	}
    93  }
    94  
    95  func TestMultiplePackageImport(t *testing.T) {
    96  	_, err := Import(".", "testdata/multi", 0)
    97  	if _, ok := err.(*MultiplePackageError); !ok {
    98  		t.Fatal(`Import("testdata/multi") did not return MultiplePackageError.`)
    99  	}
   100  }
   101  
   102  func TestLocalDirectory(t *testing.T) {
   103  	cwd, err := os.Getwd()
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	p, err := ImportDir(cwd, 0)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	if p.ImportPath != "go/build" {
   113  		t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "go/build")
   114  	}
   115  }
   116  
   117  func TestShouldBuild(t *testing.T) {
   118  	const file1 = "// +build tag1\n\n" +
   119  		"package main\n"
   120  	want1 := map[string]bool{"tag1": true}
   121  
   122  	const file2 = "// +build cgo\n\n" +
   123  		"// This package implements parsing of tags like\n" +
   124  		"// +build tag1\n" +
   125  		"package build"
   126  	want2 := map[string]bool{"cgo": true}
   127  
   128  	const file3 = "// Copyright The Go Authors.\n\n" +
   129  		"package build\n\n" +
   130  		"// shouldBuild checks tags given by lines of the form\n" +
   131  		"// +build tag\n" +
   132  		"func shouldBuild(content []byte)\n"
   133  	want3 := map[string]bool{}
   134  
   135  	ctx := &Context{BuildTags: []string{"tag1"}}
   136  	m := map[string]bool{}
   137  	if !ctx.shouldBuild([]byte(file1), m) {
   138  		t.Errorf("shouldBuild(file1) = false, want true")
   139  	}
   140  	if !reflect.DeepEqual(m, want1) {
   141  		t.Errorf("shoudBuild(file1) tags = %v, want %v", m, want1)
   142  	}
   143  
   144  	m = map[string]bool{}
   145  	if ctx.shouldBuild([]byte(file2), m) {
   146  		t.Errorf("shouldBuild(file2) = true, want fakse")
   147  	}
   148  	if !reflect.DeepEqual(m, want2) {
   149  		t.Errorf("shoudBuild(file2) tags = %v, want %v", m, want2)
   150  	}
   151  
   152  	m = map[string]bool{}
   153  	ctx = &Context{BuildTags: nil}
   154  	if !ctx.shouldBuild([]byte(file3), m) {
   155  		t.Errorf("shouldBuild(file3) = false, want true")
   156  	}
   157  	if !reflect.DeepEqual(m, want3) {
   158  		t.Errorf("shoudBuild(file3) tags = %v, want %v", m, want3)
   159  	}
   160  }
   161  
   162  type readNopCloser struct {
   163  	io.Reader
   164  }
   165  
   166  func (r readNopCloser) Close() error {
   167  	return nil
   168  }
   169  
   170  var (
   171  	ctxtP9      = Context{GOARCH: "arm", GOOS: "plan9"}
   172  	ctxtAndroid = Context{GOARCH: "arm", GOOS: "android"}
   173  )
   174  
   175  var matchFileTests = []struct {
   176  	ctxt  Context
   177  	name  string
   178  	data  string
   179  	match bool
   180  }{
   181  	{ctxtP9, "foo_arm.go", "", true},
   182  	{ctxtP9, "foo1_arm.go", "// +build linux\n\npackage main\n", false},
   183  	{ctxtP9, "foo_darwin.go", "", false},
   184  	{ctxtP9, "foo.go", "", true},
   185  	{ctxtP9, "foo1.go", "// +build linux\n\npackage main\n", false},
   186  	{ctxtP9, "foo.badsuffix", "", false},
   187  	{ctxtAndroid, "foo_linux.go", "", true},
   188  	{ctxtAndroid, "foo_android.go", "", true},
   189  	{ctxtAndroid, "foo_plan9.go", "", false},
   190  	{ctxtAndroid, "android.go", "", true},
   191  	{ctxtAndroid, "plan9.go", "", true},
   192  	{ctxtAndroid, "plan9_test.go", "", true},
   193  	{ctxtAndroid, "arm.s", "", true},
   194  	{ctxtAndroid, "amd64.s", "", true},
   195  }
   196  
   197  func TestMatchFile(t *testing.T) {
   198  	for _, tt := range matchFileTests {
   199  		ctxt := tt.ctxt
   200  		ctxt.OpenFile = func(path string) (r io.ReadCloser, err error) {
   201  			if path != "x+"+tt.name {
   202  				t.Fatalf("OpenFile asked for %q, expected %q", path, "x+"+tt.name)
   203  			}
   204  			return &readNopCloser{strings.NewReader(tt.data)}, nil
   205  		}
   206  		ctxt.JoinPath = func(elem ...string) string {
   207  			return strings.Join(elem, "+")
   208  		}
   209  		match, err := ctxt.MatchFile("x", tt.name)
   210  		if match != tt.match || err != nil {
   211  			t.Fatalf("MatchFile(%q) = %v, %v, want %v, nil", tt.name, match, err, tt.match)
   212  		}
   213  	}
   214  }
   215  
   216  func TestImportCmd(t *testing.T) {
   217  	p, err := Import("cmd/internal/objfile", "", 0)
   218  	if err != nil {
   219  		t.Fatal(err)
   220  	}
   221  	if !strings.HasSuffix(filepath.ToSlash(p.Dir), "src/cmd/internal/objfile") {
   222  		t.Fatalf("Import cmd/internal/objfile returned Dir=%q, want %q", filepath.ToSlash(p.Dir), ".../src/cmd/internal/objfile")
   223  	}
   224  }