github.com/cloudfoundry/cli@v7.1.0+incompatible/plugin/rpc/call_command_registry_test.go (about)

     1  package rpc_test
     2  
     3  import (
     4  	"os"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
     8  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
     9  	. "code.cloudfoundry.org/cli/plugin/rpc"
    10  	. "code.cloudfoundry.org/cli/plugin/rpc/fakecommand"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("calling commands in commandregistry", func() {
    16  	_ = FakeCommand1{} //make sure fake_command is imported and self-registered with init()
    17  	_ = FakeCommand3{} //make sure fake_command is imported and self-registered with init()
    18  	_ = FakeCommand4{} //make sure fake_command is imported and self-registered with init()
    19  
    20  	var (
    21  		ui         *terminalfakes.FakeUI
    22  		deps       commandregistry.Dependency
    23  		fakeLogger *tracefakes.FakePrinter
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		fakeLogger = new(tracefakes.FakePrinter)
    28  		deps = commandregistry.NewDependency(os.Stdout, fakeLogger, "")
    29  		ui = new(terminalfakes.FakeUI)
    30  		deps.UI = ui
    31  
    32  		cmd := commandregistry.Commands.FindCommand("fake-command")
    33  		commandregistry.Commands.SetCommand(cmd.SetDependency(deps, true))
    34  
    35  		cmd2 := commandregistry.Commands.FindCommand("fake-command2")
    36  		commandregistry.Commands.SetCommand(cmd2.SetDependency(deps, true))
    37  	})
    38  
    39  	When("command exists and the correct flags are passed", func() {
    40  		BeforeEach(func() {
    41  			err := NewCommandRunner().Command([]string{"fake-command"}, deps, false)
    42  			Expect(err).NotTo(HaveOccurred())
    43  		})
    44  
    45  		It("should set dependencies, execute requirements, and execute the command", func() {
    46  			Expect(ui.SayArgsForCall(0)).To(ContainSubstring("SetDependency() called, pluginCall true"))
    47  			Expect(ui.SayArgsForCall(1)).To(ContainSubstring("SetDependency() called, pluginCall false"))
    48  			Expect(ui.SayArgsForCall(2)).To(ContainSubstring("Requirement executed"))
    49  			Expect(ui.SayArgsForCall(3)).To(ContainSubstring("Command Executed"))
    50  		})
    51  	})
    52  
    53  	When("any of the command requirements fails", func() {
    54  		It("returns an error", func() {
    55  			err := NewCommandRunner().Command([]string{"fake-command2"}, deps, false)
    56  			Expect(err).To(HaveOccurred())
    57  			Expect(err.Error()).To(ContainSubstring("Requirement executed and failed"))
    58  		})
    59  	})
    60  
    61  	When("invalid flags are provided", func() {
    62  		It("returns an error", func() {
    63  			err := NewCommandRunner().Command([]string{"fake-command", "-badFlag"}, deps, false)
    64  			Expect(err).To(HaveOccurred())
    65  			Expect(err.Error()).To(ContainSubstring("Invalid flag: -badFlag"))
    66  		})
    67  	})
    68  
    69  	When("the command execute errors", func() {
    70  		BeforeEach(func() {
    71  			cmd4 := commandregistry.Commands.FindCommand("fake-command4")
    72  			commandregistry.Commands.SetCommand(cmd4.SetDependency(deps, true))
    73  		})
    74  
    75  		It("returns an error", func() {
    76  			err := NewCommandRunner().Command([]string{"fake-command4"}, deps, false)
    77  			Expect(err).To(MatchError(ErrFakeCommand4))
    78  		})
    79  	})
    80  
    81  	When("the command execute panics", func() {
    82  		BeforeEach(func() {
    83  			cmd3 := commandregistry.Commands.FindCommand("fake-command3")
    84  			commandregistry.Commands.SetCommand(cmd3.SetDependency(deps, true))
    85  		})
    86  
    87  		It("returns an error", func() {
    88  			err := NewCommandRunner().Command([]string{"fake-command3"}, deps, false)
    89  			Expect(err).To(HaveOccurred())
    90  			Expect(err.Error()).To(MatchRegexp("cli_rpc_server_test"))
    91  		})
    92  	})
    93  })