elvish/pkg/eval/port_helper_test.go
Qi Xiao a2790af67a pkg/eval: Clean up the structure and methods of Evaler and Frame.
- Make Evaler mostly thread-safe. The only remaining thread-unsafe part is the
  modules field, which is more tricky than other fields.

- Remove the state and evalerScopes type, and move their fields into Evaler.

- Expose valuePrefix via a get method, and change PortsFromFiles to take the
  prefix instead of a *Evaler. Also expose a PortsFromStdFiles.

- Make Evaler a normal field of Frame, instead of an embedded field. This makes
  access to global states more explicit.
2021-01-05 00:22:09 +00:00

54 lines
1.0 KiB
Go

package eval
import (
"io"
"io/ioutil"
"os"
"testing"
)
func TestEvalerPorts(t *testing.T) {
stdoutReader, stdout := mustPipe()
defer stdoutReader.Close()
stderrReader, stderr := mustPipe()
defer stderrReader.Close()
prefix := "> "
ports, cleanup := PortsFromFiles([3]*os.File{DevNull, stdout, stderr}, prefix)
ports[1].Chan <- "x"
ports[1].Chan <- "y"
ports[2].Chan <- "bad"
ports[2].Chan <- "err"
cleanup()
stdout.Close()
stderr.Close()
stdoutAll := mustReadAllString(stdoutReader)
wantStdoutAll := "> x\n> y\n"
if stdoutAll != wantStdoutAll {
t.Errorf("stdout is %q, want %q", stdoutAll, wantStdoutAll)
}
stderrAll := mustReadAllString(stderrReader)
wantStderrAll := "> bad\n> err\n"
if stderrAll != wantStderrAll {
t.Errorf("stderr is %q, want %q", stderrAll, wantStderrAll)
}
}
func mustReadAllString(r io.Reader) string {
b, err := ioutil.ReadAll(r)
if err != nil {
panic(err)
}
return string(b)
}
func mustPipe() (*os.File, *os.File) {
r, w, err := os.Pipe()
if err != nil {
panic(err)
}
return r, w
}