diff --git a/cf/terminal/fakes/fake_output_capture.go b/cf/terminal/fakes/fake_output_capture.go index 4af5ae9be65..f60538a860e 100644 --- a/cf/terminal/fakes/fake_output_capture.go +++ b/cf/terminal/fakes/fake_output_capture.go @@ -2,40 +2,40 @@ package fakes import ( - . "github.com/cloudfoundry/cli/cf/terminal" "sync" + + "github.com/cloudfoundry/cli/cf/terminal" ) type FakeOutputCapture struct { - GetOutputAndResetStub func() []string - getOutputAndResetMutex sync.RWMutex - getOutputAndResetArgsForCall []struct{} - getOutputAndResetReturns struct { - result1 []string + SetOutputBucketStub func(*[]string) + setOutputBucketMutex sync.RWMutex + setOutputBucketArgsForCall []struct { + arg1 *[]string } } -func (fake *FakeOutputCapture) GetOutputAndReset() []string { - fake.getOutputAndResetMutex.Lock() - defer fake.getOutputAndResetMutex.Unlock() - fake.getOutputAndResetArgsForCall = append(fake.getOutputAndResetArgsForCall, struct{}{}) - if fake.GetOutputAndResetStub != nil { - return fake.GetOutputAndResetStub() - } else { - return fake.getOutputAndResetReturns.result1 +func (fake *FakeOutputCapture) SetOutputBucket(arg1 *[]string) { + fake.setOutputBucketMutex.Lock() + fake.setOutputBucketArgsForCall = append(fake.setOutputBucketArgsForCall, struct { + arg1 *[]string + }{arg1}) + fake.setOutputBucketMutex.Unlock() + if fake.SetOutputBucketStub != nil { + fake.SetOutputBucketStub(arg1) } } -func (fake *FakeOutputCapture) GetOutputAndResetCallCount() int { - fake.getOutputAndResetMutex.RLock() - defer fake.getOutputAndResetMutex.RUnlock() - return len(fake.getOutputAndResetArgsForCall) +func (fake *FakeOutputCapture) SetOutputBucketCallCount() int { + fake.setOutputBucketMutex.RLock() + defer fake.setOutputBucketMutex.RUnlock() + return len(fake.setOutputBucketArgsForCall) } -func (fake *FakeOutputCapture) GetOutputAndResetReturns(result1 []string) { - fake.getOutputAndResetReturns = struct { - result1 []string - }{result1} +func (fake *FakeOutputCapture) SetOutputBucketArgsForCall(i int) *[]string { + fake.setOutputBucketMutex.RLock() + defer fake.setOutputBucketMutex.RUnlock() + return fake.setOutputBucketArgsForCall[i].arg1 } -var _ OutputCapture = new(FakeOutputCapture) +var _ terminal.OutputCapture = new(FakeOutputCapture) diff --git a/cf/terminal/fakes/fake_printer.go b/cf/terminal/fakes/fake_printer.go index 0df5bf5a598..e09a769d88f 100644 --- a/cf/terminal/fakes/fake_printer.go +++ b/cf/terminal/fakes/fake_printer.go @@ -66,9 +66,6 @@ type FakePrinter struct { } } -func (fake *FakePrinter) DisableTerminalOutput(bool) { -} - func (fake *FakePrinter) Print(a ...interface{}) (n int, err error) { fake.printMutex.Lock() defer fake.printMutex.Unlock() diff --git a/cf/terminal/tee_printer.go b/cf/terminal/tee_printer.go index e1509eb2798..a9e3addda2b 100644 --- a/cf/terminal/tee_printer.go +++ b/cf/terminal/tee_printer.go @@ -11,11 +11,10 @@ type Printer interface { ForcePrint(a ...interface{}) (n int, err error) ForcePrintf(format string, a ...interface{}) (n int, err error) ForcePrintln(a ...interface{}) (n int, err error) - DisableTerminalOutput(bool) } type OutputCapture interface { - GetOutputAndReset() []string + SetOutputBucket(*[]string) } type TerminalOutputSwitch interface { @@ -24,24 +23,20 @@ type TerminalOutputSwitch interface { type TeePrinter struct { disableTerminalOutput bool - output []string + outputBucket *[]string } func NewTeePrinter() *TeePrinter { - return &TeePrinter{ - output: []string{}, - } + return &TeePrinter{} } -func (t *TeePrinter) GetOutputAndReset() []string { - currentOutput := t.output - t.output = []string{} - return currentOutput +func (t *TeePrinter) SetOutputBucket(bucket *[]string) { + t.outputBucket = bucket } func (t *TeePrinter) Print(values ...interface{}) (n int, err error) { str := fmt.Sprint(values...) - t.output = append(t.output, Decolorize(str)) + t.saveOutputToBucket(str) if !t.disableTerminalOutput { return fmt.Print(str) } @@ -50,7 +45,7 @@ func (t *TeePrinter) Print(values ...interface{}) (n int, err error) { func (t *TeePrinter) Printf(format string, a ...interface{}) (n int, err error) { str := fmt.Sprintf(format, a...) - t.output = append(t.output, Decolorize(str)) + t.saveOutputToBucket(str) if !t.disableTerminalOutput { return fmt.Print(str) } @@ -59,7 +54,7 @@ func (t *TeePrinter) Printf(format string, a ...interface{}) (n int, err error) func (t *TeePrinter) Println(values ...interface{}) (n int, err error) { str := fmt.Sprint(values...) - t.output = append(t.output, Decolorize(str)) + t.saveOutputToBucket(str) if !t.disableTerminalOutput { return fmt.Println(str) } @@ -68,22 +63,30 @@ func (t *TeePrinter) Println(values ...interface{}) (n int, err error) { func (t *TeePrinter) ForcePrint(values ...interface{}) (n int, err error) { str := fmt.Sprint(values...) - t.output = append(t.output, Decolorize(str)) + t.saveOutputToBucket(str) return fmt.Print(str) } func (t *TeePrinter) ForcePrintf(format string, a ...interface{}) (n int, err error) { str := fmt.Sprintf(format, a...) - t.output = append(t.output, Decolorize(str)) + t.saveOutputToBucket(str) return fmt.Print(str) } func (t *TeePrinter) ForcePrintln(values ...interface{}) (n int, err error) { str := fmt.Sprint(values...) - t.output = append(t.output, Decolorize(str)) + t.saveOutputToBucket(str) return fmt.Println(str) } func (t *TeePrinter) DisableTerminalOutput(disable bool) { t.disableTerminalOutput = disable } + +func (t *TeePrinter) saveOutputToBucket(output string) { + if t.outputBucket == nil { + return + } + + *t.outputBucket = append(*t.outputBucket, Decolorize(output)) +} diff --git a/cf/terminal/tee_printer_test.go b/cf/terminal/tee_printer_test.go index 72cd5ae077a..6b6ee3900f5 100644 --- a/cf/terminal/tee_printer_test.go +++ b/cf/terminal/tee_printer_test.go @@ -15,9 +15,14 @@ var _ = Describe("TeePrinter", func() { ) Describe(".Print", func() { + var bucket *[]string + BeforeEach(func() { + bucket = &[]string{} + output = io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.Print("Hello ") printer.Print("Mom!") }) @@ -28,26 +33,31 @@ var _ = Describe("TeePrinter", func() { }) It("should save the output to the slice", func() { - outputs := printer.GetOutputAndReset() - Expect(outputs[0]).To(Equal("Hello ")) - Expect(outputs[1]).To(Equal("Mom!")) + Expect((*bucket)[0]).To(Equal("Hello ")) + Expect((*bucket)[1]).To(Equal("Mom!")) }) It("should decolorize text", func() { + bucket = &[]string{} io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.Print("hi " + EntityNameColor("foo")) }) - output = printer.GetOutputAndReset() - Expect(output[0]).To(Equal("hi foo")) + Expect((*bucket)[0]).To(Equal("hi foo")) }) }) Describe(".Printf", func() { + var bucket *[]string + BeforeEach(func() { + bucket = &[]string{} + output = io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.Printf("Hello %s", "everybody") }) }) @@ -57,24 +67,29 @@ var _ = Describe("TeePrinter", func() { }) It("should save the output to the slice", func() { - Expect(printer.GetOutputAndReset()[0]).To(Equal("Hello everybody")) + Expect((*bucket)[0]).To(Equal("Hello everybody")) }) It("should decolorize text", func() { + bucket = &[]string{} io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.Printf("hi %s", EntityNameColor("foo")) }) - output = printer.GetOutputAndReset() - Expect(output[0]).To(Equal("hi foo")) + Expect((*bucket)[0]).To(Equal("hi foo")) }) }) Describe(".Println", func() { + var bucket *[]string BeforeEach(func() { + bucket = &[]string{} + output = io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.Println("Hello ", "everybody") }) }) @@ -84,24 +99,30 @@ var _ = Describe("TeePrinter", func() { }) It("should save the output to the slice", func() { - Expect(printer.GetOutputAndReset()[0]).To(Equal("Hello everybody")) + Expect((*bucket)[0]).To(Equal("Hello everybody")) }) It("should decolorize text", func() { + bucket = &[]string{} io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.Println("hi " + EntityNameColor("foo")) }) - output = printer.GetOutputAndReset() - Expect(output[0]).To(Equal("hi foo")) + Expect((*bucket)[0]).To(Equal("hi foo")) }) }) Describe(".ForcePrintf", func() { + var bucket *[]string + BeforeEach(func() { + bucket = &[]string{} + output = io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.ForcePrintf("Hello %s", "everybody") }) }) @@ -111,24 +132,30 @@ var _ = Describe("TeePrinter", func() { }) It("should save the output to the slice", func() { - Expect(printer.GetOutputAndReset()[0]).To(Equal("Hello everybody")) + Expect((*bucket)[0]).To(Equal("Hello everybody")) }) It("should decolorize text", func() { + bucket = &[]string{} io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.Printf("hi %s", EntityNameColor("foo")) }) - output = printer.GetOutputAndReset() - Expect(output[0]).To(Equal("hi foo")) + Expect((*bucket)[0]).To(Equal("hi foo")) }) }) Describe(".ForcePrintln", func() { + var bucket *[]string + BeforeEach(func() { + bucket = &[]string{} + output = io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.ForcePrintln("Hello ", "everybody") }) }) @@ -138,39 +165,50 @@ var _ = Describe("TeePrinter", func() { }) It("should save the output to the slice", func() { - Expect(printer.GetOutputAndReset()[0]).To(Equal("Hello everybody")) + Expect((*bucket)[0]).To(Equal("Hello everybody")) }) It("should decolorize text", func() { + bucket = &[]string{} io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.Println("hi " + EntityNameColor("foo")) }) - output = printer.GetOutputAndReset() - Expect(output[0]).To(Equal("hi foo")) + Expect((*bucket)[0]).To(Equal("hi foo")) }) }) - Describe(".GetOutputAndReset", func() { - BeforeEach(func() { - output = io_helpers.CaptureOutput(func() { - printer = NewTeePrinter() - printer.Print("Hello") - printer.Print("Mom!") - }) + Describe(".SetOutputBucket", func() { + var bucket *[]string + + output = io_helpers.CaptureOutput(func() { + bucket = &[]string{} + printer = NewTeePrinter() + printer.SetOutputBucket(bucket) + printer.ForcePrintf("Hello %s", "everybody") }) - It("should clear the slice after access", func() { - printer.GetOutputAndReset() - Expect(printer.GetOutputAndReset()).To(BeEmpty()) + It("sets the []string used to save the output", func() { + Expect((*bucket)[0]).To(Equal("Hello everybody")) + }) + + It("disables the output saving when set to nil", func() { + printer.SetOutputBucket(nil) + Expect((*bucket)[0]).To(Equal("Hello everybody")) }) }) Describe("Pausing Output", func() { + var bucket *[]string + BeforeEach(func() { + bucket = &[]string{} + output = io_helpers.CaptureOutput(func() { printer = NewTeePrinter() + printer.SetOutputBucket(bucket) printer.DisableTerminalOutput(true) printer.Print("Hello") printer.Println("Mom!") @@ -186,13 +224,16 @@ var _ = Describe("TeePrinter", func() { }) It("should still capture all output", func() { - Expect(printer.GetOutputAndReset()).To(Equal([]string{"Hello", "Mom!", "Dad!", "Forced Hello", "Forced Mom", "Forced Dad"})) + Expect(*bucket).To(Equal([]string{"Hello", "Mom!", "Dad!", "Forced Hello", "Forced Mom", "Forced Dad"})) }) Describe(".ResumeOutput", func() { + var bucket *[]string BeforeEach(func() { - printer.GetOutputAndReset() + bucket = &[]string{} + output = io_helpers.CaptureOutput(func() { + printer.SetOutputBucket(bucket) printer.DisableTerminalOutput(false) printer.Print("Hello") printer.Println("Mom!") @@ -209,7 +250,7 @@ var _ = Describe("TeePrinter", func() { }) It("should capture all output", func() { - Expect(printer.GetOutputAndReset()).To(Equal([]string{"Hello", "Mom!", "Dad!", "Grandpa!", "ForcePrint", "ForcePrintln", "ForcePrintf"})) + Expect(*bucket).To(Equal([]string{"Hello", "Mom!", "Dad!", "Grandpa!", "ForcePrint", "ForcePrintln", "ForcePrintf"})) }) }) }) diff --git a/cf/terminal/ui.go b/cf/terminal/ui.go index 9f105d6141e..f1d8375140e 100644 --- a/cf/terminal/ui.go +++ b/cf/terminal/ui.go @@ -40,7 +40,6 @@ type UI interface { LoadingIndication() Wait(duration time.Duration) Table(headers []string) Table - DisableTerminalOutput(bool) } type terminalUI struct { @@ -55,10 +54,6 @@ func NewUI(r io.Reader, printer Printer) UI { } } -func (c *terminalUI) DisableTerminalOutput(disable bool) { - c.printer.DisableTerminalOutput(disable) -} - func (c *terminalUI) PrintPaginator(rows []string, err error) { if err != nil { c.Failed(err.Error()) diff --git a/cf/terminal/ui_test.go b/cf/terminal/ui_test.go index 9fa7a3633f0..41831a826a5 100644 --- a/cf/terminal/ui_test.go +++ b/cf/terminal/ui_test.go @@ -22,7 +22,11 @@ var _ = Describe("UI", func() { Describe("Printing message to stdout with PrintCapturingNoOutput", func() { It("prints strings without using the TeePrinter", func() { + bucket := &[]string{} + printer := NewTeePrinter() + printer.SetOutputBucket(bucket) + io_helpers.SimulateStdin("", func(reader io.Reader) { output := io_helpers.CaptureOutput(func() { ui := NewUI(reader, printer) @@ -30,7 +34,7 @@ var _ = Describe("UI", func() { }) Expect("Hello").To(Equal(strings.Join(output, ""))) - Expect(len(printer.GetOutputAndReset())).To(Equal(0)) + Expect(len(*bucket)).To(Equal(0)) }) }) }) diff --git a/main/main_test.go b/main/main_test.go index 58d083e5e7a..c9dae91f182 100644 --- a/main/main_test.go +++ b/main/main_test.go @@ -5,7 +5,6 @@ import ( "os" "os/exec" "path/filepath" - "strings" "time" . "github.com/onsi/ginkgo" @@ -51,15 +50,12 @@ var _ = Describe("main", func() { }) It("runs requirement of the non-codegangsta command", func() { - result := Cf("api") - result2 := Cf("app", "app-should-never-exist-blah-blah") + dir, err := os.Getwd() + Expect(err).ToNot(HaveOccurred()) + fullDir := filepath.Join(dir, "..", "fixtures") //set home to a config w/o targeted api + result := CfWith_CF_HOME(fullDir, "app", "app-should-never-exist-blah-blah") - if strings.Contains(string(result.Out.Contents()), "No api endpoint set") { - Eventually(result2.Out).Should(Say("No API endpoint set.")) - } else { - Eventually(result2.Out).Should(Say("App app-should-never-exist-blah-blah not found")) - Consistently(result2.Out).ShouldNot(Say("Server error")) - } + Eventually(result.Out).Should(Say("No API endpoint set.")) }) }) @@ -277,6 +273,17 @@ func CfWithIo(command string, args string) *Session { return session } +func CfWith_CF_HOME(cfHome string, args ...string) *Session { + path, err := Build("github.com/cloudfoundry/cli/main") + Expect(err).NotTo(HaveOccurred()) + + cmd := exec.Command(path, args...) + cmd.Env = append(cmd.Env, "CF_HOME="+cfHome) + session, err := Start(cmd, GinkgoWriter, GinkgoWriter) + Expect(err).NotTo(HaveOccurred()) + + return session +} // gexec.Build leaves a compiled binary behind in /tmp. var _ = AfterSuite(func() { diff --git a/plugin/rpc/call_command_registry.go b/plugin/rpc/call_command_registry.go index 3c76fc94b40..58431fda2f0 100644 --- a/plugin/rpc/call_command_registry.go +++ b/plugin/rpc/call_command_registry.go @@ -9,7 +9,7 @@ import ( ) type NonCodegangstaRunner interface { - Command([]string, command_registry.Dependency) error + Command([]string, command_registry.Dependency, bool) error } type nonCodegangstaRunner struct{} @@ -18,7 +18,7 @@ func NewNonCodegangstaRunner() NonCodegangstaRunner { return &nonCodegangstaRunner{} } -func (c *nonCodegangstaRunner) Command(args []string, deps command_registry.Dependency) error { +func (c *nonCodegangstaRunner) Command(args []string, deps command_registry.Dependency, pluginApiCall bool) error { var err error cmdRegistry := command_registry.Commands @@ -31,7 +31,7 @@ func (c *nonCodegangstaRunner) Command(args []string, deps command_registry.Depe } cfCmd := cmdRegistry.FindCommand(args[0]) - cfCmd = cfCmd.SetDependency(deps, true) + cfCmd = cfCmd.SetDependency(deps, pluginApiCall) reqs, err := cfCmd.Requirements(requirements.NewFactory(deps.Ui, deps.Config, deps.RepoLocator), fc) if err != nil { diff --git a/plugin/rpc/call_command_registry_test.go b/plugin/rpc/call_command_registry_test.go index 0f732a5df4f..2c5bff3584c 100644 --- a/plugin/rpc/call_command_registry_test.go +++ b/plugin/rpc/call_command_registry_test.go @@ -33,29 +33,29 @@ var _ = Describe("calling commands in command_registry", func() { }) It("runs the command requirements", func() { - NewNonCodegangstaRunner().Command([]string{"fake-non-codegangsta-command"}, deps) + NewNonCodegangstaRunner().Command([]string{"fake-non-codegangsta-command"}, deps, false) Expect(ui.Outputs).To(ContainSubstrings([]string{"Requirement executed"})) }) It("calls the command Execute() func", func() { - NewNonCodegangstaRunner().Command([]string{"fake-non-codegangsta-command"}, deps) + NewNonCodegangstaRunner().Command([]string{"fake-non-codegangsta-command"}, deps, false) Expect(ui.Outputs).To(ContainSubstrings([]string{"Command Executed"})) }) It("sets the dependency of the command", func() { - NewNonCodegangstaRunner().Command([]string{"fake-non-codegangsta-command"}, deps) + NewNonCodegangstaRunner().Command([]string{"fake-non-codegangsta-command"}, deps, false) Expect(ui.Outputs).To(ContainSubstrings([]string{"SetDependency() called, pluginCall true"})) }) It("returns an error if any of the requirements fail", func() { - err := NewNonCodegangstaRunner().Command([]string{"fake-non-codegangsta-command2"}, deps) + err := NewNonCodegangstaRunner().Command([]string{"fake-non-codegangsta-command2"}, deps, false) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Error in requirement")) }) It("returns an error if invalid flag is provided", func() { - err := NewNonCodegangstaRunner().Command([]string{"fake-non-codegangsta-command", "-badFlag"}, deps) + err := NewNonCodegangstaRunner().Command([]string{"fake-non-codegangsta-command", "-badFlag"}, deps, false) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Invalid flag: -badFlag")) diff --git a/plugin/rpc/cli_rpc_server.go b/plugin/rpc/cli_rpc_server.go index 731df390a6d..384a51975cd 100644 --- a/plugin/rpc/cli_rpc_server.go +++ b/plugin/rpc/cli_rpc_server.go @@ -1,6 +1,8 @@ package rpc import ( + "os" + "github.com/cloudfoundry/cli/cf/api" "github.com/cloudfoundry/cli/cf/command_registry" "github.com/cloudfoundry/cli/cf/configuration/core_config" @@ -30,6 +32,7 @@ type CliRpcCmd struct { cliConfig core_config.Repository repoLocator api.RepositoryLocator newCmdRunner NonCodegangstaRunner + outputBucket *[]string } func NewRpcService(commandRunner *cli.App, outputCapture terminal.OutputCapture, terminalOutputSwitch terminal.TerminalOutputSwitch, cliConfig core_config.Repository, repoLocator api.RepositoryLocator, newCmdRunner NonCodegangstaRunner) (*CliRpcService, error) { @@ -115,6 +118,9 @@ func (cmd *CliRpcCmd) CallCoreCommand(args []string, retVal *bool) error { var err error cmdRegistry := command_registry.Commands + cmd.outputBucket = &[]string{} + cmd.outputCapture.SetOutputBucket(cmd.outputBucket) + if cmdRegistry.CommandExists(args[0]) { deps := command_registry.NewDependency() @@ -122,8 +128,9 @@ func (cmd *CliRpcCmd) CallCoreCommand(args []string, retVal *bool) error { //once all commands are converted, we can make fresh deps for each command run deps.Config = cmd.cliConfig deps.RepoLocator = cmd.repoLocator + deps.Ui = terminal.NewUI(os.Stdin, cmd.outputCapture.(*terminal.TeePrinter)) - err = cmd.newCmdRunner.Command(args, deps) + err = cmd.newCmdRunner.Command(args, deps, false) } else { //call codegangsta command err = cmd.coreCommandRunner.Run(append([]string{"CF_NAME"}, args...)) @@ -139,7 +146,7 @@ func (cmd *CliRpcCmd) CallCoreCommand(args []string, retVal *bool) error { } func (cmd *CliRpcCmd) GetOutputAndReset(args bool, retVal *[]string) error { - *retVal = cmd.outputCapture.GetOutputAndReset() + *retVal = *cmd.outputBucket return nil } @@ -254,7 +261,8 @@ func (cmd *CliRpcCmd) GetApp(appName string, retVal *plugin_models.Application) deps.Config = cmd.cliConfig deps.RepoLocator = cmd.repoLocator deps.PluginModels.Application = retVal - deps.Ui.DisableTerminalOutput(true) + cmd.terminalOutputSwitch.DisableTerminalOutput(true) + deps.Ui = terminal.NewUI(os.Stdin, cmd.terminalOutputSwitch.(*terminal.TeePrinter)) - return cmd.newCmdRunner.Command([]string{"app", appName}, deps) + return cmd.newCmdRunner.Command([]string{"app", appName}, deps, true) } diff --git a/plugin/rpc/cli_rpc_server_test.go b/plugin/rpc/cli_rpc_server_test.go index 04d767f0f8d..689e605985c 100644 --- a/plugin/rpc/cli_rpc_server_test.go +++ b/plugin/rpc/cli_rpc_server_test.go @@ -3,15 +3,18 @@ package rpc_test import ( "net" "net/rpc" + "os" "time" "github.com/cloudfoundry/cli/cf/api" "github.com/cloudfoundry/cli/cf/configuration/core_config" "github.com/cloudfoundry/cli/cf/models" + "github.com/cloudfoundry/cli/cf/terminal" "github.com/cloudfoundry/cli/cf/terminal/fakes" "github.com/cloudfoundry/cli/plugin" "github.com/cloudfoundry/cli/plugin/models" . "github.com/cloudfoundry/cli/plugin/rpc" + cmdRunner "github.com/cloudfoundry/cli/plugin/rpc" . "github.com/cloudfoundry/cli/plugin/rpc/fake_command" fakeRunner "github.com/cloudfoundry/cli/plugin/rpc/fakes" testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" @@ -145,9 +148,8 @@ var _ = Describe("Server", func() { Describe(".GetOutputAndReset", func() { Context("success", func() { BeforeEach(func() { - outputCapture := &fakes.FakeOutputCapture{} - outputCapture.GetOutputAndResetReturns([]string{"hi from command"}) - rpcService, err = NewRpcService(nil, outputCapture, nil, nil, api.RepositoryLocator{}, nil) + outputCapture := terminal.NewTeePrinter() + rpcService, err = NewRpcService(nil, outputCapture, nil, nil, api.RepositoryLocator{}, cmdRunner.NewNonCodegangstaRunner()) Expect(err).ToNot(HaveOccurred()) err := rpcService.Start() @@ -166,10 +168,19 @@ var _ = Describe("Server", func() { It("should return the logs from the output capture", func() { client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) Expect(err).ToNot(HaveOccurred()) + + success := false + + oldStd := os.Stdout + os.Stdout = nil + client.Call("CliRpcCmd.CallCoreCommand", []string{"fake-non-codegangsta-command"}, &success) + Expect(success).To(BeTrue()) + os.Stdout = oldStd + var output []string client.Call("CliRpcCmd.GetOutputAndReset", false, &output) - Expect(output).To(Equal([]string{"hi from command"})) + Expect(output).To(Equal([]string{"Requirement executed\n", "Command Executed\n"})) }) }) }) @@ -206,10 +217,11 @@ var _ = Describe("Server", func() { var runner *fakeRunner.FakeNonCodegangstaRunner BeforeEach(func() { - outputCapture := &fakes.FakeOutputCapture{} + outputCapture := terminal.NewTeePrinter() + terminalOutputSwitch := terminal.NewTeePrinter() runner = &fakeRunner.FakeNonCodegangstaRunner{} - rpcService, err = NewRpcService(nil, outputCapture, nil, nil, api.RepositoryLocator{}, runner) + rpcService, err = NewRpcService(nil, outputCapture, terminalOutputSwitch, nil, api.RepositoryLocator{}, runner) Expect(err).ToNot(HaveOccurred()) err := rpcService.Start() @@ -234,9 +246,10 @@ var _ = Describe("Server", func() { Expect(err).ToNot(HaveOccurred()) Expect(runner.CommandCallCount()).To(Equal(1)) - arg1, _ := runner.CommandArgsForCall(0) + arg1, _, pluginApiCall := runner.CommandArgsForCall(0) Expect(arg1[0]).To(Equal("app")) Expect(arg1[1]).To(Equal("fake-app")) + Expect(pluginApiCall).To(BeTrue()) }) }) @@ -259,7 +272,7 @@ var _ = Describe("Server", func() { }, } - outputCapture := &fakes.FakeOutputCapture{} + outputCapture := terminal.NewTeePrinter() runner = &fakeRunner.FakeNonCodegangstaRunner{} rpcService, err = NewRpcService(app, outputCapture, nil, nil, api.RepositoryLocator{}, runner) @@ -286,7 +299,10 @@ var _ = Describe("Server", func() { err = client.Call("CliRpcCmd.CallCoreCommand", []string{"fake-non-codegangsta-command3"}, &success) Expect(err).ToNot(HaveOccurred()) - Expect(success).To(BeTrue()) + Expect(runner.CommandCallCount()).To(Equal(1)) + + _, _, pluginApiCall := runner.CommandArgsForCall(0) + Expect(pluginApiCall).To(BeFalse()) }) It("calls the code gangsta cli App command", func() { diff --git a/plugin/rpc/fakes/fake_non_codegangsta_runner.go b/plugin/rpc/fakes/fake_non_codegangsta_runner.go index 0140cfa0f9c..7aa9c0c7b57 100644 --- a/plugin/rpc/fakes/fake_non_codegangsta_runner.go +++ b/plugin/rpc/fakes/fake_non_codegangsta_runner.go @@ -9,26 +9,28 @@ import ( ) type FakeNonCodegangstaRunner struct { - CommandStub func([]string, command_registry.Dependency) error + CommandStub func([]string, command_registry.Dependency, bool) error commandMutex sync.RWMutex commandArgsForCall []struct { arg1 []string arg2 command_registry.Dependency + arg3 bool } commandReturns struct { result1 error } } -func (fake *FakeNonCodegangstaRunner) Command(arg1 []string, arg2 command_registry.Dependency) error { +func (fake *FakeNonCodegangstaRunner) Command(arg1 []string, arg2 command_registry.Dependency, arg3 bool) error { fake.commandMutex.Lock() fake.commandArgsForCall = append(fake.commandArgsForCall, struct { arg1 []string arg2 command_registry.Dependency - }{arg1, arg2}) + arg3 bool + }{arg1, arg2, arg3}) fake.commandMutex.Unlock() if fake.CommandStub != nil { - return fake.CommandStub(arg1, arg2) + return fake.CommandStub(arg1, arg2, arg3) } else { return fake.commandReturns.result1 } @@ -40,10 +42,10 @@ func (fake *FakeNonCodegangstaRunner) CommandCallCount() int { return len(fake.commandArgsForCall) } -func (fake *FakeNonCodegangstaRunner) CommandArgsForCall(i int) ([]string, command_registry.Dependency) { +func (fake *FakeNonCodegangstaRunner) CommandArgsForCall(i int) ([]string, command_registry.Dependency, bool) { fake.commandMutex.RLock() defer fake.commandMutex.RUnlock() - return fake.commandArgsForCall[i].arg1, fake.commandArgsForCall[i].arg2 + return fake.commandArgsForCall[i].arg1, fake.commandArgsForCall[i].arg2, fake.commandArgsForCall[i].arg3 } func (fake *FakeNonCodegangstaRunner) CommandReturns(result1 error) { diff --git a/testhelpers/terminal/ui.go b/testhelpers/terminal/ui.go index f8929fa2146..ad2749c2737 100644 --- a/testhelpers/terminal/ui.go +++ b/testhelpers/terminal/ui.go @@ -39,8 +39,6 @@ func (ui *FakeUI) PrintPaginator(rows []string, err error) { } } -func (ui *FakeUI) DisableTerminalOutput(bool) {} - func (ui *FakeUI) PrintCapturingNoOutput(message string, args ...interface{}) { ui.sayMutex.Lock() defer ui.sayMutex.Unlock()