github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/help_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  
     7  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/ginkgo/extensions/table"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("help command", func() {
    17  	It("appears in cf help -a", func() {
    18  		session := helpers.CF("help", "-a")
    19  		Eventually(session).Should(Exit(0))
    20  		Expect(session).To(HaveCommandInCategoryWithDescription("help", "GETTING STARTED", "Show help"))
    21  	})
    22  
    23  	DescribeTable("displays help for common commands",
    24  		func(setup func() *exec.Cmd) {
    25  			cmd := setup()
    26  			session, err := Start(cmd, GinkgoWriter, GinkgoWriter)
    27  			Expect(err).NotTo(HaveOccurred())
    28  
    29  			Eventually(session).Should(Say("Cloud Foundry command line tool"))
    30  			Eventually(session).Should(Say(`\[global options\] command \[arguments...\] \[command options\]`))
    31  			Eventually(session).Should(Say("Before getting started:"))
    32  			Eventually(session).Should(Say(`  config\s+login,l\s+target,t`))
    33  			Eventually(session).Should(Say("Application lifecycle:"))
    34  			Eventually(session).Should(Say(`  apps,a\s+run-task,rt\s+events`))
    35  			Eventually(session).Should(Say(`  restage,rg\s+scale`))
    36  
    37  			Eventually(session).Should(Say("Services integration:"))
    38  			Eventually(session).Should(Say(`  marketplace,m\s+create-user-provided-service,cups`))
    39  			Eventually(session).Should(Say(`  services,s\s+update-user-provided-service,uups`))
    40  
    41  			Eventually(session).Should(Say("Route and domain management:"))
    42  			Eventually(session).Should(Say(`  routes,r\s+delete-route\s+create-private-domain`))
    43  			Eventually(session).Should(Say(`  domains\s+map-route`))
    44  
    45  			Eventually(session).Should(Say("Space management:"))
    46  			Eventually(session).Should(Say(`  spaces\s+create-space,csp\s+set-space-role`))
    47  
    48  			Eventually(session).Should(Say("Org management:"))
    49  			Eventually(session).Should(Say(`  orgs,o\s+set-org-role`))
    50  
    51  			Eventually(session).Should(Say("CLI plugin management:"))
    52  			Eventually(session).Should(Say("  install-plugin    list-plugin-repos"))
    53  			Eventually(session).Should(Say("Global options:"))
    54  			Eventually(session).Should(Say("  --help, -h                         Show help"))
    55  			Eventually(session).Should(Say("  -v                                 Print API request diagnostics to stdout"))
    56  
    57  			Eventually(session).Should(Say(`TIP: Use 'cf help -a' to see all commands\.`))
    58  			Eventually(session).Should(Exit(0))
    59  		},
    60  
    61  		Entry("when cf is run without providing a command or a flag", func() *exec.Cmd {
    62  			return exec.Command("cf")
    63  		}),
    64  
    65  		Entry("when cf help is run", func() *exec.Cmd {
    66  			return exec.Command("cf", "help")
    67  		}),
    68  
    69  		Entry("when cf is run with -h flag alone", func() *exec.Cmd {
    70  			return exec.Command("cf", "-h")
    71  		}),
    72  
    73  		Entry("when cf is run with --help flag alone", func() *exec.Cmd {
    74  			return exec.Command("cf", "--help")
    75  		}),
    76  	)
    77  
    78  	DescribeTable("displays help for all commands",
    79  		func(setup func() *exec.Cmd) {
    80  			cmd := setup()
    81  			session, err := Start(cmd, GinkgoWriter, GinkgoWriter)
    82  			Expect(err).NotTo(HaveOccurred())
    83  
    84  			Eventually(session).Should(Say("NAME:"))
    85  			Eventually(session).Should(Say("USAGE:"))
    86  			Eventually(session).Should(Say("VERSION:"))
    87  			Eventually(session).Should(Say("GETTING STARTED:"))
    88  			Eventually(session).Should(Say("ENVIRONMENT VARIABLES:"))
    89  			Eventually(session).Should(Say(`CF_DIAL_TIMEOUT=6\s+Max wait time to establish a connection, including name resolution, in seconds`))
    90  			Eventually(session).Should(Say("GLOBAL OPTIONS:"))
    91  			Eventually(session).Should(Exit(0))
    92  		},
    93  
    94  		Entry("when cf help is run", func() *exec.Cmd {
    95  			return exec.Command("cf", "help", "-a")
    96  		}),
    97  
    98  		Entry("when cf is run with -h -a flag", func() *exec.Cmd {
    99  			return exec.Command("cf", "-h", "-a")
   100  		}),
   101  
   102  		Entry("when cf is run with --help -a flag", func() *exec.Cmd {
   103  			return exec.Command("cf", "--help", "-a")
   104  		}),
   105  	)
   106  
   107  	Describe("commands that appear in cf help -a", func() {
   108  		It("includes run-task", func() {
   109  			session := helpers.CF("help", "-a")
   110  			Eventually(session).Should(Say(`run-task\s+Run a one-off task on an app`))
   111  			Eventually(session).Should(Exit(0))
   112  		})
   113  
   114  		It("includes list-task", func() {
   115  			session := helpers.CF("help", "-a")
   116  			Eventually(session).Should(Say(`tasks\s+List tasks of an app`))
   117  			Eventually(session).Should(Exit(0))
   118  		})
   119  
   120  		It("includes terminate-task", func() {
   121  			session := helpers.CF("help", "-a")
   122  			Eventually(session).Should(Say(`terminate-task\s+Terminate a running task of an app`))
   123  			Eventually(session).Should(Exit(0))
   124  		})
   125  	})
   126  
   127  	Context("displays the help text for a given command", func() {
   128  		DescribeTable("displays the help",
   129  			func(setup func() (*exec.Cmd, int)) {
   130  				cmd, exitCode := setup()
   131  				session, err := Start(cmd, GinkgoWriter, GinkgoWriter)
   132  				Expect(err).NotTo(HaveOccurred())
   133  
   134  				Eventually(session).Should(Say("NAME:"))
   135  				Eventually(session).Should(Say("create-user-provided-service - Make a user-provided service instance available to CF apps"))
   136  				Eventually(session).Should(Say(`cf create-user-provided-service SERVICE_INSTANCE \[-p CREDENTIALS\] \[-l SYSLOG_DRAIN_URL\] \[-r ROUTE_SERVICE_URL\]`))
   137  				Eventually(session).Should(Say(`-l\s+URL to which logs for bound applications will be streamed`))
   138  				Eventually(session).Should(Exit(exitCode))
   139  			},
   140  
   141  			Entry("when a command is called with the --help flag", func() (*exec.Cmd, int) {
   142  				return exec.Command("cf", "create-user-provided-service", "--help"), 0
   143  			}),
   144  
   145  			Entry("when a command is called with the --help flag and command arguments", func() (*exec.Cmd, int) {
   146  				return exec.Command("cf", "create-user-provided-service", "-l", "http://example.com", "--help"), 0
   147  			}),
   148  
   149  			Entry("when a command is called with the --help flag and command arguments prior to the command", func() (*exec.Cmd, int) {
   150  				return exec.Command("cf", "-l", "create-user-provided-service", "--help"), 1
   151  			}),
   152  
   153  			Entry("when the help command is passed a command name", func() (*exec.Cmd, int) {
   154  				return exec.Command("cf", "help", "create-user-provided-service"), 0
   155  			}),
   156  
   157  			Entry("when the --help flag is passed with a command name", func() (*exec.Cmd, int) {
   158  				return exec.Command("cf", "--help", "create-user-provided-service"), 0
   159  			}),
   160  
   161  			Entry("when the -h flag is passed with a command name", func() (*exec.Cmd, int) {
   162  				return exec.Command("cf", "-h", "create-user-provided-service"), 0
   163  			}),
   164  
   165  			Entry("when the help command is passed a command alias", func() (*exec.Cmd, int) {
   166  				return exec.Command("cf", "help", "cups"), 0
   167  			}),
   168  
   169  			Entry("when the --help flag is passed with a command alias", func() (*exec.Cmd, int) {
   170  				return exec.Command("cf", "--help", "cups"), 0
   171  			}),
   172  
   173  			Entry("when the --help flag is passed after a command alias", func() (*exec.Cmd, int) {
   174  				return exec.Command("cf", "cups", "--help"), 0
   175  			}),
   176  
   177  			Entry("when an invalid flag is passed", func() (*exec.Cmd, int) {
   178  				return exec.Command("cf", "create-user-provided-service", "--invalid-flag"), 1
   179  			}),
   180  
   181  			Entry("when missing required arguments", func() (*exec.Cmd, int) {
   182  				return exec.Command("cf", "create-user-provided-service"), 1
   183  			}),
   184  
   185  			Entry("when missing arguments to flags", func() (*exec.Cmd, int) {
   186  				return exec.Command("cf", "create-user-provided-service", "foo", "-l"), 1
   187  			}),
   188  		)
   189  
   190  		When("the command uses timeout environment variables", func() {
   191  			DescribeTable("shows the CF_STAGING_TIMEOUT and CF_STARTUP_TIMEOUT environment variables",
   192  				func(setup func() (*exec.Cmd, int)) {
   193  					cmd, exitCode := setup()
   194  					session, err := Start(cmd, GinkgoWriter, GinkgoWriter)
   195  					Expect(err).NotTo(HaveOccurred())
   196  
   197  					Eventually(session).Should(Say("ENVIRONMENT:"))
   198  					Eventually(session).Should(Say("CF_STAGING_TIMEOUT=15\\s+Max wait time for staging, in minutes"))
   199  					Eventually(session).Should(Say("CF_STARTUP_TIMEOUT=5\\s+Max wait time for app instance startup, in minutes"))
   200  					Eventually(session).Should(Exit(exitCode))
   201  				},
   202  
   203  				Entry("cf push", func() (*exec.Cmd, int) {
   204  					return exec.Command("cf", "h", "push"), 0
   205  				}),
   206  
   207  				Entry("cf start", func() (*exec.Cmd, int) {
   208  					return exec.Command("cf", "h", "start"), 0
   209  				}),
   210  
   211  				Entry("cf restart", func() (*exec.Cmd, int) {
   212  					return exec.Command("cf", "h", "restart"), 0
   213  				}),
   214  
   215  				Entry("cf copy-source", func() (*exec.Cmd, int) {
   216  					return exec.Command("cf", "h", "copy-source"), 0
   217  				}),
   218  
   219  				Entry("cf restage", func() (*exec.Cmd, int) {
   220  					return exec.Command("cf", "h", "restage"), 0
   221  				}),
   222  			)
   223  		})
   224  	})
   225  
   226  	When("the command does not exist", func() {
   227  		DescribeTable("help displays an error message",
   228  			func(command func() *exec.Cmd) {
   229  				session, err := Start(command(), GinkgoWriter, GinkgoWriter)
   230  				Expect(err).NotTo(HaveOccurred())
   231  				Eventually(session.Err).Should(Say("'rock' is not a registered command. See 'cf help -a'"))
   232  				Eventually(session).Should(Exit(1))
   233  			},
   234  
   235  			Entry("passing --help into rock (cf rock --help)", func() *exec.Cmd {
   236  				return exec.Command("cf", "rock", "--help")
   237  			}),
   238  
   239  			Entry("passing the --help flag (cf --help rock)", func() *exec.Cmd {
   240  				return exec.Command("cf", "--help", "rock")
   241  			}),
   242  
   243  			Entry("calling the help command directly", func() *exec.Cmd {
   244  				return exec.Command("cf", "help", "rock")
   245  			}),
   246  		)
   247  
   248  	})
   249  
   250  	When("the option does not exist", func() {
   251  		DescribeTable("help display an error message as well as help for common commands",
   252  
   253  			func(command func() *exec.Cmd) {
   254  				session, err := Start(command(), GinkgoWriter, GinkgoWriter)
   255  				Expect(err).NotTo(HaveOccurred())
   256  
   257  				Eventually(session).Should(Exit(1))
   258  				Eventually(session).Should(Say("Before getting started:")) // common help
   259  				Expect(strings.Count(string(session.Err.Contents()), "unknown flag")).To(Equal(1))
   260  			},
   261  
   262  			Entry("passing invalid option", func() *exec.Cmd {
   263  				return exec.Command("cf", "-c")
   264  			}),
   265  
   266  			Entry("passing -a option", func() *exec.Cmd {
   267  				return exec.Command("cf", "-a")
   268  			}),
   269  		)
   270  	})
   271  })