pkg/program/shell: Test against Interact.

This addresses #944.
This commit is contained in:
Qi Xiao 2020-04-16 11:41:34 +01:00
parent 8deec2e3fe
commit e23c622863
2 changed files with 57 additions and 0 deletions

View File

@ -9,6 +9,55 @@ import (
"github.com/elves/elvish/pkg/eval/vars"
)
func TestInteract_SingleCommand(t *testing.T) {
f := setup()
defer f.cleanup()
f.feedIn("echo hello\n")
Interact(f.fds(), &InteractConfig{})
f.testOut(t, 1, "hello\n")
}
func TestInteract_Exception(t *testing.T) {
f := setup()
defer f.cleanup()
f.feedIn("fail mock\n")
Interact(f.fds(), &InteractConfig{})
f.testOutSnippet(t, 2, "fail mock")
}
func TestInteract_RcFile(t *testing.T) {
f := setup()
defer f.cleanup()
f.feedIn("")
writeFile("rc.elv", "echo hello from rc.elv")
Interact(f.fds(), &InteractConfig{Paths: Paths{Rc: "rc.elv"}})
f.testOut(t, 1, "hello from rc.elv\n")
}
func TestInteract_RcFile_Exception(t *testing.T) {
f := setup()
defer f.cleanup()
f.feedIn("")
writeFile("rc.elv", "fail mock")
Interact(f.fds(), &InteractConfig{Paths: Paths{Rc: "rc.elv"}})
f.testOutSnippet(t, 2, "fail mock")
}
func TestInteract_RcFile_NonexistentIsOK(t *testing.T) {
f := setup()
defer f.cleanup()
f.feedIn("")
Interact(f.fds(), &InteractConfig{Paths: Paths{Rc: "rc.elv"}})
f.testOut(t, 1, "")
}
func TestExtractExports(t *testing.T) {
ns := eval.Ns{
exportsVarName: vars.NewReadOnly(vals.EmptyMap.Assoc("a", "lorem")),

View File

@ -30,6 +30,14 @@ func (f *fixture) fds() [3]*os.File {
return [3]*os.File{f.pipes[0].r, f.pipes[1].w, f.pipes[2].w}
}
func (f *fixture) feedIn(s string) {
_, err := f.pipes[0].w.WriteString(s)
if err != nil {
panic(err)
}
f.pipes[0].w.Close()
}
func (f *fixture) testOut(t *testing.T, fd int, wantOut string) {
t.Helper()
if out := f.pipes[fd].get(); out != wantOut {