Verify stdout and stderr in shell tests

While working on my next commit, to prevent I/O deadlocks, I experienced
some problems because I had a syntax error in the Elvish code to generate
the output. That wasn't immediately obvious because I had copied another
test that only tested the stdout of the shell and the syntax error was
written to stderr. This change modifies existing tests to verify both
stdout and stderr have the expected content.

Note that there are three interactive tests for which we still do not
verify the content of stderr. That's because stderr for those tests only
contains a shell prompt whose content changes each time the test is run.
TBD is modifying the interactive tests to have a predictable prompt.
This commit is contained in:
Kurtis Rader 2020-07-13 21:12:42 -07:00 committed by Qi Xiao
parent 3300c81c8b
commit 3643f252f3
5 changed files with 19 additions and 0 deletions

View File

@ -17,6 +17,7 @@ func TestVersion(t *testing.T) {
prog.Run(f.Fds(), Elvish("-version"), Program)
f.TestOut(t, 1, Version+"\n")
f.TestOut(t, 2, "")
}
func TestBuildInfo(t *testing.T) {
@ -31,6 +32,7 @@ func TestBuildInfo(t *testing.T) {
Version,
runtime.Version(),
Reproducible))
f.TestOut(t, 2, "")
}
func TestBuildInfo_JSON(t *testing.T) {
@ -49,6 +51,7 @@ func TestBuildInfo_JSON(t *testing.T) {
runtime.Version(),
Reproducible == "true",
})+"\n")
f.TestOut(t, 2, "")
}
func mustToJSON(v interface{}) string {

View File

@ -34,6 +34,7 @@ func TestCPUProfile_BadPath(t *testing.T) {
defer f.Cleanup()
Run(f.Fds(), Elvish("-cpuprofile", "/a/bad/path"), testProgram{shouldRun: true})
f.TestOut(t, 1, "")
f.TestOutSnippet(t, 2, "Warning: cannot create CPU profile:")
f.TestOutSnippet(t, 2, "Continuing without CPU profiling.")
}
@ -45,6 +46,7 @@ func TestHelp(t *testing.T) {
Run(f.Fds(), Elvish("-help"))
f.TestOutSnippet(t, 1, "Usage: elvish [flags] [script]")
f.TestOut(t, 2, "")
}
func TestShowDeprecations(t *testing.T) {
@ -76,6 +78,7 @@ func TestGoodProgram(t *testing.T) {
testProgram{shouldRun: true, writeOut: "program 2"})
f.TestOut(t, 1, "program 2")
f.TestOut(t, 2, "")
}
func TestPreferEarlierProgram(t *testing.T) {
@ -87,6 +90,7 @@ func TestPreferEarlierProgram(t *testing.T) {
testProgram{shouldRun: true, writeOut: "program 2"})
f.TestOut(t, 1, "program 1")
f.TestOut(t, 2, "")
}
func TestBadUsageError(t *testing.T) {
@ -98,6 +102,7 @@ func TestBadUsageError(t *testing.T) {
TestError(t, f, exit, "lorem ipsum")
f.TestOutSnippet(t, 2, "Usage:")
f.TestOut(t, 1, "")
}
func TestExitError(t *testing.T) {
@ -111,6 +116,7 @@ func TestExitError(t *testing.T) {
t.Errorf("exit = %v, want 3", exit)
}
f.TestOut(t, 2, "")
f.TestOut(t, 1, "")
}
func TestExitError_0(t *testing.T) {

View File

@ -32,6 +32,7 @@ func TestInteract_Exception(t *testing.T) {
Interact(f.Fds(), &InteractConfig{})
f.TestOutSnippet(t, 2, "fail mock")
f.TestOut(t, 1, "")
}
func TestInteract_RcFile(t *testing.T) {
@ -54,6 +55,7 @@ func TestInteract_RcFile_DoesNotCompile(t *testing.T) {
Interact(f.Fds(), &InteractConfig{Paths: Paths{Rc: "rc.elv"}})
f.TestOutSnippet(t, 2, "variable $a not found")
f.TestOut(t, 1, "")
}
func TestInteract_RcFile_Exception(t *testing.T) {
@ -65,6 +67,7 @@ func TestInteract_RcFile_Exception(t *testing.T) {
Interact(f.Fds(), &InteractConfig{Paths: Paths{Rc: "rc.elv"}})
f.TestOutSnippet(t, 2, "fail mock")
f.TestOut(t, 1, "")
}
func TestInteract_RcFile_NonexistentIsOK(t *testing.T) {

View File

@ -14,6 +14,7 @@ func TestScript_File(t *testing.T) {
Script(f.Fds(), []string{"a.elv"}, &ScriptConfig{})
f.TestOut(t, 1, "hello\n")
f.TestOut(t, 2, "")
}
func TestScript_BadFile(t *testing.T) {
@ -26,6 +27,7 @@ func TestScript_BadFile(t *testing.T) {
t.Errorf("got ret %v, want 2", ret)
}
f.TestOutSnippet(t, 2, "cannot read script")
f.TestOut(t, 1, "")
}
func TestScript_Cmd(t *testing.T) {
@ -35,6 +37,7 @@ func TestScript_Cmd(t *testing.T) {
Script(f.Fds(), []string{"echo hello"}, &ScriptConfig{Cmd: true})
f.TestOut(t, 1, "hello\n")
f.TestOut(t, 2, "")
}
func TestScript_DoesNotCompile(t *testing.T) {
@ -47,6 +50,7 @@ func TestScript_DoesNotCompile(t *testing.T) {
t.Errorf("got ret %v, want 2", ret)
}
f.TestOutSnippet(t, 2, "compilation error")
f.TestOut(t, 1, "")
}
func TestScript_DoesNotCompile_JSON(t *testing.T) {
@ -60,6 +64,7 @@ func TestScript_DoesNotCompile_JSON(t *testing.T) {
t.Errorf("got ret %v, want 2", ret)
}
f.TestOutSnippet(t, 1, "variable $a not found")
f.TestOut(t, 2, "")
}
func TestScript_Exception(t *testing.T) {
@ -71,6 +76,7 @@ func TestScript_Exception(t *testing.T) {
t.Errorf("got ret %v, want 2", ret)
}
f.TestOutSnippet(t, 2, "fail failure")
f.TestOut(t, 1, "")
}
func TestScript_Exception_CompileOnly(t *testing.T) {

View File

@ -58,6 +58,7 @@ func testSHLVL(t *testing.T, wantSHLVL string) {
Script(f.Fds(), []string{"print $E:SHLVL"}, &ScriptConfig{Cmd: true})
f.TestOut(t, 1, wantSHLVL)
f.TestOut(t, 2, "")
// Test that state of SHLVL is restored.
newValue, newOK := os.LookupEnv("SHLVL")