github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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/command/commandfakes"
     9  	v7 "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  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("set-env Command", func() {
    19  	var (
    20  		cmd             v7.SetEnvCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v7fakes.FakeSetEnvActor
    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(v7fakes.FakeSetEnvActor)
    35  
    36  		cmd = v7.SetEnvCommand{
    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  	When("checking target fails", func() {
    57  		BeforeEach(func() {
    58  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    59  		})
    60  
    61  		It("returns an error", func() {
    62  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    63  
    64  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    65  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    66  			Expect(checkTargetedOrg).To(BeTrue())
    67  			Expect(checkTargetedSpace).To(BeTrue())
    68  		})
    69  	})
    70  
    71  	When("the user is logged in, an org is targeted and a space is targeted", func() {
    72  		BeforeEach(func() {
    73  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
    74  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    75  		})
    76  
    77  		When("getting the current user returns an error", func() {
    78  			BeforeEach(func() {
    79  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error"))
    80  			})
    81  
    82  			It("returns the error", func() {
    83  				Expect(executeErr).To(MatchError("some-error"))
    84  			})
    85  		})
    86  
    87  		When("getting the current user succeeds", func() {
    88  			BeforeEach(func() {
    89  				fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    90  			})
    91  
    92  			When("setting the environment succeeds", func() {
    93  				BeforeEach(func() {
    94  					fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceReturns(v7action.Warnings{"set-warning-1", "set-warning-2"}, nil)
    95  				})
    96  
    97  				It("sets the environment variable and value pair", func() {
    98  					Expect(executeErr).ToNot(HaveOccurred())
    99  
   100  					Expect(testUI.Out).To(Say(`Setting env variable some-key for app some-app in org some-org / space some-space as banana\.\.\.`))
   101  
   102  					Expect(testUI.Err).To(Say("set-warning-1"))
   103  					Expect(testUI.Err).To(Say("set-warning-2"))
   104  					Expect(testUI.Out).To(Say("OK"))
   105  					Expect(testUI.Out).To(Say(`TIP: Use 'cf stage some-app' to ensure your env variable changes take effect\.`))
   106  
   107  					Expect(fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceCallCount()).To(Equal(1))
   108  					appName, spaceGUID, envVariablePair := fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceArgsForCall(0)
   109  					Expect(appName).To(Equal("some-app"))
   110  					Expect(spaceGUID).To(Equal("some-space-guid"))
   111  					Expect(envVariablePair.Key).To(Equal("some-key"))
   112  					Expect(envVariablePair.Value).To(Equal("some-value"))
   113  				})
   114  			})
   115  
   116  			When("the set environment variable returns an unknown error", func() {
   117  				var expectedErr error
   118  				BeforeEach(func() {
   119  					expectedErr = errors.New("some-error")
   120  					fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceReturns(v7action.Warnings{"get-warning-1", "get-warning-2"}, expectedErr)
   121  				})
   122  
   123  				It("returns the error", func() {
   124  					Expect(executeErr).To(Equal(expectedErr))
   125  					Expect(testUI.Out).To(Say(`Setting env variable some-key for app some-app in org some-org / space some-space as banana\.\.\.`))
   126  
   127  					Expect(testUI.Err).To(Say("get-warning-1"))
   128  					Expect(testUI.Err).To(Say("get-warning-2"))
   129  					Expect(testUI.Out).ToNot(Say("OK"))
   130  				})
   131  			})
   132  		})
   133  	})
   134  })