Permalink
Browse files

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.
  • Loading branch information...
dmitshur committed Apr 15, 2018
1 parent b24e356 commit d0d69c08b6af458cebb60d3295f643e4a471029d
Showing with 76 additions and 4 deletions.
  1. +48 −0 tests/gopherjsvendored_test.sh
  2. +23 −2 tests/gorepo_test.go
  3. +5 −2 tests/lowlevel_test.go
@@ -0,0 +1,48 @@
#!/bin/sh
# Don't run this file directly. It's executed as part of TestGopherJSCanBeVendored.
set -e
tmp=$(mktemp -d "${TMPDIR:-/tmp}/gopherjsvendored_test.XXXXXXXXXX")
cleanup() {
rm -rf "$tmp"
exit
}
trap cleanup EXIT HUP INT TERM
# copyGoPackage copies Go package with import path $1 to directory $2.
# The target directory is created if it doesn't exist.
copyGoPackage() {
mkdir -p "$2"
pkgDir=$(go list -f '{{.Dir}}' "$1")
# Copy all files (not directories), other than ones that start with "." or "_".
for f in $(find -H "$pkgDir" -maxdepth 1 -name "[^._]*" -type f); do
cp "$f" "$2"
done
}
# Make a hello project that will vendor GopherJS.
mkdir -p "$tmp/src/example.org/hello"
echo 'package main
import "github.com/gopherjs/gopherjs/js"
func main() {
js.Global.Get("console").Call("log", "hello using js pkg")
}' > "$tmp/src/example.org/hello/main.go"
# Vendor GopherJS and its dependencies into hello project.
for pkg in $(go list -f '{{if not .Goroot}}{{.ImportPath}}{{end}}' $(go list -f '{{.ImportPath}} {{join .Deps " "}}' github.com/gopherjs/gopherjs)); do
copyGoPackage "$pkg" "$tmp/src/example.org/hello/vendor/$pkg"
done
# Make $tmp our GOPATH workspace.
export GOPATH="$tmp"
# Build the vendored copy of GopherJS.
go install example.org/hello/vendor/github.com/gopherjs/gopherjs
# Use it to build and run the hello command.
(cd "$GOPATH/src/example.org/hello" && "$GOPATH/bin/gopherjs" run main.go)
@@ -1,15 +1,18 @@
// +build !js
package tests_test
import (
"os"
"os/exec"
"runtime"
"testing"
)
// Go repository basic compiler tests, and regression tests for fixed compiler bugs.
func TestGoRepositoryCompilerTests(t *testing.T) {
if runtime.GOARCH == "js" {
t.Skip("test meant to be run using normal Go compiler (needs os/exec)")
}
args := []string{"go", "run", "run.go", "-summary"}
if testing.Verbose() {
args = append(args, "-v")
@@ -22,3 +25,21 @@ func TestGoRepositoryCompilerTests(t *testing.T) {
t.Fatal(err)
}
}
// Test that GopherJS can be vendored into a project, and then used to build Go programs.
// See issue https://github.com/gopherjs/gopherjs/issues/415.
func TestGopherJSCanBeVendored(t *testing.T) {
if runtime.GOARCH == "js" {
t.Skip("test meant to be run using normal Go compiler (needs os/exec)")
}
cmd := exec.Command("sh", "gopherjsvendored_test.sh")
cmd.Stderr = os.Stdout
got, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
if want := "hello using js pkg\n"; string(got) != want {
t.Errorf("unexpected stdout from gopherjsvendored_test.sh:\ngot:\n%s\nwant:\n%s", got, want)
}
}
@@ -1,12 +1,11 @@
// +build !js
package tests_test
import (
"bytes"
"io/ioutil"
"os/exec"
"path/filepath"
"runtime"
"testing"
)
@@ -15,6 +14,10 @@ import (
//
// See https://github.com/gopherjs/gopherjs/issues/279.
func TestTimeInternalizationExternalization(t *testing.T) {
if runtime.GOARCH == "js" {
t.Skip("test meant to be run using normal Go compiler (needs os/exec)")
}
got, err := exec.Command("gopherjs", "run", filepath.Join("testdata", "time_inexternalization.go")).Output()
if err != nil {
t.Fatalf("%v:\n%s", err, got)

0 comments on commit d0d69c0

Please sign in to comment.