github.com/loafoe/cli@v7.1.0+incompatible/command/v7/delete_buildpack_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  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/util/ui"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/gbytes"
    15  )
    16  
    17  var _ = Describe("delete-buildpack Command", func() {
    18  
    19  	var (
    20  		cmd             DeleteBuildpackCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v7fakes.FakeActor
    25  		input           *Buffer
    26  		binaryName      string
    27  		buildpackName   string
    28  		executeErr      error
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		input = NewBuffer()
    33  		fakeActor = new(v7fakes.FakeActor)
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    37  
    38  		cmd = DeleteBuildpackCommand{
    39  			BaseCommand: BaseCommand{
    40  				Actor:       fakeActor,
    41  				UI:          testUI,
    42  				Config:      fakeConfig,
    43  				SharedActor: fakeSharedActor,
    44  			},
    45  		}
    46  		binaryName = "faceman"
    47  		buildpackName = "the-buildpack"
    48  		fakeConfig.BinaryNameReturns(binaryName)
    49  		cmd.RequiredArgs.Buildpack = buildpackName
    50  		cmd.Force = true
    51  	})
    52  
    53  	When("checking target fails", func() {
    54  		BeforeEach(func() {
    55  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    56  		})
    57  
    58  		It("returns an error if the check fails", func() {
    59  			executeErr = cmd.Execute(nil)
    60  
    61  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "faceman"}))
    62  
    63  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    64  			shouldCheckTargetedOrg, shouldCheckTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    65  			Expect(shouldCheckTargetedOrg).To(BeFalse())
    66  			Expect(shouldCheckTargetedSpace).To(BeFalse())
    67  		})
    68  	})
    69  
    70  	When("the DeleteBuildpack actor completes successfully", func() {
    71  		BeforeEach(func() {
    72  			fakeActor.DeleteBuildpackByNameAndStackReturns(nil, nil)
    73  		})
    74  		JustBeforeEach(func() {
    75  			executeErr = cmd.Execute(nil)
    76  		})
    77  
    78  		When("--force is specified", func() {
    79  			BeforeEach(func() {
    80  				cmd.Force = true
    81  			})
    82  
    83  			When("a stack is not specified", func() {
    84  				BeforeEach(func() {
    85  					cmd.Stack = ""
    86  				})
    87  
    88  				It("prints appropriate output", func() {
    89  					Expect(testUI.Out).To(Say("Deleting buildpack the-buildpack..."))
    90  					Expect(testUI.Out).To(Say("OK"))
    91  				})
    92  			})
    93  
    94  			When("a stack is specified", func() {
    95  				BeforeEach(func() {
    96  					cmd.Stack = "a-stack"
    97  				})
    98  
    99  				It("prints appropriate output that includes the stack name", func() {
   100  					Expect(testUI.Out).To(Say("Deleting buildpack the-buildpack with stack a-stack..."))
   101  					Expect(testUI.Out).To(Say("OK"))
   102  				})
   103  			})
   104  		})
   105  
   106  		When("--force is not specified", func() {
   107  			BeforeEach(func() {
   108  				cmd.Force = false
   109  			})
   110  
   111  			When("the user inputs yes", func() {
   112  				BeforeEach(func() {
   113  					_, err := input.Write([]byte("y\n"))
   114  					Expect(err).ToNot(HaveOccurred())
   115  				})
   116  
   117  				It("prompted the user for confirmation", func() {
   118  					Expect(testUI.Out).To(Say("Really delete the buildpack the-buildpack?"))
   119  					Expect(testUI.Out).To(Say("Deleting buildpack the-buildpack..."))
   120  					Expect(testUI.Out).To(Say("OK"))
   121  				})
   122  			})
   123  
   124  			When("the user inputs no", func() {
   125  				BeforeEach(func() {
   126  					_, err := input.Write([]byte("n\n"))
   127  					Expect(err).ToNot(HaveOccurred())
   128  				})
   129  
   130  				It("cancels the delete", func() {
   131  					Expect(testUI.Out).To(Say("Really delete the buildpack the-buildpack?"))
   132  					Expect(testUI.Out).To(Say("Delete cancelled"))
   133  					Expect(testUI.Out).NotTo(Say("Deleting buildpack the-buildpack..."))
   134  				})
   135  			})
   136  		})
   137  	})
   138  
   139  	When("the buildpack does not exist", func() {
   140  		BeforeEach(func() {
   141  			fakeActor.DeleteBuildpackByNameAndStackReturns(v7action.Warnings{"a-warning"}, actionerror.BuildpackNotFoundError{BuildpackName: buildpackName, StackName: "stack!"})
   142  		})
   143  
   144  		When("deleting with a stack", func() {
   145  			BeforeEach(func() {
   146  				cmd.Stack = "stack!"
   147  				executeErr = cmd.Execute(nil)
   148  			})
   149  
   150  			It("prints warnings and helpful error message (that includes the stack name)", func() {
   151  				Expect(testUI.Err).To(Say("a-warning"))
   152  				Expect(testUI.Err).To(Say(`Buildpack 'the-buildpack' with stack 'stack!' not found\.`))
   153  			})
   154  		})
   155  
   156  		When("deleting without a stack", func() {
   157  			BeforeEach(func() {
   158  				cmd.Stack = ""
   159  				executeErr = cmd.Execute(nil)
   160  			})
   161  
   162  			It("prints warnings and helpful error message", func() {
   163  				Expect(testUI.Err).To(Say("a-warning"))
   164  				Expect(testUI.Err).To(Say(`Buildpack 'the-buildpack' does not exist\.`))
   165  			})
   166  		})
   167  	})
   168  
   169  	It("delegates to the actor", func() {
   170  		cmd.Stack = "the-stack"
   171  		fakeActor.DeleteBuildpackByNameAndStackReturns(nil, nil)
   172  
   173  		executeErr = cmd.Execute(nil)
   174  
   175  		Expect(executeErr).ToNot(HaveOccurred())
   176  		actualBuildpack, actualStack := fakeActor.DeleteBuildpackByNameAndStackArgsForCall(0)
   177  		Expect(actualBuildpack).To(Equal("the-buildpack"))
   178  		Expect(actualStack).To(Equal("the-stack"))
   179  	})
   180  
   181  	It("prints warnings", func() {
   182  		cmd.Stack = "a-stack"
   183  		fakeActor.DeleteBuildpackByNameAndStackReturns(v7action.Warnings{"a-warning"}, nil)
   184  
   185  		executeErr = cmd.Execute(nil)
   186  
   187  		Expect(executeErr).ToNot(HaveOccurred())
   188  		Expect(testUI.Err).To(Say("a-warning"))
   189  	})
   190  
   191  	It("returns error from the actor and prints the errors", func() {
   192  		cmd.Stack = "a-stack"
   193  
   194  		fakeActor.DeleteBuildpackByNameAndStackReturns(v7action.Warnings{"a-warning"}, errors.New("some-error"))
   195  
   196  		executeErr = cmd.Execute(nil)
   197  
   198  		Expect(executeErr).To(MatchError("some-error"))
   199  		Expect(testUI.Err).To(Say("a-warning"))
   200  	})
   201  })