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