github.com/april1989/origin-go-tools@v0.0.32/cmd/guru/unit_test.go (about) 1 // Copyright 2013 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 main 6 7 import ( 8 "fmt" 9 "go/build" 10 "io/ioutil" 11 "os" 12 "path/filepath" 13 "runtime" 14 "strings" 15 "testing" 16 ) 17 18 // Unit tests for internal guru functions 19 20 func TestIssue17515(t *testing.T) { 21 // Tests handling of symlinks in function guessImportPath 22 // If we have Go code inside $HOME/go/src and create a symlink $HOME/src to it 23 // there are 4 possible cases that need to be tested: 24 // (1) absolute & absolute: GOPATH=$HOME/go/src file=$HOME/go/src/test/test.go 25 // (2) absolute & symlink: GOPATH=$HOME/go/src file=$HOME/src/test/test.go 26 // (3) symlink & symlink: GOPATH=$HOME/src file=$HOME/src/test/test.go 27 // (4) symlink & absolute: GOPATH=$HOME/src file= $HOME/go/src/test/test.go 28 29 // Create a temporary home directory under /tmp 30 home, err := ioutil.TempDir(os.TempDir(), "home") 31 if err != nil { 32 t.Errorf("Unable to create a temporary directory in %s", os.TempDir()) 33 } 34 35 defer os.RemoveAll(home) 36 37 // create filepath /tmp/home/go/src/test/test.go 38 if err = os.MkdirAll(home+"/go/src/test", 0755); err != nil { 39 t.Fatal(err) 40 } 41 42 var buildContext = build.Default 43 44 // Success test cases 45 type SuccessTest struct { 46 gopath, filename, wantSrcdir string 47 } 48 49 successTests := []SuccessTest{ 50 {home + "/go", home + "/go/src/test/test.go", filepath.FromSlash(home + "/go/src")}, 51 } 52 53 // Add symlink cases if not on Windows, Plan 9 54 if runtime.GOOS != "windows" && runtime.GOOS != "plan9" { 55 // symlink between /tmp/home/go/src and /tmp/home/src 56 if err := os.Symlink(home+"/go/src", home+"/src"); err != nil { 57 t.Fatal(err) 58 } 59 60 successTests = append(successTests, []SuccessTest{ 61 {home + "/go", home + "/src/test/test.go", filepath.FromSlash(home + "/go/src")}, 62 {home, home + "/go/src/test/test.go", filepath.FromSlash(home + "/src")}, 63 {home, home + "/src/test/test.go", filepath.FromSlash(home + "/src")}, 64 }...) 65 } 66 67 for _, test := range successTests { 68 buildContext.GOPATH = test.gopath 69 srcdir, importPath, err := guessImportPath(test.filename, &buildContext) 70 if srcdir != test.wantSrcdir || importPath != "test" || err != nil { 71 t.Errorf("guessImportPath(%q, %q) = %q, %q, %q; want %q, %q, %q", 72 test.filename, test.gopath, srcdir, importPath, err, test.wantSrcdir, "test", "nil") 73 } 74 } 75 // Function to format expected error message 76 errFormat := func(fpath string) string { 77 return fmt.Sprintf("can't evaluate symlinks of %s", fpath) 78 } 79 80 // Failure test cases 81 type FailTest struct { 82 gopath, filename, wantErr string 83 } 84 85 failTests := []FailTest{ 86 {home + "/go", home + "/go/src/fake/test.go", errFormat(filepath.FromSlash(home + "/go/src/fake"))}, 87 } 88 89 if runtime.GOOS != "windows" && runtime.GOOS != "plan9" { 90 failTests = append(failTests, []FailTest{ 91 {home + "/go", home + "/src/fake/test.go", errFormat(filepath.FromSlash(home + "/src/fake"))}, 92 {home, home + "/src/fake/test.go", errFormat(filepath.FromSlash(home + "/src/fake"))}, 93 {home, home + "/go/src/fake/test.go", errFormat(filepath.FromSlash(home + "/go/src/fake"))}, 94 }...) 95 } 96 97 for _, test := range failTests { 98 buildContext.GOPATH = test.gopath 99 srcdir, importPath, err := guessImportPath(test.filename, &buildContext) 100 if !strings.HasPrefix(fmt.Sprint(err), test.wantErr) { 101 t.Errorf("guessImportPath(%q, %q) = %q, %q, %q; want %q, %q, %q", 102 test.filename, test.gopath, srcdir, importPath, err, "", "", test.wantErr) 103 } 104 } 105 }