github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/v3_set_env_command_test.go (about) 1 package v6_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v3action" 8 "code.cloudfoundry.org/cli/command/commandfakes" 9 . "code.cloudfoundry.org/cli/command/v6" 10 "code.cloudfoundry.org/cli/command/v6/v6fakes" 11 "code.cloudfoundry.org/cli/util/configv3" 12 "code.cloudfoundry.org/cli/util/ui" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("v3-set-env Command", func() { 19 var ( 20 cmd V3SetEnvCommand 21 testUI *ui.UI 22 fakeConfig *commandfakes.FakeConfig 23 fakeSharedActor *commandfakes.FakeSharedActor 24 fakeActor *v6fakes.FakeV3SetEnvActor 25 binaryName string 26 executeErr error 27 appName string 28 ) 29 30 BeforeEach(func() { 31 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 32 fakeConfig = new(commandfakes.FakeConfig) 33 fakeSharedActor = new(commandfakes.FakeSharedActor) 34 fakeActor = new(v6fakes.FakeV3SetEnvActor) 35 36 cmd = V3SetEnvCommand{ 37 UI: testUI, 38 Config: fakeConfig, 39 SharedActor: fakeSharedActor, 40 Actor: fakeActor, 41 } 42 43 binaryName = "faceman" 44 fakeConfig.BinaryNameReturns(binaryName) 45 appName = "some-app" 46 47 cmd.RequiredArgs.AppName = appName 48 cmd.RequiredArgs.EnvironmentVariableName = "some-key" 49 cmd.RequiredArgs.EnvironmentVariableValue = "some-value" 50 }) 51 52 JustBeforeEach(func() { 53 executeErr = cmd.Execute(nil) 54 }) 55 56 It("displays the experimental warning", func() { 57 Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice")) 58 }) 59 60 When("checking target fails", func() { 61 BeforeEach(func() { 62 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 63 }) 64 65 It("returns an error", func() { 66 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 67 68 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 69 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 70 Expect(checkTargetedOrg).To(BeTrue()) 71 Expect(checkTargetedSpace).To(BeTrue()) 72 }) 73 }) 74 75 When("the user is logged in, an org is targeted and a space is targeted", func() { 76 BeforeEach(func() { 77 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 78 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 79 }) 80 81 When("getting the current user returns an error", func() { 82 BeforeEach(func() { 83 fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error")) 84 }) 85 86 It("returns the error", func() { 87 Expect(executeErr).To(MatchError("some-error")) 88 }) 89 }) 90 91 When("getting the current user succeeds", func() { 92 BeforeEach(func() { 93 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 94 }) 95 96 When("setting the environment succeeds", func() { 97 BeforeEach(func() { 98 fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceReturns(v3action.Warnings{"set-warning-1", "set-warning-2"}, nil) 99 }) 100 101 It("sets the environment variable and value pair", func() { 102 Expect(executeErr).ToNot(HaveOccurred()) 103 104 Expect(testUI.Out).To(Say(`Setting env variable some-key for app some-app in org some-org / space some-space as banana\.\.\.`)) 105 106 Expect(testUI.Err).To(Say("set-warning-1")) 107 Expect(testUI.Err).To(Say("set-warning-2")) 108 Expect(testUI.Out).To(Say("OK")) 109 Expect(testUI.Out).To(Say(`TIP: Use 'cf v3-stage some-app' to ensure your env variable changes take effect\.`)) 110 111 Expect(fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceCallCount()).To(Equal(1)) 112 appName, spaceGUID, envVariablePair := fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceArgsForCall(0) 113 Expect(appName).To(Equal("some-app")) 114 Expect(spaceGUID).To(Equal("some-space-guid")) 115 Expect(envVariablePair.Key).To(Equal("some-key")) 116 Expect(envVariablePair.Value).To(Equal("some-value")) 117 }) 118 }) 119 120 When("the set environment variable returns an unknown error", func() { 121 var expectedErr error 122 BeforeEach(func() { 123 expectedErr = errors.New("some-error") 124 fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceReturns(v3action.Warnings{"get-warning-1", "get-warning-2"}, expectedErr) 125 }) 126 127 It("returns the error", func() { 128 Expect(executeErr).To(Equal(expectedErr)) 129 Expect(testUI.Out).To(Say(`Setting env variable some-key for app some-app in org some-org / space some-space as banana\.\.\.`)) 130 131 Expect(testUI.Err).To(Say("get-warning-1")) 132 Expect(testUI.Err).To(Say("get-warning-2")) 133 Expect(testUI.Out).ToNot(Say("OK")) 134 }) 135 }) 136 }) 137 }) 138 })