github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v7/set_env_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/api/cloudcontroller/ccversion" 9 "code.cloudfoundry.org/cli/command/commandfakes" 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 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gbytes" 17 ) 18 19 var _ = Describe("set-env Command", func() { 20 var ( 21 cmd v7.SetEnvCommand 22 testUI *ui.UI 23 fakeConfig *commandfakes.FakeConfig 24 fakeSharedActor *commandfakes.FakeSharedActor 25 fakeActor *v7fakes.FakeSetEnvActor 26 binaryName string 27 executeErr error 28 appName string 29 ) 30 31 BeforeEach(func() { 32 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 33 fakeConfig = new(commandfakes.FakeConfig) 34 fakeSharedActor = new(commandfakes.FakeSharedActor) 35 fakeActor = new(v7fakes.FakeSetEnvActor) 36 37 cmd = v7.SetEnvCommand{ 38 UI: testUI, 39 Config: fakeConfig, 40 SharedActor: fakeSharedActor, 41 Actor: fakeActor, 42 } 43 44 binaryName = "faceman" 45 fakeConfig.BinaryNameReturns(binaryName) 46 appName = "some-app" 47 48 cmd.RequiredArgs.AppName = appName 49 cmd.RequiredArgs.EnvironmentVariableName = "some-key" 50 cmd.RequiredArgs.EnvironmentVariableValue = "some-value" 51 }) 52 53 JustBeforeEach(func() { 54 executeErr = cmd.Execute(nil) 55 }) 56 57 When("checking target fails", func() { 58 BeforeEach(func() { 59 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionApplicationFlowV3) 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(BeTrue()) 69 Expect(checkTargetedSpace).To(BeTrue()) 70 }) 71 }) 72 73 When("the user is logged in, an org is targeted and a space is targeted", func() { 74 BeforeEach(func() { 75 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionApplicationFlowV3) 76 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 77 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 78 }) 79 80 When("getting the current user returns an error", func() { 81 BeforeEach(func() { 82 fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error")) 83 }) 84 85 It("returns the error", func() { 86 Expect(executeErr).To(MatchError("some-error")) 87 }) 88 }) 89 90 When("getting the current user succeeds", func() { 91 BeforeEach(func() { 92 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 93 }) 94 95 When("setting the environment succeeds", func() { 96 BeforeEach(func() { 97 fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceReturns(v7action.Warnings{"set-warning-1", "set-warning-2"}, nil) 98 }) 99 100 It("sets the environment variable and value pair", func() { 101 Expect(executeErr).ToNot(HaveOccurred()) 102 103 Expect(testUI.Out).To(Say(`Setting env variable some-key for app some-app in org some-org / space some-space as banana\.\.\.`)) 104 105 Expect(testUI.Err).To(Say("set-warning-1")) 106 Expect(testUI.Err).To(Say("set-warning-2")) 107 Expect(testUI.Out).To(Say("OK")) 108 Expect(testUI.Out).To(Say(`TIP: Use 'cf v3-stage some-app' to ensure your env variable changes take effect\.`)) 109 110 Expect(fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceCallCount()).To(Equal(1)) 111 appName, spaceGUID, envVariablePair := fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceArgsForCall(0) 112 Expect(appName).To(Equal("some-app")) 113 Expect(spaceGUID).To(Equal("some-space-guid")) 114 Expect(envVariablePair.Key).To(Equal("some-key")) 115 Expect(envVariablePair.Value).To(Equal("some-value")) 116 }) 117 }) 118 119 When("the set environment variable returns an unknown error", func() { 120 var expectedErr error 121 BeforeEach(func() { 122 expectedErr = errors.New("some-error") 123 fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceReturns(v7action.Warnings{"get-warning-1", "get-warning-2"}, expectedErr) 124 }) 125 126 It("returns the error", func() { 127 Expect(executeErr).To(Equal(expectedErr)) 128 Expect(testUI.Out).To(Say(`Setting env variable some-key for app some-app in org some-org / space some-space as banana\.\.\.`)) 129 130 Expect(testUI.Err).To(Say("get-warning-1")) 131 Expect(testUI.Err).To(Say("get-warning-2")) 132 Expect(testUI.Out).ToNot(Say("OK")) 133 }) 134 }) 135 }) 136 }) 137 })