all: use new testing features of Go 1.14 and 1.15

This simplifies the tests a bit.
Этот коммит содержится в:
Ayke van Laethem 2021-08-15 16:03:17 +02:00 коммит произвёл Ron Evans
родитель 25c7bfd404
коммит 59d53182bb
7 изменённых файлов: 10 добавлений и 39 удалений

Просмотреть файл

@ -210,16 +210,7 @@ func runTestWithConfig(name, target string, t *testing.T, options compileopts.Op
} }
// Create a temporary directory for test output files. // Create a temporary directory for test output files.
tmpdir, err := ioutil.TempDir("", "tinygo-test") tmpdir := t.TempDir()
if err != nil {
t.Fatal("could not create temporary directory:", err)
}
defer func() {
rerr := os.RemoveAll(tmpdir)
if rerr != nil {
t.Errorf("failed to remove temporary directory %q: %s", tmpdir, rerr.Error())
}
}()
// Determine whether we're on a system that supports environment variables // Determine whether we're on a system that supports environment variables
// and command line parameters (operating systems, WASI) or not (baremetal, // and command line parameters (operating systems, WASI) or not (baremetal,

Просмотреть файл

@ -11,8 +11,7 @@ func TestChan(t *testing.T) {
t.Parallel() t.Parallel()
wasmTmpDir, server, cleanup := startServer(t) wasmTmpDir, server := startServer(t)
defer cleanup()
err := run("tinygo build -o " + wasmTmpDir + "/chan.wasm -target wasm testdata/chan.go") err := run("tinygo build -o " + wasmTmpDir + "/chan.wasm -target wasm testdata/chan.go")
if err != nil { if err != nil {

Просмотреть файл

@ -11,8 +11,7 @@ func TestEvent(t *testing.T) {
t.Parallel() t.Parallel()
wasmTmpDir, server, cleanup := startServer(t) wasmTmpDir, server := startServer(t)
defer cleanup()
err := run("tinygo build -o " + wasmTmpDir + "/event.wasm -target wasm testdata/event.go") err := run("tinygo build -o " + wasmTmpDir + "/event.wasm -target wasm testdata/event.go")
if err != nil { if err != nil {

Просмотреть файл

@ -11,8 +11,7 @@ func TestFmt(t *testing.T) {
t.Parallel() t.Parallel()
wasmTmpDir, server, cleanup := startServer(t) wasmTmpDir, server := startServer(t)
defer cleanup()
err := run("tinygo build -o " + wasmTmpDir + "/fmt.wasm -target wasm testdata/fmt.go") err := run("tinygo build -o " + wasmTmpDir + "/fmt.wasm -target wasm testdata/fmt.go")
if err != nil { if err != nil {

Просмотреть файл

@ -11,8 +11,7 @@ func TestFmtprint(t *testing.T) {
t.Parallel() t.Parallel()
wasmTmpDir, server, cleanup := startServer(t) wasmTmpDir, server := startServer(t)
defer cleanup()
err := run("tinygo build -o " + wasmTmpDir + "/fmtprint.wasm -target wasm testdata/fmtprint.go") err := run("tinygo build -o " + wasmTmpDir + "/fmtprint.wasm -target wasm testdata/fmtprint.go")
if err != nil { if err != nil {

Просмотреть файл

@ -11,8 +11,7 @@ func TestLog(t *testing.T) {
t.Parallel() t.Parallel()
wasmTmpDir, server, cleanup := startServer(t) wasmTmpDir, server := startServer(t)
defer cleanup()
err := run("tinygo build -o " + wasmTmpDir + "/log.wasm -target wasm testdata/log.go") err := run("tinygo build -o " + wasmTmpDir + "/log.wasm -target wasm testdata/log.go")
if err != nil { if err != nil {

Просмотреть файл

@ -4,11 +4,9 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"os/exec" "os/exec"
"regexp" "regexp"
"strings" "strings"
@ -47,12 +45,8 @@ func chromectx(timeout time.Duration) (context.Context, context.CancelFunc) {
return ctx, cancel return ctx, cancel
} }
func startServer(t *testing.T) (string, *httptest.Server, func()) { func startServer(t *testing.T) (string, *httptest.Server) {
// In Go 1.15, all this can be replaced by t.TempDir() tmpDir := t.TempDir()
tmpDir, err := ioutil.TempDir("", "wasm_test")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
fsh := http.FileServer(http.Dir(tmpDir)) fsh := http.FileServer(http.Dir(tmpDir))
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -124,18 +118,9 @@ if (wasmSupported) {
server := httptest.NewServer(h) server := httptest.NewServer(h)
t.Logf("Started server at %q for dir: %s", server.URL, tmpDir) t.Logf("Started server at %q for dir: %s", server.URL, tmpDir)
t.Cleanup(server.Close)
// In Go 1.14+, this can be replaced by t.Cleanup() return tmpDir, server
cleanup := func() {
err := os.RemoveAll(tmpDir)
if err != nil {
t.Error(err)
}
server.Close()
}
return tmpDir, server, cleanup
} }
// waitLog blocks until the log output equals the text provided (ignoring whitespace before and after) // waitLog blocks until the log output equals the text provided (ignoring whitespace before and after)