From c945ee3be47cb14cbc5e48ff0572d967d772d8b9 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Mon, 5 Dec 2016 15:37:24 -0500 Subject: [PATCH] go/buildutil, cmd/guru: fix tests for symlinks in guru and gorename to account for windows Restructure tests to account for possibility of being run on Windows (which doesn't handle symlinks). Change-Id: I428db26c9a1aad337d8972baa2b71468be3a2e58 Reviewed-on: https://go-review.googlesource.com/33920 Reviewed-by: Alan Donovan --- cmd/guru/unit_test.go | 55 ++++++++++++++++++++++++++------------- go/buildutil/util.go | 2 +- go/buildutil/util_test.go | 51 ++++++++++++++++++++---------------- 3 files changed, 66 insertions(+), 42 deletions(-) diff --git a/cmd/guru/unit_test.go b/cmd/guru/unit_test.go index 45678ff9..588cb431 100644 --- a/cmd/guru/unit_test.go +++ b/cmd/guru/unit_test.go @@ -9,6 +9,7 @@ import ( "go/build" "io/ioutil" "os" + "runtime" "strings" "testing" ) @@ -35,25 +36,34 @@ 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 - for _, test := range []struct { + type SuccessTest struct { gopath, filename, wantSrcdir string - }{ + } + + successTests := []SuccessTest{ {home + "/go", home + "/go/src/test/test.go", home + "/go/src"}, - {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"}, - } { + } + + // 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 { buildContext.GOPATH = test.gopath srcdir, importPath, err := guessImportPath(test.filename, &buildContext) if srcdir != test.wantSrcdir || importPath != "test" || err != nil { @@ -67,14 +77,23 @@ func TestIssue17515(t *testing.T) { } // Failure test cases - for _, test := range []struct { + type FailTest struct { gopath, filename, wantErr string - }{ + } + + failTests := []FailTest{ {home + "/go", home + "/go/src/fake/test.go", errFormat(home + "/go/src/fake")}, - {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")}, - } { + } + + 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 { 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 c2d28431..111989e4 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", path.Dir(filename), err) + return nil, fmt.Errorf("can't evaluate symlinks of %s: %v", filepath.Dir(filename), err) } resolvedDir := filepath.ToSlash(resolvedFilename) diff --git a/go/buildutil/util_test.go b/go/buildutil/util_test.go index 148214dc..efea63c5 100644 --- a/go/buildutil/util_test.go +++ b/go/buildutil/util_test.go @@ -24,35 +24,40 @@ func TestContainingPackage(t *testing.T) { goroot := runtime.GOROOT() gopath := filepath.SplitList(os.Getenv("GOPATH"))[0] - // 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()) - } - - // 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 { + type Test struct { gopath, filename, wantPkg string - }{ + } + + tests := []Test{ {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"}, - {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"}, - } { + } + + 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 { var got string var buildContext = build.Default buildContext.GOPATH = test.gopath