Skip to content

Commit 94adf23

Browse files
authored
avoid cross-package references in tests (#385)
The previous implementation of DataFile (which abstracts different build systems' ways of locating test data files) constructed file names of data files using $GOPATH; also, it used a cross-package reference to locate the assert.star file. This doesn't work under Go modules. Now, DataFile looks only in the current directory, and the assert.star file is moved into a constant section of the executable. Also, fix tests historically broken on Mac/Windows due to diff and CR LF differences.
1 parent d12bc23 commit 94adf23

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

internal/chunkedfile/chunkedfile.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"fmt"
2929
"io/ioutil"
3030
"regexp"
31+
"runtime"
3132
"strconv"
3233
"strings"
3334
)
@@ -61,7 +62,13 @@ func Read(filename string, report Reporter) (chunks []Chunk) {
6162
return
6263
}
6364
linenum := 1
64-
for i, chunk := range strings.Split(string(data), "\n---\n") {
65+
66+
eol := "\n"
67+
if runtime.GOOS == "windows" {
68+
eol = "\r\n"
69+
}
70+
71+
for i, chunk := range strings.Split(string(data), eol+"---"+eol) {
6572
if debug {
6673
fmt.Printf("chunk %d at line %d: %s\n", i, linenum, chunk)
6774
}

internal/test.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ set -eu
1010
cp go.mod go.mod.orig
1111
cp go.sum go.sum.orig
1212
go mod tidy
13-
diff go.mod.orig go.mod
14-
diff go.sum.orig go.sum
13+
# Use -w to ignore differences in OS newlines.
14+
diff -w go.mod.orig go.mod
15+
diff -w go.sum.orig go.sum
1516
rm go.mod.orig go.sum.orig
1617

1718
# Run tests

starlarktest/assert.star renamed to starlarktest/assert.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
package starlarktest
2+
3+
// assertStar contains the logical data file assert.star.
4+
// TODO(adonovan): move it back into an actual file,
5+
// when fs.Embed is more than two releases old.
6+
const assertStar = `
17
# Predeclared built-ins for this module:
28
#
39
# error(msg): report an error in Go's test framework without halting execution.
@@ -49,3 +55,4 @@ assert = module(
4955
contains = _contains,
5056
fails = _fails,
5157
)
58+
`

starlarktest/starlarktest.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ package starlarktest // import "go.starlark.net/starlarktest"
1414

1515
import (
1616
"fmt"
17-
"go/build"
1817
"os"
1918
"path/filepath"
2019
"regexp"
@@ -68,12 +67,9 @@ func LoadAssertModule() (starlark.StringDict, error) {
6867
"_freeze": starlark.NewBuiltin("freeze", freeze),
6968
}
7069
// TODO(adonovan): embed the file using embed.FS when we can rely on go1.16,
71-
// and stop using DataFile. Otherwise this reaches from the application's working
72-
// directory into the starlarktest package's directory, which is not compatible
73-
// with modules.
74-
filename := DataFile("starlarktest", "assert.star")
70+
// and make the apparent filename reference that file.
7571
thread := new(starlark.Thread)
76-
assert, assertErr = starlark.ExecFile(thread, filename, nil, predeclared)
72+
assert, assertErr = starlark.ExecFile(thread, "builtins/assert.star", assertStar, predeclared)
7773
})
7874
return assert, assertErr
7975
}
@@ -147,5 +143,7 @@ var DataFile = func(pkgdir, filename string) string {
147143
return filepath.Join(testSrcdir, "net_starlark_go", pkgdir, filename)
148144
}
149145

150-
return filepath.Join(build.Default.GOPATH, "src/go.starlark.net", pkgdir, filename)
146+
// Under go test, ignore pkgdir, which is the directory of the
147+
// current package relative to the module root.
148+
return filename
151149
}

0 commit comments

Comments
 (0)