-
Notifications
You must be signed in to change notification settings - Fork 243
/
stars_test.go
58 lines (51 loc) · 1.63 KB
/
stars_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"testing"
"time"
"github.com/tetratelabs/wazero/internal/testing/maintester"
"github.com/tetratelabs/wazero/internal/testing/require"
)
// Test_main ensures the following will work:
//
// go run stars.go
func Test_main(t *testing.T) {
stdout, stderr := maintester.TestMain(t, main, "stars")
require.Equal(t, "", stderr)
require.Equal(t, "wazero has 9999999 stars. Does that include you?\n", stdout)
}
// TestMain compiles the wasm on-demand, which uses the runner's Go as opposed
// to a binary checked in, which would be pinned to one version. This is
// separate from Test_main to show that compilation doesn't dominate the
// execution time.
func TestMain(m *testing.M) {
// Notably our scratch containers don't have go, so don't fail tests.
if err := compileFromGo(); err != nil {
log.Println("Skipping tests due to:", err)
os.Exit(0)
}
os.Exit(m.Run())
}
// compileFromGo compiles "stars/main.go" on demand as the binary generated is
// too big (>7MB) to check into the source tree.
func compileFromGo() error {
cmd := exec.Command("go", "build", "-o", "main.wasm", ".")
cmd.Dir = "stars"
cmd.Env = append(os.Environ(), "GOARCH=wasm", "GOOS=js")
start := time.Now()
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("go build: %v\n%s", err, out)
}
compilationTime := time.Since(start).Milliseconds()
wasm := path.Join("stars", "main.wasm")
if s, err := os.Stat(wasm); err != nil {
return fmt.Errorf("couldn't build %s: %v", wasm, err)
} else {
log.Printf("go build took %dms and produced %dKB wasm", compilationTime, s.Size()/1024)
}
return nil
}