diff --git a/book/super-example/main.go b/book/super-example/main.go index b36aa6f8da..93c43590f4 100644 --- a/book/super-example/main.go +++ b/book/super-example/main.go @@ -53,7 +53,9 @@ func run(opts opts) wasm.Promise { var r io.Reader switch typ := opts.Input.Type(); typ { case js.TypeString: - r = strings.NewReader(opts.Input.String()) + if s := opts.Input.String(); strings.TrimSpace(s) != "" { + r = strings.NewReader(s) + } case js.TypeObject: if !opts.Input.InstanceOf(js.Global().Get("ReadableStream")) { return nil, errInvalidInput @@ -63,13 +65,15 @@ func run(opts opts) wasm.Promise { return "", errInvalidInput } zctx := super.NewContext() - zr, err := anyio.NewReaderWithOpts(zctx, r, anyio.ReaderOpts{ - Format: opts.InputFormat, - }) - if err != nil { - return "", err + var readers []sio.Reader + if r != nil { + rc, err := anyio.NewReaderWithOpts(zctx, r, anyio.ReaderOpts{Format: opts.InputFormat}) + if err != nil { + return "", err + } + defer rc.Close() + readers = []sio.Reader{rc} } - defer zr.Close() var buf bytes.Buffer zwc, err := anyio.NewWriter(sio.NopCloser(&buf), anyio.WriterOpts{Format: opts.OutputFormat}) if err != nil { @@ -78,7 +82,7 @@ func run(opts opts) wasm.Promise { defer zwc.Close() local := storage.NewLocalEngine() comp := compiler.NewCompiler(local) - query, err := runtime.CompileQuery(context.Background(), zctx, comp, flowgraph, []sio.Reader{zr}) + query, err := runtime.CompileQuery(context.Background(), zctx, comp, flowgraph, readers) if err != nil { return "", err } diff --git a/mdtest/mdtest.go b/mdtest/mdtest.go index a47aa594c6..8edbdd2412 100644 --- a/mdtest/mdtest.go +++ b/mdtest/mdtest.go @@ -51,7 +51,9 @@ // contains an SPQ test. The content of the block must comprise three sections, // each preceeded by one or more "#"-prefixed lines. The first section contains // an SPQ program, the second contains input provided to the program when the -// test runs, and the third contains the program's expected output. +// test runs, and the third contains the program's expected output. If the +// second section contains only whitespace, no input is provided when the test +// runs. // // SPQ tests are run via the super command. The command's exit status must // indicate success (i.e., be zero) unless the mdtest-spq block's info string diff --git a/mdtest/test.go b/mdtest/test.go index bc91b768ff..4ff5d9b39d 100644 --- a/mdtest/test.go +++ b/mdtest/test.go @@ -33,8 +33,11 @@ func (t *Test) Run() error { } var c *exec.Cmd if t.SPQ != "" { - c = exec.Command("super", "-s", "-c", t.SPQ, "-") - c.Stdin = strings.NewReader(t.Input) + c = exec.Command("super", "-s", "-c", t.SPQ) + if s := t.Input; strings.TrimSpace(s) != "" { + c.Args = append(c.Args, "-") + c.Stdin = strings.NewReader(s) + } } else { c = exec.Command("bash", "-e", "-o", "pipefail") c.Dir = t.Dir