github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/stacks_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror" 7 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action" 8 "github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes" 9 . "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7" 10 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes" 11 "github.com/LukasHeimann/cloudfoundrycli/v8/resources" 12 "github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3" 13 "github.com/LukasHeimann/cloudfoundrycli/v8/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.FakeActor 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.FakeActor) 43 args = nil 44 45 cmd = StacksCommand{ 46 BaseCommand: BaseCommand{ 47 UI: testUI, 48 Config: fakeConfig, 49 SharedActor: fakeSharedActor, 50 Actor: fakeActor, 51 }, 52 } 53 54 binaryName = "faceman" 55 fakeConfig.BinaryNameReturns(binaryName) 56 }) 57 58 Context("When the environment is not setup correctly", func() { 59 When("checking target fails", func() { 60 BeforeEach(func() { 61 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 62 }) 63 64 It("returns an error", func() { 65 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 66 67 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 68 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 69 Expect(checkTargetedOrg).To(BeFalse()) 70 Expect(checkTargetedSpace).To(BeFalse()) 71 }) 72 }) 73 }) 74 75 Context("When the environment is setup correctly", func() { 76 BeforeEach(func() { 77 fakeActor.GetCurrentUserReturns(configv3.User{Name: "banana"}, nil) 78 }) 79 80 When("StacksActor returns an error", func() { 81 var expectedErr error 82 83 BeforeEach(func() { 84 warnings := v7action.Warnings{"warning-1", "warning-2"} 85 expectedErr = errors.New("some-error") 86 fakeActor.GetStacksReturns(nil, warnings, expectedErr) 87 }) 88 89 It("prints that error with warnings", func() { 90 Expect(executeErr).To(Equal(expectedErr)) 91 92 Expect(testUI.Err).To(Say("warning-1")) 93 Expect(testUI.Err).To(Say("warning-2")) 94 Expect(testUI.Out).ToNot(Say(tableHeaders)) 95 }) 96 }) 97 98 When("StacksActor does not return an error", func() { 99 BeforeEach(func() { 100 stacks := []resources.Stack{ 101 {Name: "Stack2", Description: "desc2"}, 102 {Name: "stack1", Description: "desc1"}, 103 } 104 fakeActor.GetStacksReturns(stacks, v7action.Warnings{"warning-1", "warning-2"}, nil) 105 }) 106 107 When("the --labels flag is given", func() { 108 labelsFlagValue := "some-label-selector" 109 BeforeEach(func() { 110 cmd.Labels = labelsFlagValue 111 }) 112 It("passes the label selector to GetStacks", func() { 113 labelSelector := fakeActor.GetStacksArgsForCall(0) 114 Expect(labelSelector).To(Equal(labelsFlagValue)) 115 }) 116 }) 117 118 It("prints warnings", func() { 119 Expect(testUI.Err).To(Say(`warning-1`)) 120 Expect(testUI.Err).To(Say(`warning-2`)) 121 }) 122 123 It("prints the list of stacks in alphabetical order", func() { 124 Expect(testUI.Out).To(Say(tableHeaders)) 125 Expect(testUI.Out).To(Say(`stack1\s+desc1`)) 126 Expect(testUI.Out).To(Say(`Stack2\s+desc2`)) 127 }) 128 129 It("prints the flavor text", func() { 130 Expect(testUI.Out).To(Say("Getting stacks as banana\\.\\.\\.")) 131 }) 132 }) 133 }) 134 })