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.
14 func TestMatch(t *testing.T) {
17 match := func(tag string) {
19 t.Errorf("%s context should match %s, does not", what, tag)
22 nomatch := func(tag string) {
24 t.Errorf("%s context should NOT match %s, does", what, tag)
28 match(runtime.GOOS + "," + runtime.GOARCH)
29 match(runtime.GOOS + "," + runtime.GOARCH + ",!foo")
30 nomatch(runtime.GOOS + "," + runtime.GOARCH + ",foo")
33 ctxt.BuildTags = []string{"foo"}
34 match(runtime.GOOS + "," + runtime.GOARCH)
35 match(runtime.GOOS + "," + runtime.GOARCH + ",foo")
36 nomatch(runtime.GOOS + "," + runtime.GOARCH + ",!foo")
37 match(runtime.GOOS + "," + runtime.GOARCH + ",!bar")
38 nomatch(runtime.GOOS + "," + runtime.GOARCH + ",bar")
42 func TestDotSlashImport(t *testing.T) {
43 p, err := ImportDir("testdata/other", 0)
47 if len(p.Imports) != 1 || p.Imports[0] != "./file" {
48 t.Fatalf("testdata/other: Imports=%v, want [./file]", p.Imports)
51 p1, err := Import("./file", "testdata/other", 0)
55 if p1.Name != "file" {
56 t.Fatalf("./file: Name=%q, want %q", p1.Name, "file")
58 dir := filepath.Clean("testdata/other/file") // Clean to use \ on Windows
60 t.Fatalf("./file: Dir=%q, want %q", p1.Name, dir)
64 func TestEmptyImport(t *testing.T) {
65 p, err := Import("", Default.GOROOT, FindOnly)
67 t.Fatal(`Import("") returned nil error.`)
70 t.Fatal(`Import("") returned nil package.`)
72 if p.ImportPath != "" {
73 t.Fatalf("ImportPath=%q, want %q.", p.ImportPath, "")
77 func TestLocalDirectory(t *testing.T) {
78 cwd, err := os.Getwd()
83 p, err := ImportDir(cwd, 0)
87 if p.ImportPath != "go/build" {
88 t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "go/build")
92 func TestShouldBuild(t *testing.T) {
93 const file1 = "// +build tag1\n\n" +
96 const file2 = "// +build cgo\n\n" +
97 "// This package implements parsing of tags like\n" +
101 const file3 = "// Copyright The Go Authors.\n\n" +
102 "package build\n\n" +
103 "// shouldBuild checks tags given by lines of the form\n" +
105 "func shouldBuild(content []byte)\n"
107 ctx := &Context{BuildTags: []string{"tag1"}}
108 if !ctx.shouldBuild([]byte(file1)) {
109 t.Errorf("should not build file1, expected the contrary")
111 if ctx.shouldBuild([]byte(file2)) {
112 t.Errorf("should build file2, expected the contrary")
115 ctx = &Context{BuildTags: nil}
116 if !ctx.shouldBuild([]byte(file3)) {
117 t.Errorf("should not build file3, expected the contrary")