1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:44:43 -07:00

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>
This commit is contained in:
Rebecca Stambler 2016-12-05 15:37:24 -05:00 committed by Alan Donovan
parent 3a9a2cbbc4
commit c945ee3be4
3 changed files with 66 additions and 42 deletions

View File

@ -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) {

View File

@ -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)

View File

@ -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