github.com/muesli/go@v0.0.0-20170208044820-e410d2a81ef2/src/cmd/go/internal/work/build_test.go (about) 1 // Copyright 2016 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 work 6 7 import ( 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "reflect" 12 "testing" 13 14 "cmd/go/internal/base" 15 "cmd/go/internal/cfg" 16 "cmd/go/internal/load" 17 ) 18 19 func TestRemoveDevNull(t *testing.T) { 20 fi, err := os.Lstat(os.DevNull) 21 if err != nil { 22 t.Skip(err) 23 } 24 if fi.Mode().IsRegular() { 25 t.Errorf("Lstat(%s).Mode().IsRegular() = true; expected false", os.DevNull) 26 } 27 mayberemovefile(os.DevNull) 28 _, err = os.Lstat(os.DevNull) 29 if err != nil { 30 t.Errorf("mayberemovefile(%s) did remove it; oops", os.DevNull) 31 } 32 } 33 34 func TestSplitPkgConfigOutput(t *testing.T) { 35 for _, test := range []struct { 36 in []byte 37 want []string 38 }{ 39 {[]byte(`-r:foo -L/usr/white\ space/lib -lfoo\ bar -lbar\ baz`), []string{"-r:foo", "-L/usr/white space/lib", "-lfoo bar", "-lbar baz"}}, 40 {[]byte(`-lextra\ fun\ arg\\`), []string{`-lextra fun arg\`}}, 41 {[]byte(`broken flag\`), []string{"broken", "flag"}}, 42 {[]byte("\textra whitespace\r\n"), []string{"extra", "whitespace"}}, 43 {[]byte(" \r\n "), nil}, 44 } { 45 got := splitPkgConfigOutput(test.in) 46 if !reflect.DeepEqual(got, test.want) { 47 t.Errorf("splitPkgConfigOutput(%v) = %v; want %v", test.in, got, test.want) 48 } 49 } 50 } 51 52 func TestSharedLibName(t *testing.T) { 53 // TODO(avdva) - make these values platform-specific 54 prefix := "lib" 55 suffix := ".so" 56 testData := []struct { 57 args []string 58 pkgs []*load.Package 59 expected string 60 expectErr bool 61 rootedAt string 62 }{ 63 { 64 args: []string{"std"}, 65 pkgs: []*load.Package{}, 66 expected: "std", 67 }, 68 { 69 args: []string{"std", "cmd"}, 70 pkgs: []*load.Package{}, 71 expected: "std,cmd", 72 }, 73 { 74 args: []string{}, 75 pkgs: []*load.Package{pkgImportPath("gopkg.in/somelib")}, 76 expected: "gopkg.in-somelib", 77 }, 78 { 79 args: []string{"./..."}, 80 pkgs: []*load.Package{pkgImportPath("somelib")}, 81 expected: "somelib", 82 rootedAt: "somelib", 83 }, 84 { 85 args: []string{"../somelib", "../somelib"}, 86 pkgs: []*load.Package{pkgImportPath("somelib")}, 87 expected: "somelib", 88 }, 89 { 90 args: []string{"../lib1", "../lib2"}, 91 pkgs: []*load.Package{pkgImportPath("gopkg.in/lib1"), pkgImportPath("gopkg.in/lib2")}, 92 expected: "gopkg.in-lib1,gopkg.in-lib2", 93 }, 94 { 95 args: []string{"./..."}, 96 pkgs: []*load.Package{ 97 pkgImportPath("gopkg.in/dir/lib1"), 98 pkgImportPath("gopkg.in/lib2"), 99 pkgImportPath("gopkg.in/lib3"), 100 }, 101 expected: "gopkg.in", 102 rootedAt: "gopkg.in", 103 }, 104 { 105 args: []string{"std", "../lib2"}, 106 pkgs: []*load.Package{}, 107 expectErr: true, 108 }, 109 { 110 args: []string{"all", "./"}, 111 pkgs: []*load.Package{}, 112 expectErr: true, 113 }, 114 { 115 args: []string{"cmd", "fmt"}, 116 pkgs: []*load.Package{}, 117 expectErr: true, 118 }, 119 } 120 for _, data := range testData { 121 func() { 122 if data.rootedAt != "" { 123 tmpGopath, err := ioutil.TempDir("", "gopath") 124 if err != nil { 125 t.Fatal(err) 126 } 127 oldGopath := cfg.BuildContext.GOPATH 128 defer func() { 129 cfg.BuildContext.GOPATH = oldGopath 130 os.Chdir(base.Cwd) 131 err := os.RemoveAll(tmpGopath) 132 if err != nil { 133 t.Error(err) 134 } 135 }() 136 root := filepath.Join(tmpGopath, "src", data.rootedAt) 137 err = os.MkdirAll(root, 0755) 138 if err != nil { 139 t.Fatal(err) 140 } 141 cfg.BuildContext.GOPATH = tmpGopath 142 os.Chdir(root) 143 } 144 computed, err := libname(data.args, data.pkgs) 145 if err != nil { 146 if !data.expectErr { 147 t.Errorf("libname returned an error %q, expected a name", err.Error()) 148 } 149 } else if data.expectErr { 150 t.Errorf("libname returned %q, expected an error", computed) 151 } else { 152 expected := prefix + data.expected + suffix 153 if expected != computed { 154 t.Errorf("libname returned %q, expected %q", computed, expected) 155 } 156 } 157 }() 158 } 159 } 160 161 func pkgImportPath(pkgpath string) *load.Package { 162 return &load.Package{ 163 PackagePublic: load.PackagePublic{ 164 ImportPath: pkgpath, 165 }, 166 } 167 }