github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v7/buildpacks_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/types"
     5  	"errors"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/actor/v7action"
     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  
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("buildpacks Command", func() {
    21  	var (
    22  		cmd             BuildpacksCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v7fakes.FakeBuildpacksActor
    27  		executeErr      error
    28  		args            []string
    29  		binaryName      string
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v7fakes.FakeBuildpacksActor)
    37  		args = nil
    38  
    39  		cmd = BuildpacksCommand{
    40  			UI:          testUI,
    41  			Config:      fakeConfig,
    42  			SharedActor: fakeSharedActor,
    43  			Actor:       fakeActor,
    44  		}
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  	})
    49  
    50  	JustBeforeEach(func() {
    51  		executeErr = cmd.Execute(args)
    52  	})
    53  
    54  	When("the environment is not set up correctly", func() {
    55  		BeforeEach(func() {
    56  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    57  		})
    58  
    59  		It("returns an error", func() {
    60  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    61  
    62  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    63  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    64  			Expect(checkTargetedOrg).To(BeFalse())
    65  			Expect(checkTargetedSpace).To(BeFalse())
    66  		})
    67  	})
    68  
    69  	When("the environment is setup correctly", func() {
    70  		BeforeEach(func() {
    71  			fakeConfig.CurrentUserReturns(configv3.User{Name: "apple"}, nil)
    72  		})
    73  
    74  		It("should print text indicating its runnning", func() {
    75  			Expect(executeErr).NotTo(HaveOccurred())
    76  			Expect(testUI.Out).To(Say(`Getting buildpacks as apple\.\.\.`))
    77  		})
    78  
    79  		When("getting buildpacks fails", func() {
    80  			BeforeEach(func() {
    81  				fakeActor.GetBuildpacksReturns(nil, v7action.Warnings{"some-warning-1", "some-warning-2"},
    82  					errors.New("some-error"))
    83  			})
    84  
    85  			It("prints warnings and returns error", func() {
    86  				Expect(executeErr).To(MatchError("some-error"))
    87  
    88  				Expect(testUI.Err).To(Say("some-warning-1"))
    89  				Expect(testUI.Err).To(Say("some-warning-2"))
    90  			})
    91  		})
    92  
    93  		When("getting buildpacks succeeds", func() {
    94  			When("buildpacks exist", func() {
    95  				BeforeEach(func() {
    96  					buildpacks := []v7action.Buildpack{
    97  						{
    98  							Name:     "buildpack-1",
    99  							Position: types.NullInt{Value: 1, IsSet: true},
   100  							Enabled:  types.NullBool{Value: true, IsSet: true},
   101  							Locked:   types.NullBool{Value: false, IsSet: true},
   102  							Filename: "buildpack-1.file",
   103  							Stack:    "buildpack-1-stack",
   104  						},
   105  
   106  						{
   107  							Name:     "buildpack-2",
   108  							Position: types.NullInt{Value: 2, IsSet: true},
   109  							Enabled:  types.NullBool{Value: false, IsSet: true},
   110  							Locked:   types.NullBool{Value: true, IsSet: true},
   111  							Filename: "buildpack-2.file",
   112  							Stack:    "",
   113  						},
   114  					}
   115  					fakeActor.GetBuildpacksReturns(buildpacks, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
   116  				})
   117  				It("prints a table of buildpacks", func() {
   118  					Expect(executeErr).NotTo(HaveOccurred())
   119  					Expect(testUI.Err).To(Say("some-warning-1"))
   120  					Expect(testUI.Err).To(Say("some-warning-2"))
   121  					Expect(testUI.Out).To(Say(`position\s+name\s+stack\s+enabled\s+locked\s+filename`))
   122  					Expect(testUI.Out).To(Say(`1\s+buildpack-1\s+buildpack-1-stack\s+true\s+false\s+buildpack-1.file`))
   123  					Expect(testUI.Out).To(Say(`2\s+buildpack-2\s+false\s+true\s+buildpack-2.file`))
   124  				})
   125  			})
   126  			When("there are no buildpacks", func() {
   127  				BeforeEach(func() {
   128  					buildpacks := []v7action.Buildpack{}
   129  					fakeActor.GetBuildpacksReturns(buildpacks, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
   130  				})
   131  				It("prints a table of buildpacks", func() {
   132  					Expect(executeErr).NotTo(HaveOccurred())
   133  					Expect(testUI.Err).To(Say("some-warning-1"))
   134  					Expect(testUI.Err).To(Say("some-warning-2"))
   135  					Expect(testUI.Out).To(Say("No buildpacks found"))
   136  				})
   137  			})
   138  		})
   139  	})
   140  })