diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wasm/chan_test.go | 5 | ||||
-rw-r--r-- | tests/wasm/event_test.go | 5 | ||||
-rw-r--r-- | tests/wasm/fmt_test.go | 5 | ||||
-rw-r--r-- | tests/wasm/fmtprint_test.go | 5 | ||||
-rw-r--r-- | tests/wasm/log_test.go | 5 | ||||
-rw-r--r-- | tests/wasm/setup_test.go | 49 |
6 files changed, 40 insertions, 34 deletions
diff --git a/tests/wasm/chan_test.go b/tests/wasm/chan_test.go index 1cd08e664..2faf95bac 100644 --- a/tests/wasm/chan_test.go +++ b/tests/wasm/chan_test.go @@ -11,6 +11,9 @@ func TestChan(t *testing.T) { t.Parallel() + wasmTmpDir, server, cleanup := startServer(t) + defer cleanup() + err := run("tinygo build -o " + wasmTmpDir + "/chan.wasm -target wasm testdata/chan.go") if err != nil { t.Fatal(err) @@ -20,7 +23,7 @@ func TestChan(t *testing.T) { defer cancel() err = chromedp.Run(ctx, - chromedp.Navigate("http://localhost:8826/run?file=chan.wasm"), + chromedp.Navigate(server.URL+"/run?file=chan.wasm"), waitLog(`1 2 4 diff --git a/tests/wasm/event_test.go b/tests/wasm/event_test.go index d2b8340ce..f4ede023a 100644 --- a/tests/wasm/event_test.go +++ b/tests/wasm/event_test.go @@ -13,6 +13,9 @@ func TestEvent(t *testing.T) { t.Parallel() + wasmTmpDir, server, cleanup := startServer(t) + defer cleanup() + err := run("tinygo build -o " + wasmTmpDir + "/event.wasm -target wasm testdata/event.go") if err != nil { t.Fatal(err) @@ -23,7 +26,7 @@ func TestEvent(t *testing.T) { var log1, log2 string err = chromedp.Run(ctx, - chromedp.Navigate("http://localhost:8826/run?file=event.wasm"), + chromedp.Navigate(server.URL+"/run?file=event.wasm"), chromedp.WaitVisible("#log"), chromedp.Sleep(time.Second), chromedp.InnerHTML("#log", &log1), diff --git a/tests/wasm/fmt_test.go b/tests/wasm/fmt_test.go index 8b4fe8c75..cac38e721 100644 --- a/tests/wasm/fmt_test.go +++ b/tests/wasm/fmt_test.go @@ -20,6 +20,9 @@ func TestFmt(t *testing.T) { t.Parallel() + wasmTmpDir, server, cleanup := startServer(t) + defer cleanup() + err := run("tinygo build -o " + wasmTmpDir + "/fmt.wasm -target wasm testdata/fmt.go") if err != nil { t.Fatal(err) @@ -30,7 +33,7 @@ func TestFmt(t *testing.T) { var log1 string err = chromedp.Run(ctx, - chromedp.Navigate("http://localhost:8826/run?file=fmt.wasm"), + chromedp.Navigate(server.URL+"/run?file=fmt.wasm"), chromedp.Sleep(time.Second), chromedp.InnerHTML("#log", &log1), waitLog(`did not panic`), diff --git a/tests/wasm/fmtprint_test.go b/tests/wasm/fmtprint_test.go index ebd1ffa99..7b4a4701a 100644 --- a/tests/wasm/fmtprint_test.go +++ b/tests/wasm/fmtprint_test.go @@ -13,6 +13,9 @@ func TestFmtprint(t *testing.T) { t.Parallel() + wasmTmpDir, server, cleanup := startServer(t) + defer cleanup() + err := run("tinygo build -o " + wasmTmpDir + "/fmtprint.wasm -target wasm testdata/fmtprint.go") if err != nil { t.Fatal(err) @@ -23,7 +26,7 @@ func TestFmtprint(t *testing.T) { var log1 string err = chromedp.Run(ctx, - chromedp.Navigate("http://localhost:8826/run?file=fmtprint.wasm"), + chromedp.Navigate(server.URL+"/run?file=fmtprint.wasm"), chromedp.Sleep(time.Second), chromedp.InnerHTML("#log", &log1), waitLog(`test from fmtprint 1 diff --git a/tests/wasm/log_test.go b/tests/wasm/log_test.go index a9cc1befa..ea314e478 100644 --- a/tests/wasm/log_test.go +++ b/tests/wasm/log_test.go @@ -13,6 +13,9 @@ func TestLog(t *testing.T) { t.Parallel() + wasmTmpDir, server, cleanup := startServer(t) + defer cleanup() + err := run("tinygo build -o " + wasmTmpDir + "/log.wasm -target wasm testdata/log.go") if err != nil { t.Fatal(err) @@ -23,7 +26,7 @@ func TestLog(t *testing.T) { var log1 string err = chromedp.Run(ctx, - chromedp.Navigate("http://localhost:8826/run?file=log.wasm"), + chromedp.Navigate(server.URL+"/run?file=log.wasm"), chromedp.Sleep(time.Second), chromedp.InnerHTML("#log", &log1), waitLogRe(`^..../../.. ..:..:.. log 1 diff --git a/tests/wasm/setup_test.go b/tests/wasm/setup_test.go index 6a84983dc..77f5063f8 100644 --- a/tests/wasm/setup_test.go +++ b/tests/wasm/setup_test.go @@ -3,11 +3,11 @@ package wasm import ( "context" "errors" - "flag" "fmt" "io/ioutil" "log" "net/http" + "net/http/httptest" "os" "os/exec" "regexp" @@ -20,29 +20,6 @@ import ( "github.com/chromedp/chromedp" ) -var addr = flag.String("addr", ":8826", "Host:port to listen on for wasm test server") - -var wasmTmpDir string // set in TestMain to a temp directory for build output - -func TestMain(m *testing.M) { - flag.Parse() - - os.Exit(func() int { - - var err error - wasmTmpDir, err = ioutil.TempDir("", "wasm_test") - if err != nil { - log.Fatalf("unable to create temp dir: %v", err) - } - defer os.RemoveAll(wasmTmpDir) // cleanup even on panic and before os.Exit - - startServer(wasmTmpDir) - - return m.Run() - }()) - -} - func run(cmdline string) error { args := strings.Fields(cmdline) return runargs(args...) @@ -70,7 +47,12 @@ func chromectx(timeout time.Duration) (context.Context, context.CancelFunc) { return ctx, cancel } -func startServer(tmpDir string) { +func startServer(t *testing.T) (string, *httptest.Server, func()) { + // In Go 1.15, all this can be replaced by 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)) h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -140,11 +122,20 @@ if (wasmSupported) { fsh.ServeHTTP(w, r) }) - log.Printf("Starting server at %q for dir: %s", *addr, tmpDir) - go func() { - log.Fatal(http.ListenAndServe(*addr, h)) - }() + server := httptest.NewServer(h) + t.Logf("Started server at %q for dir: %s", server.URL, tmpDir) + + // In Go 1.14+, this can be replaced by t.Cleanup() + 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) |