package wasm import ( "context" "errors" "flag" "fmt" "io/ioutil" "log" "net/http" "os" "os/exec" "strings" "testing" "time" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/chromedp" ) var addr = flag.String("addr", ":8826", "Host:port to listen on for wasm test server") var wasmTmpDir string // set in TestMain to a temp directory for build output func TestMain(m *testing.M) { flag.Parse() os.Exit(func() int { var err error wasmTmpDir, err = ioutil.TempDir("", "wasm_test") if err != nil { log.Fatalf("unable to create temp dir: %v", err) } defer os.RemoveAll(wasmTmpDir) // cleanup even on panic and before os.Exit startServer(wasmTmpDir) return m.Run() }()) } func run(cmdline string) error { args := strings.Fields(cmdline) return runargs(args...) } func runargs(args ...string) error { cmd := exec.Command(args[0], args[1:]...) b, err := cmd.CombinedOutput() log.Printf("Command: %s; err=%v; full output:\n%s", strings.Join(args, " "), err, b) if err != nil { return err } return nil } func chromectx(timeout time.Duration) (context.Context, context.CancelFunc) { var ctx context.Context // looks for locally installed Chrome ctx, _ = chromedp.NewContext(context.Background()) ctx, cancel := context.WithTimeout(ctx, timeout) return ctx, cancel } func startServer(tmpDir string) { fsh := http.FileServer(http.Dir(tmpDir)) h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/wasm_exec.js" { http.ServeFile(w, r, "../../targets/wasm_exec.js") return } if r.URL.Path == "/run" { fmt.Fprintf(w, `