github.com/sleungcy-sap/cli@v7.1.0+incompatible/cf/commandregistry/registry_test.go (about)

     1  package commandregistry_test
     2  
     3  import (
     4  	"strings"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  
     8  	. "code.cloudfoundry.org/cli/cf/commandregistry/fakecommand"
     9  
    10  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    11  
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("CommandRegistry", func() {
    18  	BeforeEach(func() {
    19  		commandregistry.Commands = commandregistry.NewRegistry() // because other tests load all the commands into the registry
    20  	})
    21  
    22  	Context("i18n", func() {
    23  		It("initialize i18n T() func", func() {
    24  			Expect(T).ToNot(BeNil())
    25  		})
    26  	})
    27  
    28  	Describe("Register()", func() {
    29  		AfterEach(func() {
    30  			commandregistry.Commands.RemoveCommand("fake-command2")
    31  		})
    32  
    33  		It("registers a command and its alias into the Command Registry map", func() {
    34  			Expect(commandregistry.Commands.CommandExists("fake-command2")).To(BeFalse())
    35  			Expect(commandregistry.Commands.CommandExists("fc2")).To(BeFalse())
    36  
    37  			commandregistry.Register(FakeCommand2{})
    38  
    39  			Expect(commandregistry.Commands.CommandExists("fake-command2")).To(BeTrue())
    40  			Expect(commandregistry.Commands.CommandExists("fc2")).To(BeTrue())
    41  		})
    42  	})
    43  
    44  	Describe("CommandExists()", func() {
    45  		Context("when the command has been registered", func() {
    46  			BeforeEach(func() {
    47  				commandregistry.Register(FakeCommand1{})
    48  			})
    49  
    50  			AfterEach(func() {
    51  				commandregistry.Commands.RemoveCommand("fake-command")
    52  			})
    53  
    54  			It("returns true the command exists in the list", func() {
    55  				Expect(commandregistry.Commands.CommandExists("fake-command")).To(BeTrue())
    56  			})
    57  
    58  			It("returns true if the alias exists", func() {
    59  				Expect(commandregistry.Commands.CommandExists("fc1")).To(BeTrue())
    60  			})
    61  		})
    62  
    63  		It("returns false when the command has not been registered", func() {
    64  			Expect(commandregistry.Commands.CommandExists("non-exist-cmd")).To(BeFalse())
    65  		})
    66  
    67  		It("returns false if the command name is an empty string", func() {
    68  			Expect(commandregistry.Commands.CommandExists("")).To(BeFalse())
    69  		})
    70  	})
    71  
    72  	Describe("FindCommand()", func() {
    73  		Context("when the command has been registered", func() {
    74  			BeforeEach(func() {
    75  				commandregistry.Register(FakeCommand1{})
    76  			})
    77  
    78  			AfterEach(func() {
    79  				commandregistry.Commands.RemoveCommand("fake-command")
    80  			})
    81  
    82  			It("returns the command when the command's name is given", func() {
    83  				cmd := commandregistry.Commands.FindCommand("fake-command")
    84  				Expect(cmd.MetaData().Usage[0]).To(ContainSubstring("Usage of fake-command"))
    85  				Expect(cmd.MetaData().Description).To(Equal("Description for fake-command"))
    86  			})
    87  
    88  			It("returns the command when the command's alias is given", func() {
    89  				cmd := commandregistry.Commands.FindCommand("fc1")
    90  				Expect(cmd.MetaData().Usage[0]).To(ContainSubstring("Usage of fake-command"))
    91  				Expect(cmd.MetaData().Description).To(Equal("Description for fake-command"))
    92  			})
    93  		})
    94  
    95  		It("returns nil when the command has not been registered", func() {
    96  			cmd := commandregistry.Commands.FindCommand("fake-command")
    97  			Expect(cmd).To(BeNil())
    98  		})
    99  	})
   100  
   101  	Describe("ShowAllCommands()", func() {
   102  		BeforeEach(func() {
   103  			commandregistry.Register(FakeCommand1{})
   104  			commandregistry.Register(FakeCommand2{})
   105  			commandregistry.Register(FakeCommand3{})
   106  		})
   107  
   108  		AfterEach(func() {
   109  			commandregistry.Commands.RemoveCommand("fake-command")
   110  			commandregistry.Commands.RemoveCommand("fake-command2")
   111  			commandregistry.Commands.RemoveCommand("this-is-a-really-long-command-name-123123123123123123123") // fake-command3
   112  		})
   113  
   114  		It("show all the commands in registry", func() {
   115  			cmds := commandregistry.Commands.ListCommands()
   116  			Expect(cmds).To(ContainElement("fake-command2"))
   117  			Expect(cmds).To(ContainElement("this-is-a-really-long-command-name-123123123123123123123"))
   118  			Expect(cmds).To(ContainElement("fake-command"))
   119  		})
   120  	})
   121  
   122  	Describe("SetCommand()", func() {
   123  		It("replaces the command in registry with command provided", func() {
   124  			updatedCmd := FakeCommand1{Data: "This is new data"}
   125  			oldCmd := commandregistry.Commands.FindCommand("fake-command")
   126  			Expect(oldCmd).ToNot(Equal(updatedCmd))
   127  
   128  			commandregistry.Commands.SetCommand(updatedCmd)
   129  			oldCmd = commandregistry.Commands.FindCommand("fake-command")
   130  			Expect(oldCmd).To(Equal(updatedCmd))
   131  		})
   132  	})
   133  
   134  	Describe("TotalCommands()", func() {
   135  		Context("when there are commands registered", func() {
   136  			BeforeEach(func() {
   137  				commandregistry.Register(FakeCommand1{})
   138  				commandregistry.Register(FakeCommand2{})
   139  				commandregistry.Register(FakeCommand3{})
   140  			})
   141  
   142  			AfterEach(func() {
   143  				commandregistry.Commands.RemoveCommand("fake-command")
   144  				commandregistry.Commands.RemoveCommand("fake-command2")
   145  				commandregistry.Commands.RemoveCommand("this-is-a-really-long-command-name-123123123123123123123") // fake-command3
   146  			})
   147  
   148  			It("returns the total number of registered commands", func() {
   149  				Expect(commandregistry.Commands.TotalCommands()).To(Equal(3))
   150  			})
   151  		})
   152  
   153  		It("returns 0 when there are no commands registered", func() {
   154  			Expect(commandregistry.Commands.TotalCommands()).To(Equal(0))
   155  		})
   156  	})
   157  
   158  	Describe("Metadatas()", func() {
   159  		BeforeEach(func() {
   160  			commandregistry.Register(FakeCommand1{})
   161  			commandregistry.Register(FakeCommand2{})
   162  			commandregistry.Register(FakeCommand3{})
   163  		})
   164  
   165  		AfterEach(func() {
   166  			commandregistry.Commands.RemoveCommand("fake-command")
   167  			commandregistry.Commands.RemoveCommand("fake-command2")
   168  			commandregistry.Commands.RemoveCommand("this-is-a-really-long-command-name-123123123123123123123") // fake-command3
   169  		})
   170  
   171  		It("returns all the metadata for all registered commands", func() {
   172  			Expect(len(commandregistry.Commands.Metadatas())).To(Equal(3))
   173  		})
   174  	})
   175  
   176  	Describe("RemoveCommand()", func() {
   177  		BeforeEach(func() {
   178  			commandregistry.Register(FakeCommand1{})
   179  		})
   180  
   181  		It("removes the command in registry with command name provided", func() {
   182  			commandregistry.Commands.RemoveCommand("fake-command")
   183  			Expect(commandregistry.Commands.CommandExists("fake-command")).To(BeFalse())
   184  		})
   185  	})
   186  
   187  	Describe("MaxCommandNameLength()", func() {
   188  		BeforeEach(func() {
   189  			commandregistry.Register(FakeCommand1{})
   190  			commandregistry.Register(FakeCommand2{})
   191  			commandregistry.Register(FakeCommand3{})
   192  		})
   193  
   194  		AfterEach(func() {
   195  			commandregistry.Commands.RemoveCommand("fake-command")
   196  			commandregistry.Commands.RemoveCommand("fake-command2")
   197  			commandregistry.Commands.RemoveCommand("this-is-a-really-long-command-name-123123123123123123123") // fake-command3
   198  		})
   199  
   200  		It("returns the length of the longest command name", func() {
   201  			maxLen := commandregistry.Commands.MaxCommandNameLength()
   202  			Expect(maxLen).To(Equal(len("this-is-a-really-long-command-name-123123123123123123123")))
   203  		})
   204  	})
   205  
   206  	Describe("CommandUsage()", func() {
   207  		BeforeEach(func() {
   208  			commandregistry.Register(FakeCommand1{})
   209  		})
   210  
   211  		AfterEach(func() {
   212  			commandregistry.Commands.RemoveCommand("fake-command")
   213  		})
   214  
   215  		It("prints the name, description and usage of a command", func() {
   216  			o := commandregistry.Commands.CommandUsage("fake-command")
   217  			outputs := strings.Split(o, "\n")
   218  			Expect(outputs).To(BeInDisplayOrder(
   219  				[]string{"NAME:"},
   220  				[]string{"   fake-command", "Description"},
   221  				[]string{"USAGE:"},
   222  			))
   223  		})
   224  
   225  		Context("i18n translations", func() {
   226  			var originalT func(string, ...interface{}) string
   227  
   228  			BeforeEach(func() {
   229  				originalT = T
   230  			})
   231  
   232  			AfterEach(func() {
   233  				T = originalT
   234  			})
   235  
   236  			It("includes ':' in caption translation strings for language like French to be translated correctly", func() {
   237  				nameCaption := "NAME:"
   238  				aliasCaption := "ALIAS:"
   239  				usageCaption := "USAGE:"
   240  				optionsCaption := "OPTIONS:"
   241  				captionCheckCount := 0
   242  
   243  				T = func(translationID string, args ...interface{}) string {
   244  					if strings.HasPrefix(translationID, "NAME") {
   245  						Expect(translationID).To(Equal(nameCaption))
   246  						captionCheckCount += 1
   247  					} else if strings.HasPrefix(translationID, "ALIAS") {
   248  						Expect(translationID).To(Equal(aliasCaption))
   249  						captionCheckCount += 1
   250  					} else if strings.HasPrefix(translationID, "USAGE") {
   251  						Expect(translationID).To(Equal(usageCaption))
   252  						captionCheckCount += 1
   253  					} else if strings.HasPrefix(translationID, "OPTIONS") {
   254  						Expect(translationID).To(Equal(optionsCaption))
   255  						captionCheckCount += 1
   256  					}
   257  
   258  					return translationID
   259  				}
   260  
   261  				commandregistry.Commands.CommandUsage("fake-command")
   262  			})
   263  		})
   264  
   265  		It("prints the flag options", func() {
   266  			o := commandregistry.Commands.CommandUsage("fake-command")
   267  			outputs := strings.Split(o, "\n")
   268  			Expect(outputs).To(BeInDisplayOrder(
   269  				[]string{"NAME:"},
   270  				[]string{"USAGE:"},
   271  				[]string{"OPTIONS:"},
   272  				[]string{"intFlag", "Usage for"},
   273  			))
   274  		})
   275  
   276  		It("replaces 'CF_NAME' with executable name from os.Arg[0]", func() {
   277  			o := commandregistry.Commands.CommandUsage("fake-command")
   278  			outputs := strings.Split(o, "\n")
   279  			Expect(outputs).To(BeInDisplayOrder(
   280  				[]string{"USAGE:"},
   281  				[]string{"cf", "Usage of"},
   282  			))
   283  			Consistently(outputs).ShouldNot(ContainSubstrings([]string{"CF_NAME"}))
   284  		})
   285  	})
   286  })