test/modules/shell.go: have a single NewShell call with functional expect.Session support.
MultiPart: 1/5

Change-Id: Iba39c53991f9528c619e63431106f42a35e58ebf
diff --git a/test/modules/core/core_test.go b/test/modules/core/core_test.go
index 9ebecb8..f114981 100644
--- a/test/modules/core/core_test.go
+++ b/test/modules/core/core_test.go
@@ -46,7 +46,7 @@
 
 func newShell(t *testing.T) (*modules.Shell, func()) {
 	ctx, shutdown := test.InitForTest()
-	sh, err := modules.NewExpectShell(ctx, nil, t, testing.Verbose())
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
diff --git a/test/modules/examples_test.go b/test/modules/examples_test.go
index 9fd1f24..299ec71 100644
--- a/test/modules/examples_test.go
+++ b/test/modules/examples_test.go
@@ -31,7 +31,7 @@
 		return
 	}
 	// Parent process.
-	sh, _ := modules.NewShell(ctx, nil)
+	sh, _ := modules.NewShell(ctx, nil, false, nil)
 	defer sh.Cleanup(nil, nil)
 	h, _ := sh.Start("echo", nil, "a", "b")
 	h.Shutdown(os.Stdout, os.Stderr)
@@ -45,7 +45,7 @@
 	defer shutdown()
 	// DispatchAndExit will call os.Exit(0) when executed within the child.
 	modules.DispatchAndExit()
-	sh, _ := modules.NewShell(ctx, nil)
+	sh, _ := modules.NewShell(ctx, nil, false, nil)
 	defer sh.Cleanup(nil, nil)
 	h, _ := sh.Start("echo", nil, "c", "d")
 	h.Shutdown(os.Stdout, os.Stderr)
diff --git a/test/modules/modules_internal_test.go b/test/modules/modules_internal_test.go
index 38ef18e..0585238 100644
--- a/test/modules/modules_internal_test.go
+++ b/test/modules/modules_internal_test.go
@@ -33,7 +33,7 @@
 
 	defer shutdown()
 
