github.com/sleungcy/cli@v7.1.0+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.FakeActor
    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.FakeActor)
    35  
    36  		cmd = v7.SetEnvCommand{
    37  			BaseCommand: v7.BaseCommand{
    38  				UI:          testUI,
    39  				Config:      fakeConfig,
    40  				SharedActor: fakeSharedActor,
    41  				Actor:       fakeActor,
    42  			},
    43  		}
    44  
    45  		binaryName = "faceman"
    46  		fakeConfig.BinaryNameReturns(binaryName)
    47  		appName = "some-app"
    48  
    49  		cmd.RequiredArgs.AppName = appName
    50  		cmd.RequiredArgs.EnvironmentVariableName = "some-key"
    51  		cmd.RequiredArgs.EnvironmentVariableValue = "some-value"
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(nil)
    56  	})
    57  
    58  	When("checking target fails", func() {
    59  		BeforeEach(func() {
    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  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
    76  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    77  		})
    78  
    79  		When("getting the current user returns an error", func() {
    80  			BeforeEach(func() {
    81  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error"))
    82  			})
    83  
    84  			It("returns the error", func() {
    85  				Expect(executeErr).To(MatchError("some-error"))
    86  			})
    87  		})
    88  
    89  		When("getting the current user succeeds", func() {
    90  			BeforeEach(func() {
    91  				fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    92  			})
    93  
    94  			When("setting the environment succeeds", func() {
    95  				BeforeEach(func() {
    96  					fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceReturns(v7action.Warnings{"set-warning-1", "set-warning-2"}, nil)
    97  				})
    98  
    99  				It("sets the environment variable and value pair", func() {
   100  					Expect(executeErr).ToNot(HaveOccurred())
   101  
   102  					Expect(testUI.Out).To(Say(`Setting env variable some-key for app some-app in org some-org / space some-space as banana\.\.\.`))
   103  
   104  					Expect(testUI.Err).To(Say("set-warning-1"))
   105  					Expect(testUI.Err).To(Say("set-warning-2"))
   106  					Expect(testUI.Out).To(Say("OK"))
   107  					Expect(testUI.Out).To(Say(`TIP: Use 'cf restage some-app' to ensure your env variable changes take effect\.`))
   108  
   109  					Expect(fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceCallCount()).To(Equal(1))
   110  					appName, spaceGUID, envVariablePair := fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceArgsForCall(0)
   111  					Expect(appName).To(Equal("some-app"))
   112  					Expect(spaceGUID).To(Equal("some-space-guid"))
   113  					Expect(envVariablePair.Key).To(Equal("some-key"))
   114  					Expect(envVariablePair.Value).To(Equal("some-value"))
   115  				})
   116  			})
   117  
   118  			When("the set environment variable returns an unknown error", func() {
   119  				var expectedErr error
   120  				BeforeEach(func() {
   121  					expectedErr = errors.New("some-error")
   122  					fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceReturns(v7action.Warnings{"get-warning-1", "get-warning-2"}, expectedErr)
   123  				})
   124  
   125  				It("returns the error", func() {
   126  					Expect(executeErr).To(Equal(expectedErr))
   127  					Expect(testUI.Out).To(Say(`Setting env variable some-key for app some-app in org some-org / space some-space as banana\.\.\.`))
   128  
   129  					Expect(testUI.Err).To(Say("get-warning-1"))
   130  					Expect(testUI.Err).To(Say("get-warning-2"))
   131  					Expect(testUI.Out).ToNot(Say("OK"))
   132  				})
   133  			})
   134  		})
   135  	})
   136  })