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