-	sh, err := NewShell(ctx, nil)
+	sh, err := NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
diff --git a/test/modules/modules_test.go b/test/modules/modules_test.go
index 1aece64..108eba7 100644
--- a/test/modules/modules_test.go
+++ b/test/modules/modules_test.go
@@ -148,11 +148,15 @@
 	defer func() {
 		var stdout, stderr bytes.Buffer
 		sh.Cleanup(&stdout, &stderr)
-		if len(stdout.String()) != 0 {
-			t.Errorf("unexpected stdout: %q", stdout.String())
+		want := ""
+		if testing.Verbose() {
+			want = "---- Shell Cleanup ----\n"
 		}
-		if len(stderr.String()) != 0 {
-			t.Errorf("unexpected stderr: %q", stderr.String())
+		if got := stdout.String(); got != "" && got != want {
+			t.Errorf("got %q, want %q", got, want)
+		}
+		if got := stderr.String(); got != "" && got != want {
+			t.Errorf("got %q, want %q", got, want)
 		}
 	}()
 	scanner := bufio.NewScanner(h.Stdout())
@@ -206,7 +210,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -220,7 +224,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -238,7 +242,7 @@
 
 	p := security.NewPrincipal("myshell")
 	cleanDebug := p.BlessingStore().DebugString()
-	sh, err := modules.NewShell(ctx, p)
+	sh, err := modules.NewShell(ctx, p, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -258,7 +262,7 @@
 	defer shutdown()
 
 	root := security.NewIDProvider("myshell")
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -306,7 +310,7 @@
 func TestNoAgent(t *testing.T) {
 	creds, _ := security.NewCredentials("noagent")
 	defer os.RemoveAll(creds)
-	sh, err := modules.NewShell(nil, nil)
+	sh, err := modules.NewShell(nil, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -321,8 +325,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	//fmt.Fprintf(os.Stderr, "B\n")
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -341,7 +344,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -355,7 +358,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -398,7 +401,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, false, t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -413,7 +416,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, false, t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -442,7 +445,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -478,7 +481,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, false, t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -490,7 +493,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -517,7 +520,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -572,7 +575,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -621,7 +624,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -641,7 +644,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -679,7 +682,7 @@
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
 
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -728,7 +731,7 @@
 func TestLIFO(t *testing.T) {
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
-	sh, err := modules.NewShell(ctx, nil)
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -759,7 +762,7 @@
 }
 
 func TestStartOpts(t *testing.T) {
-	sh, err := modules.NewShell(nil, nil)
+	sh, err := modules.NewShell(nil, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -795,7 +798,7 @@
 }
 
 func TestEmbeddedSession(t *testing.T) {
-	sh, err := modules.NewExpectShell(nil, nil, t, testing.Verbose())
+	sh, err := modules.NewShell(nil, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -808,7 +811,7 @@
 func TestCredentialsAndNoExec(t *testing.T) {
 	ctx, shutdown := test.InitForTest()
 	defer shutdown()
-	sh, err := modules.NewExpectShell(ctx, nil, t, testing.Verbose())
+	sh, err := modules.NewShell(ctx, nil, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
diff --git a/test/modules/shell.go b/test/modules/shell.go
index 4dc7e43..7878550 100644
--- a/test/modules/shell.go
+++ b/test/modules/shell.go
@@ -199,13 +199,23 @@
 // If p is non-nil, any child process created has its principal blessed
 // by the default blessings of 'p', Else any child process created has its
 // principal blessed by the default blessings of ctx's principal.
-func NewShell(ctx *context.T, p security.Principal) (*Shell, error) {
+//
+// If verbosity is true additional debugging info will be displayed,
+// in particular by the Shutdown.
+//
+// If t is non-nil, then the expect Session created for every invocation
+// will be constructed with that value of t unless overridden by a
+// StartOpts provided to that invocation. Providing a non-nil value of
+// t enables expect.Session to call t.Error, Errorf and Log.
+func NewShell(ctx *context.T, p security.Principal, verbosity bool, t expect.Testing) (*Shell, error) {
 	sh := &Shell{
 		env:              make(map[string]string),
 		handles:          make(map[Handle]struct{}),
 		config:           exec.NewConfig(),
 		defaultStartOpts: defaultStartOpts,
+		sessionVerbosity: verbosity,
 	}
+	sh.defaultStartOpts = sh.defaultStartOpts.WithSessions(t, time.Minute)
 	if ctx == nil {
 		return sh, nil
 	}
@@ -229,18 +239,6 @@
 	return sh, nil
 }
 
-// NewExpectShell creates a new instance of Shell as per NewShell, but with
-// the default StartOpts set to include the specified expect.Testing parameter.
-func NewExpectShell(ctx *context.T, p security.Principal, t expect.Testing, verbosity bool) (*Shell, error) {
-	sh, err := NewShell(ctx, p)
-	if err != nil {
-		return nil, err
-	}
-	sh.sessionVerbosity = verbosity
-	sh.SetDefaultStartOpts(DefaultStartOpts().WithSessions(t, time.Minute))
-	return sh, nil
-}
-
 // DefaultStartOpts returns the current StartOpts stored with the Shell.
 func (sh *Shell) DefaultStartOpts() StartOpts {
 	return sh.defaultStartOpts
diff --git a/test/v23tests/v23tests.go b/test/v23tests/v23tests.go
index cac131c..3cc0bb6 100644
--- a/test/v23tests/v23tests.go
+++ b/test/v23tests/v23tests.go
@@ -500,7 +500,7 @@
 		t.Fatalf("failed to set principal: %v", err)
 	}
 
-	shell, err := modules.NewShell(ctx, principal)
+	shell, err := modules.NewShell(ctx, principal, testing.Verbose(), t)
 	if err != nil {
 		t.Fatalf("NewShell() failed: %v", err)
 	}
diff --git a/test/v23tests/v23tests_test.go b/test/v23tests/v23tests_test.go
index 3acd20e..1af5d02 100644
--- a/test/v23tests/v23tests_test.go
+++ b/test/v23tests/v23tests_test.go
@@ -112,7 +112,7 @@
 }
 
 func TestDeferHandling(t *testing.T) {
-	sh, _ := modules.NewShell(nil, nil)
+	sh, _ := modules.NewShell(nil, nil, testing.Verbose(), t)
 	child, err := sh.Start("RunIntegrationTestInChild", nil, "--test.run=TestHelperProcess", "--v23.tests")
 	if err != nil {
 		t.Fatal(err)