@@ -4,116 +4,59 @@ import (
4
4
"bytes"
5
5
"context"
6
6
"flag"
7
- "io"
8
7
"os"
9
8
"os/exec"
10
9
"path/filepath"
11
10
"testing"
12
11
)
13
12
14
13
func TestRun (t * testing.T ) {
15
- t .Parallel ()
16
-
17
14
for _ , tc := range []struct {
18
15
description string
19
16
files map [string ]string
20
17
args []string
21
18
expectErr string
22
19
}{
23
- {
24
- description : "pass" ,
25
- files : map [string ]string {
26
- "go.mod" : `
27
- module foo
28
- ` ,
29
- "foo_test.go" : `
30
- package foo
31
-
32
- import "testing"
33
-
34
- func TestFoo(t *testing.T) {
35
- if false {
36
- t.Errorf("foo failed")
37
- }
38
- }
39
- ` ,
40
- },
41
- },
42
- {
43
- description : "fails" ,
44
- files : map [string ]string {
45
- "go.mod" : `
46
- module foo
47
- ` ,
48
- "foo_test.go" : `
49
- package foo
50
-
51
- import "testing"
52
-
53
- func TestFooFails(t *testing.T) {
54
- t.Errorf("foo failed")
55
- }
56
- ` ,
57
- },
58
- expectErr : "exit with status 1" ,
59
- },
60
20
{
61
21
description : "panic fails" ,
62
22
files : map [string ]string {
63
23
"go.mod" : `
64
24
module foo
65
- ` ,
66
- "foo_test.go" : `
67
- package foo
68
25
69
- import "testing"
26
+ go 1.19
27
+ ` ,
28
+ "foo.go" : `
29
+ package main
70
30
71
- func TestFooPanic(t *testing.T ) {
31
+ func main( ) {
72
32
panic("failed")
73
33
}
74
34
` ,
75
35
},
76
36
expectErr : "exit with status 2" ,
77
37
},
78
- {
79
- description : "panic in goroutine fails" ,
80
- files : map [string ]string {
81
- "go.mod" : `
82
- module foo
83
- ` ,
84
- "foo_test.go" : `
85
- package foo
86
-
87
- import "testing"
88
-
89
- func TestFooGoroutinePanic(t *testing.T) {
90
- go panic("foo failed")
91
- }
92
- ` ,
93
- },
94
- expectErr : "exit with status 1" ,
95
- },
96
38
{
97
39
description : "panic in next run of event loop fails" ,
98
40
files : map [string ]string {
99
41
"go.mod" : `
100
- module foo
101
- ` ,
102
- "foo_test.go" : `
103
- package foo
104
-
105
- import (
106
- "syscall/js"
107
- "testing"
108
- )
109
-
110
- func TestFooNextEventLoopPanic(t *testing.T) {
111
- js.Global().Call("setTimeout", js.FuncOf(func(js.Value, []js.Value) interface{} {
112
- panic("bad")
113
- return nil
114
- }), 0)
115
- }
116
- ` ,
42
+ module foo
43
+
44
+ go 1.19
45
+ ` ,
46
+ "foo.go" : `
47
+ package main
48
+
49
+ import (
50
+ "syscall/js"
51
+ )
52
+
53
+ func main() {
54
+ js.Global().Call("setTimeout", js.FuncOf(func(js.Value, []js.Value) any {
55
+ panic("bad")
56
+ return nil
57
+ }), 0)
58
+ }
59
+ ` ,
117
60
},
118
61
expectErr : "context canceled" ,
119
62
},
@@ -132,28 +75,13 @@ func TestFooNextEventLoopPanic(t *testing.T) {
132
75
}
133
76
}
134
77
135
- type testWriter struct {
136
- testingT * testing.T
137
- }
138
-
139
- func testLogger (t * testing.T ) io.Writer {
140
- return & testWriter {t }
141
- }
142
-
143
- func (w * testWriter ) Write (b []byte ) (int , error ) {
144
- w .testingT .Helper ()
145
- w .testingT .Log (string (b ))
146
- return len (b ), nil
147
- }
148
-
149
78
func testRun (t * testing.T , wasmFile string , flags ... string ) ([]byte , error ) {
150
79
var logs bytes.Buffer
151
- output := io .MultiWriter (testLogger (t ), & logs )
152
80
flagSet := flag .NewFlagSet ("wasmbrowsertest" , flag .ContinueOnError )
153
81
ctx , cancel := context .WithCancel (context .Background ())
154
82
t .Cleanup (cancel )
155
83
156
- err := run (ctx , append ([]string {"go_js_wasm_exec" , wasmFile , "-test.v" }, flags ... ), output , flagSet )
84
+ err := run (ctx , append ([]string {"go_js_wasm_exec" , wasmFile }, flags ... ), & logs , flagSet )
157
85
return logs .Bytes (), err
158
86
}
159
87
@@ -176,7 +104,7 @@ func writeFile(t *testing.T, baseDir, path, contents string) {
176
104
func buildTestWasm (t * testing.T , path string ) string {
177
105
t .Helper ()
178
106
outputFile := filepath .Join (t .TempDir (), "out.wasm" )
179
- cmd := exec .Command ("go" , "test" , "-c " , "-o" , outputFile , "." )
107
+ cmd := exec .Command ("go" , "build " , "-o" , outputFile , "." )
180
108
cmd .Dir = path
181
109
cmd .Env = append (os .Environ (),
182
110
"GOOS=js" ,
0 commit comments