github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/help_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/command_registry"
     5  	"github.com/cloudfoundry/cli/cf/configuration/plugin_config"
     6  	testconfig "github.com/cloudfoundry/cli/cf/configuration/plugin_config/fakes"
     7  	"github.com/cloudfoundry/cli/commands_loader"
     8  	"github.com/cloudfoundry/cli/plugin"
     9  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    10  	io_helpers "github.com/cloudfoundry/cli/testhelpers/io"
    11  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    12  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    13  
    14  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("Help", func() {
    20  
    21  	commands_loader.Load()
    22  
    23  	var (
    24  		ui                  *testterm.FakeUI
    25  		requirementsFactory *testreq.FakeReqFactory
    26  		config              *testconfig.FakePluginConfiguration
    27  		deps                command_registry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.Ui = ui
    32  		deps.PluginConfig = config
    33  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("help").SetDependency(deps, pluginCall))
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		ui = &testterm.FakeUI{}
    38  		requirementsFactory = &testreq.FakeReqFactory{}
    39  		config = &testconfig.FakePluginConfiguration{}
    40  	})
    41  
    42  	runCommand := func(args ...string) bool {
    43  		return testcmd.RunCliCommand("help", args, requirementsFactory, updateCommandDependency, false)
    44  	}
    45  
    46  	Context("when no argument is provided", func() {
    47  		It("prints the main help menu of the 'cf' app", func() {
    48  			outputs := io_helpers.CaptureOutput(func() { runCommand() })
    49  
    50  			Eventually(outputs).Should(ContainSubstrings([]string{"A command line tool to interact with Cloud Foundry"}))
    51  			Eventually(outputs).Should(ContainSubstrings([]string{"CF_TRACE=true"}))
    52  		})
    53  	})
    54  
    55  	Context("when a command name is provided as an argument", func() {
    56  		Context("When the command exists", func() {
    57  			It("prints the usage help for the command", func() {
    58  				runCommand("target")
    59  
    60  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"target - Set or view the targeted org or space"}))
    61  			})
    62  		})
    63  
    64  		Context("When the command exists", func() {
    65  			It("prints the usage help for the command", func() {
    66  				runCommand("bad-command")
    67  
    68  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"'bad-command' is not a registered command. See 'cf help'"}))
    69  			})
    70  		})
    71  	})
    72  
    73  	Context("when a command provided is a plugin command", func() {
    74  		BeforeEach(func() {
    75  			m := make(map[string]plugin_config.PluginMetadata)
    76  			m["fakePlugin"] = plugin_config.PluginMetadata{
    77  				Commands: []plugin.Command{
    78  					plugin.Command{
    79  						Name:     "fakePluginCmd1",
    80  						Alias:    "fpc1",
    81  						HelpText: "help text here",
    82  						UsageDetails: plugin.Usage{
    83  							Usage: "Usage for fpc1",
    84  							Options: map[string]string{
    85  								"f": "test flag",
    86  							},
    87  						},
    88  					},
    89  				},
    90  			}
    91  
    92  			config.PluginsReturns(m)
    93  		})
    94  
    95  		Context("command is a plugin command name", func() {
    96  			It("prints the usage help for the command", func() {
    97  				runCommand("fakePluginCmd1")
    98  
    99  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"fakePluginCmd1", "help text here"}))
   100  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"ALIAS"}))
   101  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"fpc1"}))
   102  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"USAGE"}))
   103  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"Usage for fpc1"}))
   104  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"OPTIONS"}))
   105  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"-f", "test flag"}))
   106  			})
   107  		})
   108  
   109  		Context("command is a plugin command alias", func() {
   110  			It("prints the usage help for the command alias", func() {
   111  				runCommand("fpc1")
   112  
   113  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"fakePluginCmd1", "help text here"}))
   114  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"ALIAS"}))
   115  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"fpc1"}))
   116  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"USAGE"}))
   117  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"Usage for fpc1"}))
   118  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"OPTIONS"}))
   119  				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"-f", "test flag"}))
   120  			})
   121  		})
   122  
   123  	})
   124  })