From 64f9b0cc910c1f1bb99b746663866f16c816953b Mon Sep 17 00:00:00 2001 From: Vadim Shtayura Date: Thu, 31 Jan 2019 22:41:36 -0800 Subject: [PATCH] Make default DataFile work with GOPATH with multiple entries. --- starlarktest/starlarktest.go | 10 +++++++++- syntax/parse_test.go | 10 +--------- syntax/scan_test.go | 10 +++++++++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/starlarktest/starlarktest.go b/starlarktest/starlarktest.go index 5436d225..a8ca851f 100644 --- a/starlarktest/starlarktest.go +++ b/starlarktest/starlarktest.go @@ -16,6 +16,7 @@ import ( "bytes" "fmt" "go/build" + "os" "path/filepath" "regexp" "sync" @@ -133,5 +134,12 @@ func freeze(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, k // 'go build', under which a test runs in its package directory, // and Blaze, under which a test runs in the root of the tree. var DataFile = func(pkgdir, filename string) string { - return filepath.Join(build.Default.GOPATH, "src/go.starlark.net", pkgdir, filename) + rel := filepath.Join("go.starlark.net", pkgdir, filename) + for _, p := range build.Default.SrcDirs() { + full := filepath.Join(p, rel) + if _, err := os.Stat(full); err == nil { + return full + } + } + panic(fmt.Sprintf("could not find %s", rel)) } diff --git a/syntax/parse_test.go b/syntax/parse_test.go index 309e249c..9aa71ee0 100644 --- a/syntax/parse_test.go +++ b/syntax/parse_test.go @@ -8,9 +8,7 @@ import ( "bufio" "bytes" "fmt" - "go/build" "io/ioutil" - "path/filepath" "reflect" "strings" "testing" @@ -481,14 +479,8 @@ File } } -// dataFile is the same as starlarktest.DataFile. -// We make a copy to avoid a dependency cycle. -var dataFile = func(pkgdir, filename string) string { - return filepath.Join(build.Default.GOPATH, "src/go.starlark.net", pkgdir, filename) -} - func BenchmarkParse(b *testing.B) { - filename := dataFile("syntax", "testdata/scan.star") + filename := starlarktest.DataFile("syntax", "testdata/scan.star") b.StopTimer() data, err := ioutil.ReadFile(filename) if err != nil { diff --git a/syntax/scan_test.go b/syntax/scan_test.go index 06f42dbd..6861e9cf 100644 --- a/syntax/scan_test.go +++ b/syntax/scan_test.go @@ -9,6 +9,7 @@ import ( "fmt" "go/build" "io/ioutil" + "os" "path/filepath" "testing" ) @@ -228,7 +229,14 @@ pass`, "pass newline pass EOF"}, // consecutive newlines are consolidated // dataFile is the same as starlarktest.DataFile. // We make a copy to avoid a dependency cycle. var dataFile = func(pkgdir, filename string) string { - return filepath.Join(build.Default.GOPATH, "src/go.starlark.net", pkgdir, filename) + rel := filepath.Join("go.starlark.net", pkgdir, filename) + for _, p := range build.Default.SrcDirs() { + full := filepath.Join(p, rel) + if _, err := os.Stat(full); err == nil { + return full + } + } + panic(fmt.Sprintf("could not find %s", rel)) } func BenchmarkScan(b *testing.B) {