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 <adonovan@google.com>
diff --git a/cmd/guru/unit_test.go b/cmd/guru/unit_test.go
index 45678ff..588cb43 100644
--- a/cmd/guru/unit_test.go
+++ b/cmd/guru/unit_test.go
@@ -9,6 +9,7 @@
"go/build"
"io/ioutil"
"os"
+ "runtime"
"strings"
"testing"
)
@@ -35,25 +36,34 @@
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 @@
}
// 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 c2d2843..111989e 100644
--- a/go/buildutil/util.go
+++ b/go/buildutil/util.go
@@ -68,7 +68,7 @@
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 148214d..efea63c 100644
--- a/go/buildutil/util_test.go
+++ b/go/buildutil/util_test.go
@@ -24,35 +24,40 @@
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