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.
18 // Unit tests for internal guru functions
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
29 // Create a temporary home directory under /tmp
30 home, err := ioutil.TempDir(os.TempDir(), "home")
32 t.Errorf("Unable to create a temporary directory in %s", os.TempDir())
35 defer os.RemoveAll(home)
37 // create filepath /tmp/home/go/src/test/test.go
38 if err = os.MkdirAll(home+"/go/src/test", 0755); err != nil {
42 var buildContext = build.Default
45 type SuccessTest struct {
46 gopath, filename, wantSrcdir string
49 successTests := []SuccessTest{
50 {home + "/go", home + "/go/src/test/test.go", filepath.FromSlash(home + "/go/src")},
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 {
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")},
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")
75 // Function to format expected error message
76 errFormat := func(fpath string) string {
77 return fmt.Sprintf("can't evaluate symlinks of %s", fpath)
81 type FailTest struct {
82 gopath, filename, wantErr string
85 failTests := []FailTest{
86 {home + "/go", home + "/go/src/fake/test.go", errFormat(filepath.FromSlash(home + "/go/src/fake"))},
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"))},
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)