Skip to content

Commit d0d69c0

Browse files
committed
tests: Add test that GopherJS can be vendored.
Rely on runtime.GOARCH and t.Skip to skip tests that shouldn't be run with GopherJS. It's more clear and reliable than a file-wide build tag. Include stderr from gopherjsvendored_test.sh in output of TestGopherJSCanBeVendored. This is only to get get more information when it fails.
1 parent b24e356 commit d0d69c0

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

tests/gopherjsvendored_test.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/sh
2+
# Don't run this file directly. It's executed as part of TestGopherJSCanBeVendored.
3+
4+
set -e
5+
6+
tmp=$(mktemp -d "${TMPDIR:-/tmp}/gopherjsvendored_test.XXXXXXXXXX")
7+
8+
cleanup() {
9+
rm -rf "$tmp"
10+
exit
11+
}
12+
13+
trap cleanup EXIT HUP INT TERM
14+
15+
# copyGoPackage copies Go package with import path $1 to directory $2.
16+
# The target directory is created if it doesn't exist.
17+
copyGoPackage() {
18+
mkdir -p "$2"
19+
pkgDir=$(go list -f '{{.Dir}}' "$1")
20+
# Copy all files (not directories), other than ones that start with "." or "_".
21+
for f in $(find -H "$pkgDir" -maxdepth 1 -name "[^._]*" -type f); do
22+
cp "$f" "$2"
23+
done
24+
}
25+
26+
# Make a hello project that will vendor GopherJS.
27+
mkdir -p "$tmp/src/example.org/hello"
28+
echo 'package main
29+
30+
import "github.com/gopherjs/gopherjs/js"
31+
32+
func main() {
33+
js.Global.Get("console").Call("log", "hello using js pkg")
34+
}' > "$tmp/src/example.org/hello/main.go"
35+
36+
# Vendor GopherJS and its dependencies into hello project.
37+
for pkg in $(go list -f '{{if not .Goroot}}{{.ImportPath}}{{end}}' $(go list -f '{{.ImportPath}} {{join .Deps " "}}' github.com/gopherjs/gopherjs)); do
38+
copyGoPackage "$pkg" "$tmp/src/example.org/hello/vendor/$pkg"
39+
done
40+
41+
# Make $tmp our GOPATH workspace.
42+
export GOPATH="$tmp"
43+
44+
# Build the vendored copy of GopherJS.
45+
go install example.org/hello/vendor/github.com/gopherjs/gopherjs
46+
47+
# Use it to build and run the hello command.
48+
(cd "$GOPATH/src/example.org/hello" && "$GOPATH/bin/gopherjs" run main.go)

tests/gorepo_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
// +build !js
2-
31
package tests_test
42

53
import (
64
"os"
75
"os/exec"
6+
"runtime"
87
"testing"
98
)
109

1110
// Go repository basic compiler tests, and regression tests for fixed compiler bugs.
1211
func TestGoRepositoryCompilerTests(t *testing.T) {
12+
if runtime.GOARCH == "js" {
13+
t.Skip("test meant to be run using normal Go compiler (needs os/exec)")
14+
}
15+
1316
args := []string{"go", "run", "run.go", "-summary"}
1417
if testing.Verbose() {
1518
args = append(args, "-v")
@@ -22,3 +25,21 @@ func TestGoRepositoryCompilerTests(t *testing.T) {
2225
t.Fatal(err)
2326
}
2427
}
28+
29+
// Test that GopherJS can be vendored into a project, and then used to build Go programs.
30+
// See issue https://github.com/gopherjs/gopherjs/issues/415.
31+
func TestGopherJSCanBeVendored(t *testing.T) {
32+
if runtime.GOARCH == "js" {
33+
t.Skip("test meant to be run using normal Go compiler (needs os/exec)")
34+
}
35+
36+
cmd := exec.Command("sh", "gopherjsvendored_test.sh")
37+
cmd.Stderr = os.Stdout
38+
got, err := cmd.Output()
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
if want := "hello using js pkg\n"; string(got) != want {
43+
t.Errorf("unexpected stdout from gopherjsvendored_test.sh:\ngot:\n%s\nwant:\n%s", got, want)
44+
}
45+
}

tests/lowlevel_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// +build !js
2-
31
package tests_test
42

53
import (
64
"bytes"
75
"io/ioutil"
86
"os/exec"
97
"path/filepath"
8+
"runtime"
109
"testing"
1110
)
1211

@@ -15,6 +14,10 @@ import (
1514
//
1615
// See https://github.com/gopherjs/gopherjs/issues/279.
1716
func TestTimeInternalizationExternalization(t *testing.T) {
17+
if runtime.GOARCH == "js" {
18+
t.Skip("test meant to be run using normal Go compiler (needs os/exec)")
19+
}
20+
1821
got, err := exec.Command("gopherjs", "run", filepath.Join("testdata", "time_inexternalization.go")).Output()
1922
if err != nil {
2023
t.Fatalf("%v:\n%s", err, got)

0 commit comments

Comments
 (0)