diff --git a/cmd/guru/unit_test.go b/cmd/guru/unit_test.go index 588cb43130..45678ff939 100644 --- a/cmd/guru/unit_test.go +++ b/cmd/guru/unit_test.go @@ -9,7 +9,6 @@ import ( "go/build" "io/ioutil" "os" - "runtime" "strings" "testing" ) @@ -36,34 +35,25 @@ func TestIssue17515(t *testing.T) { t.Fatal(err) } + // symlink between /tmp/home/go/src and /tmp/home/src + if err = os.Symlink(home+"/go/src", home+"/src"); err != nil { + t.Fatal(err) + } + + // Defer tear down (removing files, symlinks) defer os.RemoveAll(home) var buildContext = build.Default // Success test cases - type SuccessTest struct { + for _, test := range []struct { gopath, filename, wantSrcdir string - } - - successTests := []SuccessTest{ + }{ {home + "/go", home + "/go/src/test/test.go", home + "/go/src"}, - } - - // Add symlink cases if not on windows - if runtime.GOOS != "windows" { - // symlink between /tmp/home/go/src and /tmp/home/src - if err := os.Symlink(home+"/go/src", home+"/src"); err != nil { - t.Fatal(err) - } - - successTests = append(successTests, []SuccessTest{ - {home + "/go", home + "/src/test/test.go", home + "/go/src"}, - {home, home + "/go/src/test/test.go", home + "/src"}, - {home, home + "/src/test/test.go", home + "/src"}, - }...) - } - - for _, test := range successTests { + {home + "/go", home + "/src/test/test.go", home + "/go/src"}, + {home, home + "/src/test/test.go", home + "/src"}, + {home, home + "/go/src/test/test.go", home + "/src"}, + } { buildContext.GOPATH = test.gopath srcdir, importPath, err := guessImportPath(test.filename, &buildContext) if srcdir != test.wantSrcdir || importPath != "test" || err != nil { @@ -77,23 +67,14 @@ func TestIssue17515(t *testing.T) { } // Failure test cases - type FailTest struct { + for _, test := range []struct { gopath, filename, wantErr string - } - - failTests := []FailTest{ + }{ {home + "/go", home + "/go/src/fake/test.go", errFormat(home + "/go/src/fake")}, - } - - if runtime.GOOS != "windows" { - failTests = append(failTests, []FailTest{ - {home + "/go", home + "/src/fake/test.go", errFormat(home + "/src/fake")}, - {home, home + "/src/fake/test.go", errFormat(home + "/src/fake")}, - {home, home + "/go/src/fake/test.go", errFormat(home + "/go/src/fake")}, - }...) - } - - for _, test := range failTests { + {home + "/go", home + "/src/fake/test.go", errFormat(home + "/src/fake")}, + {home, home + "/src/fake/test.go", errFormat(home + "/src/fake")}, + {home, home + "/go/src/fake/test.go", errFormat(home + "/go/src/fake")}, + } { buildContext.GOPATH = test.gopath srcdir, importPath, err := guessImportPath(test.filename, &buildContext) if !strings.HasPrefix(fmt.Sprint(err), test.wantErr) { diff --git a/go/buildutil/util.go b/go/buildutil/util.go index 111989e40d..c2d28431c0 100644 --- a/go/buildutil/util.go +++ b/go/buildutil/util.go @@ -68,7 +68,7 @@ func ContainingPackage(ctxt *build.Context, dir, filename string) (*build.Packag resolvedFilename, err := filepath.EvalSymlinks(filepath.Dir(filename)) if err != nil { - return nil, fmt.Errorf("can't evaluate symlinks of %s: %v", filepath.Dir(filename), err) + return nil, fmt.Errorf("can't evaluate symlinks of %s: %v", path.Dir(filename), err) } resolvedDir := filepath.ToSlash(resolvedFilename) diff --git a/go/buildutil/util_test.go b/go/buildutil/util_test.go index efea63c5cf..148214dcc4 100644 --- a/go/buildutil/util_test.go +++ b/go/buildutil/util_test.go @@ -24,40 +24,35 @@ func TestContainingPackage(t *testing.T) { goroot := runtime.GOROOT() gopath := filepath.SplitList(os.Getenv("GOPATH"))[0] - type Test struct { - gopath, filename, wantPkg string + // Make a symlink to gopath for test + tmp, err := ioutil.TempDir(os.TempDir(), "go") + if err != nil { + t.Errorf("Unable to create a temporary directory in %s", os.TempDir()) } - tests := []Test{ + // symlink between $GOPATH/src and /tmp/go/src + // in order to test all possible symlink cases + if err := os.Symlink(gopath+"/src", tmp+"/src"); err != nil { + t.Fatal(err) + } + + defer os.RemoveAll(tmp) + + for _, test := range []struct { + gopath, filename, wantPkg string + }{ {gopath, goroot + "/src/fmt/print.go", "fmt"}, {gopath, goroot + "/src/encoding/json/foo.go", "encoding/json"}, {gopath, goroot + "/src/encoding/missing/foo.go", "(not found)"}, {gopath, gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", "golang.org/x/tools/go/buildutil"}, - } - - if runtime.GOOS != "windows" { - // Make a symlink to gopath for test - tmp, err := ioutil.TempDir(os.TempDir(), "go") - if err != nil { - t.Errorf("Unable to create a temporary directory in %s", os.TempDir()) - } - - defer os.RemoveAll(tmp) - - // symlink between $GOPATH/src and /tmp/go/src - // in order to test all possible symlink cases - if err := os.Symlink(gopath+"/src", tmp+"/src"); err != nil { - t.Fatal(err) - } - tests = append(tests, []Test{ - {gopath, tmp + "/src/golang.org/x/tools/go/buildutil/util_test.go", "golang.org/x/tools/go/buildutil"}, - {tmp, gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", "golang.org/x/tools/go/buildutil"}, - {tmp, tmp + "/src/golang.org/x/tools/go/buildutil/util_test.go", "golang.org/x/tools/go/buildutil"}, - }...) - } - - for _, test := range tests { + {gopath, tmp + "/src/golang.org/x/tools/go/buildutil/util_test.go", + "golang.org/x/tools/go/buildutil"}, + {tmp, gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", + "golang.org/x/tools/go/buildutil"}, + {tmp, tmp + "/src/golang.org/x/tools/go/buildutil/util_test.go", + "golang.org/x/tools/go/buildutil"}, + } { var got string var buildContext = build.Default buildContext.GOPATH = test.gopath