github.com/wanddynosios/cli@v7.1.0+incompatible/integration/v6/isolated/help_command_test.go (about)

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