github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/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 "os" 9 "path/filepath" 10 "runtime" 11 "testing" 12 ) 13 14 func TestMatch(t *testing.T) { 15 ctxt := Default 16 what := "default" 17 match := func(tag string) { 18 if !ctxt.match(tag) { 19 t.Errorf("%s context should match %s, does not", what, tag) 20 } 21 } 22 nomatch := func(tag string) { 23 if ctxt.match(tag) { 24 t.Errorf("%s context should NOT match %s, does", what, tag) 25 } 26 } 27 28 match(runtime.GOOS + "," + runtime.GOARCH) 29 match(runtime.GOOS + "," + runtime.GOARCH + ",!foo") 30 nomatch(runtime.GOOS + "," + runtime.GOARCH + ",foo") 31 32 what = "modified" 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") 39 nomatch("!") 40 } 41 42 func TestDotSlashImport(t *testing.T) { 43 p, err := ImportDir("testdata/other", 0) 44 if err != nil { 45 t.Fatal(err) 46 } 47 if len(p.Imports) != 1 || p.Imports[0] != "./file" { 48 t.Fatalf("testdata/other: Imports=%v, want [./file]", p.Imports) 49 } 50 51 p1, err := Import("./file", "testdata/other", 0) 52 if err != nil { 53 t.Fatal(err) 54 } 55 if p1.Name != "file" { 56 t.Fatalf("./file: Name=%q, want %q", p1.Name, "file") 57 } 58 dir := filepath.Clean("testdata/other/file") // Clean to use \ on Windows 59 if p1.Dir != dir { 60 t.Fatalf("./file: Dir=%q, want %q", p1.Name, dir) 61 } 62 } 63 64 func TestEmptyImport(t *testing.T) { 65 p, err := Import("", Default.GOROOT, FindOnly) 66 if err == nil { 67 t.Fatal(`Import("") returned nil error.`) 68 } 69 if p == nil { 70 t.Fatal(`Import("") returned nil package.`) 71 } 72 if p.ImportPath != "" { 73 t.Fatalf("ImportPath=%q, want %q.", p.ImportPath, "") 74 } 75 } 76 77 func TestLocalDirectory(t *testing.T) { 78 cwd, err := os.Getwd() 79 if err != nil { 80 t.Fatal(err) 81 } 82 83 p, err := ImportDir(cwd, 0) 84 if err != nil { 85 t.Fatal(err) 86 } 87 if p.ImportPath != "go/build" { 88 t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "go/build") 89 } 90 } 91 92 func TestShouldBuild(t *testing.T) { 93 const file1 = "// +build tag1\n\n" + 94 "package main\n" 95 96 const file2 = "// +build cgo\n\n" + 97 "// This package implements parsing of tags like\n" + 98 "// +build tag1\n" + 99 "package build" 100 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" + 104 "// +build tag\n" + 105 "func shouldBuild(content []byte)\n" 106 107 ctx := &Context{BuildTags: []string{"tag1"}} 108 if !ctx.shouldBuild([]byte(file1)) { 109 t.Errorf("should not build file1, expected the contrary") 110 } 111 if ctx.shouldBuild([]byte(file2)) { 112 t.Errorf("should build file2, expected the contrary") 113 } 114 115 ctx = &Context{BuildTags: nil} 116 if !ctx.shouldBuild([]byte(file3)) { 117 t.Errorf("should not build file3, expected the contrary") 118 } 119 }