Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make default DataFile work with GOPATH with multiple entries. #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion starlarktest/starlarktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"bytes"
"fmt"
"go/build"
"os"
"path/filepath"
"regexp"
"sync"
Expand Down Expand Up @@ -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))
}
10 changes: 1 addition & 9 deletions syntax/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"bufio"
"bytes"
"fmt"
"go/build"
"io/ioutil"
"path/filepath"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -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")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already used by this file in TestParseErrors above.

b.StopTimer()
data, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion syntax/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
Expand Down Expand Up @@ -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) {
Expand Down