github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/stacks_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("stacks Command", func() {
    20  	var (
    21  		cmd             StacksCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeActor
    26  		executeErr      error
    27  		args            []string
    28  		binaryName      string
    29  	)
    30  
    31  	const tableHeaders = `name\s+description`
    32  
    33  	JustBeforeEach(func() {
    34  		executeErr = cmd.Execute(args)
    35  	})
    36  
    37  	BeforeEach(func() {
    38  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    39  		fakeConfig = new(commandfakes.FakeConfig)
    40  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    41  		fakeActor = new(v7fakes.FakeActor)
    42  		args = nil
    43  
    44  		cmd = StacksCommand{
    45  			BaseCommand: BaseCommand{
    46  				UI:          testUI,
    47  				Config:      fakeConfig,
    48  				SharedActor: fakeSharedActor,
    49  				Actor:       fakeActor,
    50  			},
    51  		}
    52  
    53  		binaryName = "faceman"
    54  		fakeConfig.BinaryNameReturns(binaryName)
    55  	})
    56  
    57  	Context("When the environment is not setup correctly", func() {
    58  		When("checking target fails", func() {
    59  			BeforeEach(func() {
    60  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    61  			})
    62  
    63  			It("returns an error", func() {
    64  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    65  
    66  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    67  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    68  				Expect(checkTargetedOrg).To(BeFalse())
    69  				Expect(checkTargetedSpace).To(BeFalse())
    70  			})
    71  		})
    72  	})
    73  
    74  	Context("When the environment is setup correctly", func() {
    75  		BeforeEach(func() {
    76  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    77  		})
    78  
    79  		When("StacksActor returns an error", func() {
    80  			var expectedErr error
    81  
    82  			BeforeEach(func() {
    83  				warnings := v7action.Warnings{"warning-1", "warning-2"}
    84  				expectedErr = errors.New("some-error")
    85  				fakeActor.GetStacksReturns(nil, warnings, expectedErr)
    86  			})
    87  
    88  			It("prints that error with warnings", func() {
    89  				Expect(executeErr).To(Equal(expectedErr))
    90  
    91  				Expect(testUI.Err).To(Say("warning-1"))
    92  				Expect(testUI.Err).To(Say("warning-2"))
    93  				Expect(testUI.Out).ToNot(Say(tableHeaders))
    94  			})
    95  		})
    96  
    97  		When("StacksActor does not return an error", func() {
    98  			BeforeEach(func() {
    99  				stacks := []v7action.Stack{
   100  					{Name: "Stack2", Description: "desc2"},
   101  					{Name: "stack1", Description: "desc1"},
   102  				}
   103  				fakeActor.GetStacksReturns(stacks, v7action.Warnings{"warning-1", "warning-2"}, nil)
   104  			})
   105  
   106  			When("the --labels flag is given", func() {
   107  				labelsFlagValue := "some-label-selector"
   108  				BeforeEach(func() {
   109  					cmd.Labels = labelsFlagValue
   110  				})
   111  				It("passes the label selector to GetStacks", func() {
   112  					labelSelector := fakeActor.GetStacksArgsForCall(0)
   113  					Expect(labelSelector).To(Equal(labelsFlagValue))
   114  				})
   115  			})
   116  
   117  			It("prints warnings", func() {
   118  				Expect(testUI.Err).To(Say(`warning-1`))
   119  				Expect(testUI.Err).To(Say(`warning-2`))
   120  			})
   121  
   122  			It("prints the list of stacks in alphabetical order", func() {
   123  				Expect(testUI.Out).To(Say(tableHeaders))
   124  				Expect(testUI.Out).To(Say(`stack1\s+desc1`))
   125  				Expect(testUI.Out).To(Say(`Stack2\s+desc2`))
   126  			})
   127  
   128  			It("prints the flavor text", func() {
   129  				Expect(testUI.Out).To(Say("Getting stacks as banana\\.\\.\\."))
   130  			})
   131  		})
   132  	})
   133  })