github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/help_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"strings"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/commands"
     8  	"code.cloudfoundry.org/cli/cf/commandsloader"
     9  	"code.cloudfoundry.org/cli/cf/configuration/pluginconfig"
    10  	"code.cloudfoundry.org/cli/cf/configuration/pluginconfig/pluginconfigfakes"
    11  	"code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/plugin"
    13  
    14  	"code.cloudfoundry.org/cli/cf/flags"
    15  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	"github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("Help", func() {
    22  
    23  	commandsloader.Load()
    24  
    25  	var (
    26  		fakeUI     *terminalfakes.FakeUI
    27  		fakeConfig *pluginconfigfakes.FakePluginConfiguration
    28  		deps       commandregistry.Dependency
    29  
    30  		cmd         *commands.Help
    31  		flagContext flags.FlagContext
    32  		buffer      *gbytes.Buffer
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		buffer = gbytes.NewBuffer()
    37  		fakeUI = new(terminalfakes.FakeUI)
    38  		fakeUI.WriterReturns(buffer)
    39  
    40  		fakeConfig = new(pluginconfigfakes.FakePluginConfiguration)
    41  
    42  		deps = commandregistry.Dependency{
    43  			UI:           fakeUI,
    44  			PluginConfig: fakeConfig,
    45  		}
    46  
    47  		cmd = &commands.Help{}
    48  		cmd.SetDependency(deps, false)
    49  
    50  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    51  	})
    52  
    53  	AfterEach(func() {
    54  		buffer.Close()
    55  	})
    56  
    57  	Context("when no argument is provided", func() {
    58  		It("prints the main help menu of the 'cf' app", func() {
    59  			flagContext.Parse()
    60  			err := cmd.Execute(flagContext)
    61  			Expect(err).NotTo(HaveOccurred())
    62  
    63  			Eventually(buffer.Contents).Should(ContainSubstring("A command line tool to interact with Cloud Foundry"))
    64  			Eventually(buffer).Should(gbytes.Say("CF_TRACE=true"))
    65  		})
    66  	})
    67  
    68  	Context("when a command name is provided as an argument", func() {
    69  		Context("When the command exists", func() {
    70  			It("prints the usage help for the command", func() {
    71  				flagContext.Parse("target")
    72  				err := cmd.Execute(flagContext)
    73  				Expect(err).NotTo(HaveOccurred())
    74  
    75  				Expect(fakeUI.SayCallCount()).To(Equal(1))
    76  				output, _ := fakeUI.SayArgsForCall(0)
    77  				Expect(output).To(ContainSubstring("target - Set or view the targeted org or space"))
    78  			})
    79  
    80  			Context("i18n translations", func() {
    81  				var originalT func(string, ...interface{}) string
    82  
    83  				BeforeEach(func() {
    84  					originalT = i18n.T
    85  				})
    86  
    87  				AfterEach(func() {
    88  					i18n.T = originalT
    89  				})
    90  
    91  				It("includes ':' in caption translation strings for language like French to be translated correctly", func() {
    92  					nameCaption := "NAME:"
    93  					aliasCaption := "ALIAS:"
    94  					usageCaption := "USAGE:"
    95  					optionsCaption := "OPTIONS:"
    96  					captionCheckCount := 0
    97  
    98  					i18n.T = func(translationID string, args ...interface{}) string {
    99  						if strings.HasPrefix(translationID, "NAME") {
   100  							Expect(translationID).To(Equal(nameCaption))
   101  							captionCheckCount += 1
   102  						} else if strings.HasPrefix(translationID, "ALIAS") {
   103  							Expect(translationID).To(Equal(aliasCaption))
   104  							captionCheckCount += 1
   105  						} else if strings.HasPrefix(translationID, "USAGE") {
   106  							Expect(translationID).To(Equal(usageCaption))
   107  							captionCheckCount += 1
   108  						} else if strings.HasPrefix(translationID, "OPTIONS") {
   109  							Expect(translationID).To(Equal(optionsCaption))
   110  							captionCheckCount += 1
   111  						}
   112  
   113  						return translationID
   114  					}
   115  
   116  					flagContext.Parse("target")
   117  					err := cmd.Execute(flagContext)
   118  					Expect(err).NotTo(HaveOccurred())
   119  
   120  					Expect(captionCheckCount).To(Equal(4))
   121  				})
   122  			})
   123  		})
   124  
   125  		Context("When the command does not exists", func() {
   126  			It("prints the usage help for the command", func() {
   127  				flagContext.Parse("bad-command")
   128  				err := cmd.Execute(flagContext)
   129  				Expect(err.Error()).To(Equal("'bad-command' is not a registered command. See 'cf help -a'"))
   130  			})
   131  		})
   132  	})
   133  
   134  	Context("when a command provided is a plugin command", func() {
   135  		BeforeEach(func() {
   136  			m := make(map[string]pluginconfig.PluginMetadata)
   137  			m["fakePlugin"] = pluginconfig.PluginMetadata{
   138  				Commands: []plugin.Command{
   139  					{
   140  						Name:     "fakePluginCmd1",
   141  						Alias:    "fpc1",
   142  						HelpText: "help text here",
   143  						UsageDetails: plugin.Usage{
   144  							Usage: "Usage for fpc1",
   145  							Options: map[string]string{
   146  								"f": "test flag",
   147  							},
   148  						},
   149  					},
   150  				},
   151  			}
   152  
   153  			fakeConfig.PluginsReturns(m)
   154  		})
   155  
   156  		Context("command is a plugin command name", func() {
   157  			It("prints the usage help for the command", func() {
   158  				flagContext.Parse("fakePluginCmd1")
   159  				err := cmd.Execute(flagContext)
   160  				Expect(err).NotTo(HaveOccurred())
   161  
   162  				Expect(fakeUI.SayCallCount()).To(Equal(1))
   163  				output, _ := fakeUI.SayArgsForCall(0)
   164  				Expect(output).To(ContainSubstring("fakePluginCmd1"))
   165  				Expect(output).To(ContainSubstring("help text here"))
   166  				Expect(output).To(ContainSubstring("ALIAS"))
   167  				Expect(output).To(ContainSubstring("fpc1"))
   168  				Expect(output).To(ContainSubstring("USAGE"))
   169  				Expect(output).To(ContainSubstring("Usage for fpc1"))
   170  				Expect(output).To(ContainSubstring("OPTIONS"))
   171  				Expect(output).To(ContainSubstring("-f"))
   172  				Expect(output).To(ContainSubstring("test flag"))
   173  			})
   174  		})
   175  
   176  		Context("command is a plugin command alias", func() {
   177  			It("prints the usage help for the command alias", func() {
   178  				flagContext.Parse("fpc1")
   179  				err := cmd.Execute(flagContext)
   180  				Expect(err).NotTo(HaveOccurred())
   181  
   182  				Expect(fakeUI.SayCallCount()).To(Equal(1))
   183  				output, _ := fakeUI.SayArgsForCall(0)
   184  				Expect(output).To(ContainSubstring("fakePluginCmd1"))
   185  				Expect(output).To(ContainSubstring("help text here"))
   186  				Expect(output).To(ContainSubstring("ALIAS"))
   187  				Expect(output).To(ContainSubstring("fpc1"))
   188  				Expect(output).To(ContainSubstring("USAGE"))
   189  				Expect(output).To(ContainSubstring("Usage for fpc1"))
   190  				Expect(output).To(ContainSubstring("OPTIONS"))
   191  				Expect(output).To(ContainSubstring("-f"))
   192  				Expect(output).To(ContainSubstring("test flag"))
   193  			})
   194  		})
   195  
   196  	})
   197  })