github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/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  func (c usageMethodCommand) Resources() string {
    53  	return `
    54  Resource 1
    55  Resource 2
    56  `
    57  }
    58  
    59  var _ = Describe("Help Actions", func() {
    60  	var actor *Actor
    61  
    62  	BeforeEach(func() {
    63  		actor = NewActor(&sharedactionfakes.FakeConfig{})
    64  	})
    65  
    66  	Describe("CommandInfoByName", func() {
    67  		When("the command exists", func() {
    68  			When("passed the command name", func() {
    69  				It("returns command info", func() {
    70  					commandInfo, err := actor.CommandInfoByName(commandList{}, "app")
    71  					Expect(err).NotTo(HaveOccurred())
    72  
    73  					Expect(commandInfo.Name).To(Equal("app"))
    74  					Expect(commandInfo.Description).To(Equal("Display health and status for an app"))
    75  					Expect(commandInfo.Alias).To(BeEmpty())
    76  					Expect(commandInfo.Usage).To(Equal("CF_NAME app APP_NAME"))
    77  					Expect(commandInfo.Flags).To(HaveLen(1))
    78  					Expect(commandInfo.Flags).To(ContainElement(CommandFlag{
    79  						Short:       "",
    80  						Long:        "guid",
    81  						Description: "Retrieve and display the given app's guid.  All other health and status output for the app is suppressed.",
    82  						Default:     "some-default",
    83  					}))
    84  					Expect(commandInfo.RelatedCommands).To(Equal([]string{
    85  						"apps", "events", "logs", "map-route", "push", "unmap-route",
    86  					}))
    87  				})
    88  
    89  				When("the command uses timeout environment variables", func() {
    90  					It("has timeout environment variables", func() {
    91  						commandInfo, err := actor.CommandInfoByName(commandList{}, "restage")
    92  						Expect(err).NotTo(HaveOccurred())
    93  
    94  						Expect(commandInfo.Environment).To(ConsistOf(
    95  							EnvironmentVariable{
    96  								Name:         "CF_STAGING_TIMEOUT",
    97  								Description:  "Max wait time for buildpack staging, in minutes",
    98  								DefaultValue: "15",
    99  							},
   100  							EnvironmentVariable{
   101  								Name:         "CF_STARTUP_TIMEOUT",
   102  								Description:  "Max wait time for app instance startup, in minutes",
   103  								DefaultValue: "5",
   104  							}))
   105  					})
   106  				})
   107  
   108  				When("the command does not use environment variables", func() {
   109  					It("does not have environment variables", func() {
   110  						commandInfo, err := actor.CommandInfoByName(commandList{}, "app")
   111  						Expect(err).NotTo(HaveOccurred())
   112  
   113  						Expect(commandInfo.Environment).To(BeEmpty())
   114  					})
   115  				})
   116  
   117  				When("the command has a Usage() method", func() {
   118  					It("retrieves the usage text from the method", func() {
   119  						commandInfo, err := actor.CommandInfoByName(commandList{}, "fancy")
   120  						Expect(err).NotTo(HaveOccurred())
   121  
   122  						Expect(commandInfo.Usage).To(Equal("Usage line 1\n   Usage line 2"))
   123  					})
   124  				})
   125  
   126  				When("the command has a Examples() method", func() {
   127  					It("retrieves the examples text from the method", func() {
   128  						commandInfo, err := actor.CommandInfoByName(commandList{}, "fancy")
   129  						Expect(err).NotTo(HaveOccurred())
   130  
   131  						Expect(commandInfo.Examples).To(Equal("Examples line 1\n   Examples line 2"))
   132  					})
   133  				})
   134  
   135  				When("the command has a Resources() method", func() {
   136  					It("retrieves the resources text from the method", func() {
   137  						commandInfo, err := actor.CommandInfoByName(commandList{}, "fancy")
   138  						Expect(err).NotTo(HaveOccurred())
   139  
   140  						Expect(commandInfo.Resources).To(Equal("Resource 1\n   Resource 2"))
   141  					})
   142  				})
   143  			})
   144  
   145  			When("passed the command alias", func() {
   146  				It("returns command info", func() {
   147  					commandInfo, err := actor.CommandInfoByName(commandList{}, "h")
   148  					Expect(err).NotTo(HaveOccurred())
   149  
   150  					Expect(commandInfo.Name).To(Equal("help"))
   151  					Expect(commandInfo.Description).To(Equal("Show help"))
   152  					Expect(commandInfo.Alias).To(Equal("h"))
   153  					Expect(commandInfo.Usage).To(Equal("CF_NAME help [COMMAND]"))
   154  					Expect(commandInfo.Flags).To(ConsistOf(
   155  						CommandFlag{
   156  							Short:       "a",
   157  							Long:        "",
   158  							Description: "All available CLI commands",
   159  						},
   160  					))
   161  				})
   162  			})
   163  		})
   164  
   165  		When("the command does not exist", func() {
   166  			It("returns err", func() {
   167  				_, err := actor.CommandInfoByName(commandList{}, "does-not-exist")
   168  
   169  				Expect(err).To(HaveOccurred())
   170  				Expect(err).To(MatchError(actionerror.InvalidCommandError{CommandName: "does-not-exist"}))
   171  			})
   172  		})
   173  	})
   174  
   175  	Describe("CommandInfos", func() {
   176  		It("returns back all the command's names and descriptions", func() {
   177  			commands := actor.CommandInfos(commandList{})
   178  
   179  			Expect(commands["app"]).To(Equal(CommandInfo{
   180  				Name:        "app",
   181  				Description: "Display health and status for an app",
   182  			}))
   183  			Expect(commands["help"]).To(Equal(CommandInfo{
   184  				Name:        "help",
   185  				Description: "Show help",
   186  				Alias:       "h",
   187  			}))
   188  			Expect(commands["restage"]).To(Equal(CommandInfo{
   189  				Name:        "restage",
   190  				Description: "Restage an app",
   191  				Alias:       "rg",
   192  			}))
   193  		})
   194  	})
   195  })