Skip to content

Commit 621a869

Browse files
authored
Merge pull request #45 from waterlink/add-new-cf-command-flavor-where-stdin-buffer-can-be-provided
Allow instantiating a CF command with provided STDIN buffer. [finishes #167944101](https://www.pivotaltracker.com/story/show/167944101)
2 parents 75adc78 + 3c60ca9 commit 621a869

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

cf/cf.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cf
22

33
import (
4+
"io"
5+
46
"github.com/cloudfoundry-incubator/cf-test-helpers/commandstarter"
57
"github.com/cloudfoundry-incubator/cf-test-helpers/internal"
68
"github.com/cloudfoundry-incubator/cf-test-helpers/silentcommandstarter"
@@ -29,3 +31,14 @@ var CfRedact = func(stringToRedact string, args ...string) *gexec.Session {
2931

3032
return internal.CfWithCustomReporter(cmdStarter, redactingReporter, args...)
3133
}
34+
35+
// CfWithStdin can be used to prepare arbitrary terminal input from the user in the tests.
36+
// Here is an example of how it can be used:
37+
//
38+
// inputConfirmingPrompt := bytes.NewBufferString("yes\n")
39+
// session := cf.CfWithStdin(inputConfirmingPrompt, "update-service", "my-service", "--upgrade")
40+
// Eventually(session).Should(Exit(0))
41+
var CfWithStdin = func(stdin io.Reader, args ...string) *gexec.Session {
42+
cmdStarter := commandstarter.NewCommandStarterWithStdin(stdin)
43+
return internal.Cf(cmdStarter, args...)
44+
}

commandstarter/command_starter.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commandstarter
22

33
import (
4+
"io"
45
"os/exec"
56
"time"
67

@@ -10,14 +11,22 @@ import (
1011
)
1112

1213
type CommandStarter struct {
14+
stdin io.Reader
1315
}
1416

1517
func NewCommandStarter() *CommandStarter {
1618
return &CommandStarter{}
1719
}
1820

21+
func NewCommandStarterWithStdin(stdin io.Reader) *CommandStarter {
22+
return &CommandStarter{
23+
stdin: stdin,
24+
}
25+
}
26+
1927
func (r *CommandStarter) Start(reporter internal.Reporter, executable string, args ...string) (*gexec.Session, error) {
2028
cmd := exec.Command(executable, args...)
29+
cmd.Stdin = r.stdin
2130
reporter.Report(time.Now(), cmd)
2231

2332
return gexec.Start(cmd, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter)

commandstarter/command_starter_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package commandstarter_test
22

33
import (
4+
"bytes"
5+
"io"
46
"os/exec"
57
"time"
68

79
"github.com/cloudfoundry-incubator/cf-test-helpers/commandstarter"
810

911
. "github.com/onsi/ginkgo"
1012
. "github.com/onsi/gomega"
13+
. "github.com/onsi/gomega/gbytes"
1114
)
1215

1316
type fakeReporter struct {
@@ -32,7 +35,25 @@ var _ = Describe("CommandStarter", func() {
3235
})
3336

3437
It("reports the command that it's running", func() {
35-
cmdStarter.Start(reporter, "bash", "-c", "echo \"hello world\"")
38+
session, err := cmdStarter.Start(reporter, "bash", "-c", "echo \"hello world\"")
39+
Expect(err).To(Succeed())
3640
Expect(reporter.calledWith.cmd.Args).To(Equal([]string{"bash", "-c", "echo \"hello world\""}))
41+
Eventually(session).Should(Say("hello world"))
42+
})
43+
44+
When("created with stdin", func() {
45+
var stdin io.Reader
46+
47+
BeforeEach(func() {
48+
stdin = bytes.NewBufferString("name from input")
49+
cmdStarter = commandstarter.NewCommandStarterWithStdin(stdin)
50+
})
51+
52+
It("reports what command is running and sends the input to the command", func() {
53+
session, err := cmdStarter.Start(reporter, "bash", "-c", `echo "hello $(cat -)"`)
54+
Expect(err).To(Succeed())
55+
Expect(reporter.calledWith.cmd.Args).To(Equal([]string{"bash", "-c", `echo "hello $(cat -)"`}))
56+
Eventually(session).Should(Say("hello name from input"))
57+
})
3758
})
3859
})

0 commit comments

Comments
 (0)