-
Notifications
You must be signed in to change notification settings - Fork 22
/
main_test.go
165 lines (149 loc) · 3.24 KB
/
main_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"context"
"flag"
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestRun(t *testing.T) {
for _, tc := range []struct {
description string
files map[string]string
args []string
expectErr string
}{
{
description: "handle panic",
files: map[string]string{
"go.mod": `
module foo
go 1.20
`,
"foo.go": `
package main
func main() {
panic("failed")
}
`,
},
expectErr: "exit with status 2",
},
{
description: "handle panic in next run of event loop",
files: map[string]string{
"go.mod": `
module foo
go 1.20
`,
"foo.go": `
package main
import (
"syscall/js"
)
func main() {
js.Global().Call("setTimeout", js.FuncOf(func(js.Value, []js.Value) any {
panic("bad")
return nil
}), 0)
}
`,
},
expectErr: "",
},
{
description: "handle callback after test exit",
files: map[string]string{
"go.mod": `
module foo
go 1.20
`,
"foo.go": `
package main
import (
"syscall/js"
"fmt"
)
func main() {
js.Global().Call("setInterval", js.FuncOf(func(js.Value, []js.Value) any {
fmt.Println("callback")
return nil
}), 5)
fmt.Println("done")
}
`,
},
expectErr: "",
},
} {
t.Run(tc.description, func(t *testing.T) {
dir := t.TempDir()
for fileName, contents := range tc.files {
writeFile(t, dir, fileName, contents)
}
wasmFile := buildTestWasm(t, dir)
_, err := testRun(t, wasmFile, tc.args...)
assertEqualError(t, tc.expectErr, err)
})
}
}
func testRun(t *testing.T, wasmFile string, flags ...string) ([]byte, error) {
var logs bytes.Buffer
flagSet := flag.NewFlagSet("wasmbrowsertest", flag.ContinueOnError)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
err := run(ctx, append([]string{"go_js_wasm_exec", wasmFile}, flags...), &logs, flagSet)
return logs.Bytes(), err
}
// writeFile creates a file at $baseDir/$path with the given contents, where 'path' is slash separated
func writeFile(t *testing.T, baseDir, path, contents string) {
t.Helper()
path = filepath.FromSlash(path)
fullPath := filepath.Join(baseDir, path)
err := os.MkdirAll(filepath.Dir(fullPath), 0755)
if err != nil {
t.Fatal("Failed to create file's base directory:", err)
}
err = os.WriteFile(fullPath, []byte(contents), 0600)
if err != nil {
t.Fatal("Failed to create file:", err)
}
}
// buildTestWasm builds the given Go package's test binary and returns the output Wasm file
func buildTestWasm(t *testing.T, path string) string {
t.Helper()
outputFile := filepath.Join(t.TempDir(), "out.wasm")
cmd := exec.Command("go", "build", "-o", outputFile, ".")
cmd.Dir = path
cmd.Env = append(os.Environ(),
"GOOS=js",
"GOARCH=wasm",
)
output, err := cmd.CombinedOutput()
if len(output) > 0 {
t.Log(string(output))
}
if err != nil {
t.Fatal("Failed to build Wasm binary:", err)
}
return outputFile
}
func assertEqualError(t *testing.T, expected string, err error) {
t.Helper()
if expected == "" {
if err != nil {
t.Error("Unexpected error:", err)
}
return
}
if err == nil {
t.Error("Expected error, got nil")
return
}
message := err.Error()
if expected != message {
t.Errorf("Unexpected error message: %q != %q", expected, message)
}
}