github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/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 "reflect" 11 "runtime" 12 "testing" 13 ) 14 15 func TestMatch(t *testing.T) { 16 ctxt := Default 17 what := "default" 18 match := func(tag string, want map[string]bool) { 19 m := make(map[string]bool) 20 if !ctxt.match(tag, m) { 21 t.Errorf("%s context should match %s, does not", what, tag) 22 } 23 if !reflect.DeepEqual(m, want) { 24 t.Errorf("%s tags = %v, want %v", tag, m, want) 25 } 26 } 27 nomatch := func(tag string, want map[string]bool) { 28 m := make(map[string]bool) 29 if ctxt.match(tag, m) { 30 t.Errorf("%s context should NOT match %s, does", what, tag) 31 } 32 if !reflect.DeepEqual(m, want) { 33 t.Errorf("%s tags = %v, want %v", tag, m, want) 34 } 35 } 36 37 match(runtime.GOOS+","+runtime.GOARCH, map[string]bool{runtime.GOOS: true, runtime.GOARCH: true}) 38 match(runtime.GOOS+","+runtime.GOARCH+",!foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 39 nomatch(runtime.GOOS+","+runtime.GOARCH+",foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 40 41 what = "modified" 42 ctxt.BuildTags = []string{"foo"} 43 match(runtime.GOOS+","+runtime.GOARCH, map[string]bool{runtime.GOOS: true, runtime.GOARCH: true}) 44 match(runtime.GOOS+","+runtime.GOARCH+",foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 45 nomatch(runtime.GOOS+","+runtime.GOARCH+",!foo", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "foo": true}) 46 match(runtime.GOOS+","+runtime.GOARCH+",!bar", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "bar": true}) 47 nomatch(runtime.GOOS+","+runtime.GOARCH+",bar", map[string]bool{runtime.GOOS: true, runtime.GOARCH: true, "bar": true}) 48 nomatch("!", map[string]bool{}) 49 } 50 51 func TestDotSlashImport(t *testing.T) { 52 p, err := ImportDir("testdata/other", 0) 53 if err != nil { 54 t.Fatal(err) 55 } 56 if len(p.Imports) != 1 || p.Imports[0] != "./file" { 57 t.Fatalf("testdata/other: Imports=%v, want [./file]", p.Imports) 58 } 59 60 p1, err := Import("./file", "testdata/other", 0) 61 if err != nil { 62 t.Fatal(err) 63 } 64 if p1.Name != "file" { 65 t.Fatalf("./file: Name=%q, want %q", p1.Name, "file") 66 } 67 dir := filepath.Clean("testdata/other/file") // Clean to use \ on Windows 68 if p1.Dir != dir { 69 t.Fatalf("./file: Dir=%q, want %q", p1.Name, dir) 70 } 71 } 72 73 func TestEmptyImport(t *testing.T) { 74 p, err := Import("", Default.GOROOT, FindOnly) 75 if err == nil { 76 t.Fatal(`Import("") returned nil error.`) 77 } 78 if p == nil { 79 t.Fatal(`Import("") returned nil package.`) 80 } 81 if p.ImportPath != "" { 82 t.Fatalf("ImportPath=%q, want %q.", p.ImportPath, "") 83 } 84 } 85 86 func TestLocalDirectory(t *testing.T) { 87 cwd, err := os.Getwd() 88 if err != nil { 89 t.Fatal(err) 90 } 91 92 p, err := ImportDir(cwd, 0) 93 if err != nil { 94 t.Fatal(err) 95 } 96 if p.ImportPath != "go/build" { 97 t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "go/build") 98 } 99 } 100 101 func TestShouldBuild(t *testing.T) { 102 const file1 = "// +build tag1\n\n" + 103 "package main\n" 104 want1 := map[string]bool{"tag1": true} 105 106 const file2 = "// +build cgo\n\n" + 107 "// This package implements parsing of tags like\n" + 108 "// +build tag1\n" + 109 "package build" 110 want2 := map[string]bool{"cgo": true} 111 112 const file3 = "// Copyright The Go Authors.\n\n" + 113 "package build\n\n" + 114 "// shouldBuild checks tags given by lines of the form\n" + 115 "// +build tag\n" + 116 "func shouldBuild(content []byte)\n" 117 want3 := map[string]bool{} 118 119 ctx := &Context{BuildTags: []string{"tag1"}} 120 m := map[string]bool{} 121 if !ctx.shouldBuild([]byte(file1), m) { 122 t.Errorf("shouldBuild(file1) = false, want true") 123 } 124 if !reflect.DeepEqual(m, want1) { 125 t.Errorf("shoudBuild(file1) tags = %v, want %v", m, want1) 126 } 127 128 m = map[string]bool{} 129 if ctx.shouldBuild([]byte(file2), m) { 130 t.Errorf("shouldBuild(file2) = true, want fakse") 131 } 132 if !reflect.DeepEqual(m, want2) { 133 t.Errorf("shoudBuild(file2) tags = %v, want %v", m, want2) 134 } 135 136 m = map[string]bool{} 137 ctx = &Context{BuildTags: nil} 138 if !ctx.shouldBuild([]byte(file3), m) { 139 t.Errorf("shouldBuild(file3) = false, want true") 140 } 141 if !reflect.DeepEqual(m, want3) { 142 t.Errorf("shoudBuild(file3) tags = %v, want %v", m, want3) 143 } 144 }