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