Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.
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
4 changes: 2 additions & 2 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestEvalExpr(t *testing.T) {
}

func TestExecFile(t *testing.T) {
testdata := skylarktest.DataFile("skylark", ".")
testdata := skylarktest.DataFile("", ".")
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't remove the "skylark" component of the package name each call. Just because something is common to every call site does not mean it is the callee's responsibility to do it.

Copy link
Author

Choose a reason for hiding this comment

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

My reasoning is not that it is common.
The reasoning is that these arguments are a lie (see DataFile signature). It is not the package name as the callee suggests but a subpackage name at best. Overall DataFile builds a special constructed directory structure, which exclusively works with go build.
It does not work with Bazel and might not work for Buck, Gradle or SCons.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. I was tempted to suggest that the Bazel DataFile concatenate "com_github_google_"+subdir, but what you have seems fine.

thread := &skylark.Thread{Load: load}
skylarktest.SetReporter(thread, t)
for _, file := range []string{
Expand Down Expand Up @@ -342,7 +342,7 @@ f()
}

func Benchmark(b *testing.B) {
testdata := skylarktest.DataFile("skylark", ".")
testdata := skylarktest.DataFile("", ".")
thread := new(skylark.Thread)
for _, file := range []string{
"testdata/benchmark.sky",
Expand Down
2 changes: 1 addition & 1 deletion resolve/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestResolve(t *testing.T) {
filename := skylarktest.DataFile("skylark/resolve", "testdata/resolve.sky")
filename := skylarktest.DataFile("resolve", "testdata/resolve.sky")
for _, chunk := range chunkedfile.Read(filename, t) {
f, err := syntax.Parse(filename, chunk.Source, 0)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion skylarkstruct/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
}

func Test(t *testing.T) {
testdata := skylarktest.DataFile("skylark/skylarkstruct", ".")
testdata := skylarktest.DataFile("skylarkstruct", ".")
thread := &skylark.Thread{Load: load}
skylarktest.SetReporter(thread, t)
filename := filepath.Join(testdata, "testdata/struct.sky")
Expand Down
25 changes: 25 additions & 0 deletions skylarktest/bazeltest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// +build bazel

// Copyright 2018 The Bazel Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Rebinds DataFile, so that it builds data paths as expected by Bazel.

package skylarktest

import (
"os"
"path/filepath"
)

func init() {
testDir := os.Getenv("TEST_SRCDIR")
DataFile = func(subdir, filename string) string {
// Late check testDir, to only panic when we actually need it.
if testDir == "" {
panic("Environment variable TEST_SRCDIR unset or empty.")
}
return filepath.Join(testDir, "com_github_google_skylark", subdir, filename)
}
}
14 changes: 8 additions & 6 deletions skylarktest/skylarktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func LoadAssertModule() (skylark.StringDict, error) {
"struct": skylark.NewBuiltin("struct", skylarkstruct.Make),
"freeze": skylark.NewBuiltin("freeze", freeze),
}
filename := DataFile("skylark/skylarktest", "assert.sky")
filename := DataFile("skylarktest", "assert.sky")
thread := new(skylark.Thread)
assertErr = skylark.ExecFile(thread, filename, nil, globals)
// Expose only these items:
Expand Down Expand Up @@ -134,9 +134,11 @@ func freeze(thread *skylark.Thread, _ *skylark.Builtin, args skylark.Tuple, kwar
}

// DataFile returns the effective filename of the specified
// test data resource. The function abstracts differences between
// '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/github.com/google", pkgdir, filename)
// test data resource. The function abstracts differences between
// - 'go build' (under which a test runs in its package directory),
// - Blaze (under which a test runs in the root of the tree) and
// - Bazel/rules_go (under which a test runs in a different package directory)
// The default behavior works for 'go build'.
var DataFile = func(subdir, filename string) string {
return filepath.Join(build.Default.GOPATH, "src/github.com/google/skylark", subdir, filename)
}
2 changes: 1 addition & 1 deletion syntax/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func writeTree(out *bytes.Buffer, x reflect.Value) {
}

func TestParseErrors(t *testing.T) {
filename := skylarktest.DataFile("skylark/syntax", "testdata/errors.sky")
filename := skylarktest.DataFile("syntax", "testdata/errors.sky")
for _, chunk := range chunkedfile.Read(filename, t) {
_, err := syntax.Parse(filename, chunk.Source, 0)
switch err := err.(type) {
Expand Down