github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/sharedaction/help_test.go (about)

     1  package sharedaction_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	. "code.cloudfoundry.org/cli/actor/sharedaction"
     6  	"code.cloudfoundry.org/cli/actor/sharedaction/sharedactionfakes"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  type commandList struct {
    13  	App     appCommand         `command:"app" description:"Display health and status for an app"`
    14  	Restage restageCommand     `command:"restage" alias:"rg" description:"Restage an app"`
    15  	Help    helpCommand        `command:"help" alias:"h" description:"Show help"`
    16  	Usage   usageMethodCommand `command:"fancy"`
    17  }
    18  
    19  type appCommand struct {
    20  	GUID            bool        `long:"guid" description:"Retrieve and display the given app's guid.  All other health and status output for the app is suppressed." default:"some-default"`
    21  	usage           interface{} `usage:"CF_NAME app APP_NAME"`
    22  	relatedCommands interface{} `related_commands:"apps, events, logs, map-route, unmap-route, push"`
    23  }
    24  
    25  type restageCommand struct {
    26  	envCFStagingTimeout interface{} `environmentName:"CF_STAGING_TIMEOUT" environmentDescription:"Max wait time for buildpack staging, in minutes" environmentDefault:"15"`
    27  	envCFStartupTimeout interface{} `environmentName:"CF_STARTUP_TIMEOUT" environmentDescription:"Max wait time for app instance startup, in minutes" environmentDefault:"5"`
    28  }
    29  
    30  type helpCommand struct {
    31  	AllCommands bool        `short:"a" description:"All available CLI commands"`
    32  	usage       interface{} `usage:"CF_NAME help [COMMAND]"`
    33  }
    34  
    35  type usageMethodCommand struct {
    36  }
    37  
    38  func (c usageMethodCommand) Usage() string {
    39  	return `
    40  Usage line 1
    41  Usage line 2
    42  `
    43  }
    44  
    45  func (c usageMethodCommand) Examples() string {
    46  	return `
    47  Examples line 1
    48  Examples line 2
    49  `
    50  }
    51  
    52  var _ = Describe("Help Actions", func() {
    53  	var actor *Actor
    54  
    55  	BeforeEach(func() {
    56  		actor = NewActor(&sharedactionfakes.FakeConfig{})
    57  	})
    58  
    59  	Describe("CommandInfoByName", func() {
    60  		When("the command exists", func() {
    61  			When("passed the command name", func() {
    62  				It("returns command info", func() {
    63  					commandInfo, err := actor.CommandInfoByName(commandList{}, "app")
    64  					Expect(err).NotTo(HaveOccurred())
    65  
    66  					Expect(commandInfo.Name).To(Equal("app"))
    67  					Expect(commandInfo.Description).To(Equal("Display health and status for an app"))
    68  					Expect(commandInfo.Alias).To(BeEmpty())
    69  					Expect(commandInfo.Usage).To(Equal("CF_NAME app APP_NAME"))
    70  					Expect(commandInfo.Flags).To(HaveLen(1))
    71  					Expect(commandInfo.Flags).To(ContainElement(CommandFlag{
    72  						Short:       "",
    73  						Long:        "guid",
    74  						Description: "Retrieve and display the given app's guid.  All other health and status output for the app is suppressed.",
    75  						Default:     "some-default",
    76  					}))
    77  					Expect(commandInfo.RelatedCommands).To(Equal([]string{
    78  						"apps", "events", "logs", "map-route", "push", "unmap-route",
    79  					}))
    80  				})
    81  
    82  				When("the command uses timeout environment variables", func() {
    83  					It("has timeout environment variables", func() {
    84  						commandInfo, err := actor.CommandInfoByName(commandList{}, "restage")
    85  						Expect(err).NotTo(HaveOccurred())
    86  
    87  						Expect(commandInfo.Environment).To(ConsistOf(
    88  							EnvironmentVariable{
    89  								Name:         "CF_STAGING_TIMEOUT",
    90  								Description:  "Max wait time for buildpack staging, in minutes",
    91  								DefaultValue: "15",
    92  							},
    93  							EnvironmentVariable{
    94  								Name:         "CF_STARTUP_TIMEOUT",
    95  								Description:  "Max wait time for app instance startup, in minutes",
    96  								DefaultValue: "5",
    97  							}))
    98  					})
    99  				})
   100  
   101  				When("the command does not use environment variables", func() {
   102  					It("does not have environment variables", func() {
   103  						commandInfo, err := actor.CommandInfoByName(commandList{}, "app")
   104  						Expect(err).NotTo(HaveOccurred())
   105  
   106  						Expect(commandInfo.Environment).To(BeEmpty())
   107  					})
   108  				})
   109  
   110  				When("the command has a Usage() method", func() {
   111  					It("retrieves the usage text from the method", func() {
   112  						commandInfo, err := actor.CommandInfoByName(commandList{}, "fancy")
   113  						Expect(err).NotTo(HaveOccurred())
   114  
   115  						Expect(commandInfo.Usage).To(Equal("Usage line 1\n   Usage line 2"))
   116  					})
   117  				})
   118  
   119  				When("the command has a Examples() method", func() {
   120  					It("retrieves the examples text from the method", func() {
   121  						commandInfo, err := actor.CommandInfoByName(commandList{}, "fancy")
   122  						Expect(err).NotTo(HaveOccurred())
   123  
   124  						Expect(commandInfo.Examples).To(Equal("Examples line 1\n   Examples line 2"))
   125  					})
   126  				})
   127  			})
   128  
   129  			When("passed the command alias", func() {
   130  				It("returns command info", func() {
   131  					commandInfo, err := actor.CommandInfoByName(commandList{}, "h")
   132  					Expect(err).NotTo(HaveOccurred())
   133  
   134  					Expect(commandInfo.Name).To(Equal("help"))
   135  					Expect(commandInfo.Description).To(Equal("Show help"))
   136  					Expect(commandInfo.Alias).To(Equal("h"))
   137  					Expect(commandInfo.Usage).To(Equal("CF_NAME help [COMMAND]"))
   138  					Expect(commandInfo.Flags).To(ConsistOf(
   139  						CommandFlag{
   140  							Short:       "a",
   141  							Long:        "",
   142  							Description: "All available CLI commands",
   143  						},
   144  					))
   145  				})
   146  			})
   147  		})
   148  
   149  		When("the command does not exist", func() {
   150  			It("returns err", func() {
   151  				_, err := actor.CommandInfoByName(commandList{}, "does-not-exist")
   152  
   153  				Expect(err).To(HaveOccurred())
   154  				Expect(err).To(MatchError(actionerror.InvalidCommandError{CommandName: "does-not-exist"}))
   155  			})
   156  		})
   157  	})
   158  
   159  	Describe("CommandInfos", func() {
   160  		It("returns back all the command's names and descriptions", func() {
   161  			commands := actor.CommandInfos(commandList{})
   162  
   163  			Expect(commands["app"]).To(Equal(CommandInfo{
   164  				Name:        "app",
   165  				Description: "Display health and status for an app",
   166  			}))
   167  			Expect(commands["help"]).To(Equal(CommandInfo{
   168  				Name:        "help",
   169  				Description: "Show help",
   170  				Alias:       "h",
   171  			}))
   172  			Expect(commands["restage"]).To(Equal(CommandInfo{
   173  				Name:        "restage",
   174  				Description: "Restage an app",
   175  				Alias:       "rg",
   176  			}))
   177  		})
   178  	})
   179  })