Repository navigation
Expand file tree
/
Copy pathworkers_internal_test.go
More file actions
97 lines (88 loc) · 2.88 KB
/
Copy pathworkers_internal_test.go
File metadata and controls
97 lines (88 loc) · 2.88 KB
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
// Root-level test (justified): exercises execCommand — the injectable command runner of BuildWorkers, replaced to test without a real toolchain.
package sitec
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"webtyp.com/js"
)
// The SIMD build hands TinyGo a target file that enables simd128, and compiles for speed.
func TestWorkers_SIMDBinaryUsesSIMDTarget(t *testing.T) {
orig := execCommand
t.Cleanup(func() { execCommand = orig })
var args []string
var target string
execCommand = func(name string, arg ...string) *exec.Cmd {
if name == "tinygo" {
args = arg
for i, a := range arg {
if a == "-target" && i+1 < len(arg) {
data, _ := os.ReadFile(arg[i+1]) // read now: the temp dir is removed after Build
target = string(data)
}
}
}
return exec.Command("true")
}
root := t.TempDir()
entry := filepath.Join(root, WorkersDir, "echo", "main.go")
if err := os.MkdirAll(filepath.Dir(entry), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(entry, []byte("//go:build wasm && tinygo\n\npackage main\nfunc main() {}\n"), 0644); err != nil {
t.Fatal(err)
}
_, _ = NewWasmBuilder(WasmBuildOptions{
Package: "./" + WorkersDir + "/echo", OutputName: "echo.simd", Runtime: js.RuntimeTinyGo, SIMD: true, Speed: true,
}).Build(root) // the stub writes no binary: only the arguments matter here
if !strings.Contains(target, "+simd128") {
t.Errorf("the -target file does not enable simd128: %q (args %v)", target, args)
}
hasOpt2 := false
for _, a := range args {
hasOpt2 = hasOpt2 || a == "-opt=2"
}
if !hasOpt2 {
t.Errorf("tinygo args lack -opt=2: %v", args)
}
}
// In development a Worker is built once, with TinyGo, the SIMD target and -opt=2 — never with Go.
func TestWorkers_DevBuildsTinyGoSIMDOnce(t *testing.T) {
orig := execCommand
t.Cleanup(func() { execCommand = orig })
var tools []string
var target string
execCommand = func(name string, arg ...string) *exec.Cmd {
tools = append(tools, name)
for i, a := range arg {
if a == "-target" && i+1 < len(arg) {
data, _ := os.ReadFile(arg[i+1])
target = string(data)
}
}
return exec.Command("true")
}
root := t.TempDir()
entry := filepath.Join(root, WorkersDir, "echo", "main.go")
if err := os.MkdirAll(filepath.Dir(entry), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(entry, []byte("//go:build wasm && tinygo\n\npackage main\nfunc main() {}\n"), 0644); err != nil {
t.Fatal(err)
}
c := NewCompiler(&Config{RootDir: root, DevMode: true})
c.SetFS(NewMemFS())
workers, err := projectWorkers(root)
if err != nil {
t.Fatal(err)
}
_ = buildWorkers(root, c, true, workers) // the stub writes no binary: only the commands matter
if len(tools) != 1 || tools[0] != "tinygo" {
t.Fatalf("commands = %v, want one tinygo build", tools)
}
if !strings.Contains(target, "+simd128") {
t.Errorf("dev worker target = %q, want simd128", target)
}
}