github.com/thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/util/command_parser/command_parser_test.go (about)

     1  package command_parser_test
     2  
     3  import (
     4  	"io/ioutil"
     5  
     6  	"code.cloudfoundry.org/cli/command/commandfakes"
     7  	"code.cloudfoundry.org/cli/command/common"
     8  	"code.cloudfoundry.org/cli/util/command_parser"
     9  	"code.cloudfoundry.org/cli/util/configv3"
    10  	"code.cloudfoundry.org/cli/util/ui"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Command 'Parser'", func() {
    16  	var (
    17  		pluginUI *ui.UI
    18  	)
    19  	BeforeEach(func() {
    20  		var err error
    21  		fakeConfig := new(commandfakes.FakeConfig)
    22  		pluginUI, err = ui.NewPluginUI(fakeConfig, ioutil.Discard, ioutil.Discard)
    23  		Expect(err).ToNot(HaveOccurred())
    24  	})
    25  
    26  	Describe("unknown command handling", func() {
    27  		var (
    28  			exitCode int
    29  			err      error
    30  		)
    31  
    32  		BeforeEach(func() {
    33  			parser, newErr := command_parser.NewCommandParser()
    34  			Expect(newErr).ToNot(HaveOccurred())
    35  			exitCode, err = parser.ParseCommandFromArgs(pluginUI, []string{"howdy"})
    36  		})
    37  
    38  		It("returns an unknown command error with the command name", func() {
    39  			unknownCommandErr := err.(command_parser.UnknownCommandError)
    40  			Expect(unknownCommandErr.CommandName).To(Equal("howdy"))
    41  			Expect(exitCode).To(Equal(0))
    42  		})
    43  	})
    44  
    45  	Describe("the verbose flag", func() {
    46  		var parser command_parser.CommandParser
    47  
    48  		BeforeEach(func() {
    49  			// Needed because the command-table is a singleton
    50  			// and the absence of -v relies on the default value of
    51  			// common.Commands.VerboseOrVersion to be false
    52  			common.Commands.VerboseOrVersion = false
    53  			var err error
    54  
    55  			parser, err = command_parser.NewCommandParser()
    56  			Expect(err).ToNot(HaveOccurred())
    57  		})
    58  
    59  		It("sets the verbose/version flag", func() {
    60  			exitCode, err := parser.ParseCommandFromArgs(pluginUI, []string{"-v", "help"})
    61  			Expect(exitCode).To(Equal(0))
    62  			Expect(err).ToNot(HaveOccurred())
    63  			Expect(parser.Config.Flags).To(Equal(configv3.FlagOverride{Verbose: true}))
    64  		})
    65  
    66  		It("sets the verbose/version flag after the command-name", func() {
    67  			exitCode, err := parser.ParseCommandFromArgs(pluginUI, []string{"help", "-v"})
    68  			Expect(exitCode).To(Equal(0))
    69  			Expect(err).ToNot(HaveOccurred())
    70  			Expect(parser.Config.Flags).To(Equal(configv3.FlagOverride{Verbose: true}))
    71  		})
    72  
    73  		It("doesn't turn verbose on by default", func() {
    74  			exitCode, err := parser.ParseCommandFromArgs(pluginUI, []string{"help"})
    75  			Expect(exitCode).To(Equal(0))
    76  			Expect(err).ToNot(HaveOccurred())
    77  			Expect(parser.Config.Flags).To(Equal(configv3.FlagOverride{Verbose: false}))
    78  		})
    79  
    80  	})
    81  })