github.com/vugu/vugu@v0.3.5/devutil/compiler_test.go (about) 1 package devutil 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "net/http" 7 "net/http/httptest" 8 "os" 9 "path/filepath" 10 "strings" 11 "testing" 12 ) 13 14 func TestWasmCompiler(t *testing.T) { 15 16 tmpDir, err := ioutil.TempDir("", "TestWasmCompiler") 17 must(err) 18 defer os.RemoveAll(tmpDir) 19 t.Logf("Using temporary dir: %s", tmpDir) 20 21 wc := NewWasmCompiler().SetBuildDir(tmpDir) 22 23 must(ioutil.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`module TestWasmCompiler 24 `), 0644)) 25 26 // just build 27 must(ioutil.WriteFile(filepath.Join(tmpDir, "main.go"), []byte(`package main 28 func main() {}`), 0644)) 29 outpath, err := wc.Execute() 30 if err != nil { 31 t.Fatal(err) 32 } 33 defer os.Remove(outpath) 34 _, err = os.Stat(outpath) 35 if err != nil { 36 t.Fatal(err) 37 } 38 39 // build with error 40 ioutil.WriteFile(filepath.Join(tmpDir, "main.go"), []byte(`package main 41 func main() { not valid go code }`), 0644) 42 outpath, err = wc.Execute() 43 if err == nil { 44 t.Fatal("should have gotten error here but didn't") 45 } 46 t.Logf("we correctly got a build error here: %v", err) 47 48 // with generate 49 must(ioutil.WriteFile(filepath.Join(tmpDir, "main.go"), []byte(`package main 50 import "fmt" 51 //go:generate go run gen.go 52 func main() { fmt.Println(other) }`), 0644)) 53 must(ioutil.WriteFile(filepath.Join(tmpDir, "gen.go"), []byte(`// +build ignore 54 55 package main 56 import "io/ioutil" 57 func main() { ioutil.WriteFile("./other.go", []byte("package main\nvar other = 123\n"), 0644) }`), 0644)) 58 wc.SetGenerateDir(tmpDir) 59 outpath, err = wc.Execute() 60 if err != nil { 61 t.Fatal(err) 62 } 63 defer os.Remove(outpath) 64 _, err = os.Stat(outpath) 65 if err != nil { 66 t.Fatal(err) 67 } 68 69 // new temp dir 70 tmpDir, err = ioutil.TempDir("", "TestWasmCompiler") 71 must(err) 72 defer os.RemoveAll(tmpDir) 73 t.Logf("Using temporary dir: %s", tmpDir) 74 wc = NewWasmCompiler().SetBuildDir(tmpDir) 75 var h http.Handler 76 77 must(ioutil.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`module TestWasmCompiler 78 `), 0644)) 79 80 // main wasm handler without error 81 must(ioutil.WriteFile(filepath.Join(tmpDir, "main.go"), []byte(`package main 82 func main() {}`), 0644)) 83 h = NewMainWasmHandler(wc) 84 req, err := http.NewRequest("GET", "/main.wasm", nil) 85 must(err) 86 wr := httptest.NewRecorder() 87 h.ServeHTTP(wr, req) 88 res := wr.Result() 89 defer res.Body.Close() 90 // resb, _ := httputil.DumpResponse(res, true) 91 // t.Logf("RESPONSE: %s", resb) 92 if res.StatusCode != 200 { 93 t.Errorf("unexpected http status code: %d", res.StatusCode) 94 } 95 ct := res.Header.Get("Content-Type") 96 if !strings.HasPrefix(ct, "application/wasm") { 97 t.Errorf("unexpected value for Content-Type header: %s", ct) 98 } 99 b, err := ioutil.ReadAll(res.Body) 100 must(err) 101 if bytes.Compare(b[:4], []byte("\x00asm")) != 0 { 102 t.Errorf("got back bytes that do not look like a wasm file: %X (len=%d, cap=%d)", b[:4], len(b), cap(b)) 103 } 104 105 // main wasm handler with error 106 must(ioutil.WriteFile(filepath.Join(tmpDir, "main.go"), []byte(`package main 107 func main() { not valid go code }`), 0644)) 108 h = NewMainWasmHandler(wc) 109 req, err = http.NewRequest("GET", "/main.wasm", nil) 110 must(err) 111 wr = httptest.NewRecorder() 112 h.ServeHTTP(wr, req) 113 res = wr.Result() 114 defer res.Body.Close() 115 // resb, _ := httputil.DumpResponse(res, true) 116 // t.Logf("RESPONSE: %s", resb) 117 if res.StatusCode != 500 { 118 t.Errorf("unexpected http status code: %d", res.StatusCode) 119 } 120 ct = res.Header.Get("Content-Type") 121 if !strings.HasPrefix(ct, "text/plain") { 122 t.Errorf("unexpected value for Content-Type header: %s", ct) 123 } 124 b, err = ioutil.ReadAll(res.Body) 125 must(err) 126 if !bytes.Contains(b, []byte("build error")) { 127 t.Errorf("unexpected error result: %s", b) 128 } 129 130 // wasm exec js handler 131 h = NewWasmExecJSHandler(wc) 132 req, err = http.NewRequest("GET", "/wasm_exec.js", nil) 133 must(err) 134 wr = httptest.NewRecorder() 135 h.ServeHTTP(wr, req) 136 res = wr.Result() 137 defer res.Body.Close() 138 if res.StatusCode != 200 { 139 t.Errorf("unexpected http status code: %d", res.StatusCode) 140 } 141 ct = res.Header.Get("Content-Type") 142 if !strings.HasPrefix(ct, "text/javascript") { 143 t.Errorf("unexpected value for Content-Type header: %s", ct) 144 } 145 b, err = ioutil.ReadAll(res.Body) 146 must(err) 147 if !bytes.Contains(b, []byte("The Go Authors")) { 148 t.Errorf("unexpected js result: %s", b) 149 } 150 151 } 152 153 func must(err error) { 154 if err != nil { 155 panic(err) 156 } 157 }