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