github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/buildpack/delete_buildpack_test.go (about)

     1  package buildpack_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/errors"
     7  	"code.cloudfoundry.org/cli/cf/models"
     8  	"code.cloudfoundry.org/cli/cf/requirements"
     9  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    10  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    11  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  
    15  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    16  )
    17  
    18  var _ = Describe("delete-buildpack command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		buildpackRepo       *apifakes.OldFakeBuildpackRepository
    22  		requirementsFactory *requirementsfakes.FakeFactory
    23  		deps                commandregistry.Dependency
    24  	)
    25  
    26  	updateCommandDependency := func(pluginCall bool) {
    27  		deps.UI = ui
    28  		deps.RepoLocator = deps.RepoLocator.SetBuildpackRepository(buildpackRepo)
    29  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-buildpack").SetDependency(deps, pluginCall))
    30  	}
    31  
    32  	BeforeEach(func() {
    33  		ui = &testterm.FakeUI{}
    34  		buildpackRepo = new(apifakes.OldFakeBuildpackRepository)
    35  		requirementsFactory = new(requirementsfakes.FakeFactory)
    36  	})
    37  
    38  	runCommand := func(args ...string) bool {
    39  		return testcmd.RunCLICommand("delete-buildpack", args, requirementsFactory, updateCommandDependency, false, ui)
    40  	}
    41  
    42  	Context("when the user is not logged in", func() {
    43  		BeforeEach(func() {
    44  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    45  		})
    46  
    47  		It("fails requirements", func() {
    48  			Expect(runCommand("-f", "my-buildpack")).To(BeFalse())
    49  		})
    50  	})
    51  
    52  	Context("when the user is logged in", func() {
    53  		BeforeEach(func() {
    54  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    55  		})
    56  
    57  		Context("when the buildpack exists", func() {
    58  			BeforeEach(func() {
    59  				buildpackRepo.FindByNameBuildpack = models.Buildpack{
    60  					Name: "my-buildpack",
    61  					GUID: "my-buildpack-guid",
    62  				}
    63  			})
    64  
    65  			It("deletes the buildpack", func() {
    66  				ui = &testterm.FakeUI{Inputs: []string{"y"}}
    67  
    68  				runCommand("my-buildpack")
    69  
    70  				Expect(buildpackRepo.FindByNameName).To(Equal("my-buildpack"))
    71  
    72  				Expect(buildpackRepo.DeleteBuildpackGUID).To(Equal("my-buildpack-guid"))
    73  
    74  				Expect(ui.Prompts).To(ContainSubstrings([]string{"delete the buildpack my-buildpack"}))
    75  				Expect(ui.Outputs()).To(ContainSubstrings(
    76  					[]string{"Deleting buildpack", "my-buildpack"},
    77  					[]string{"OK"},
    78  				))
    79  			})
    80  
    81  			Context("when the force flag is provided", func() {
    82  				It("does not prompt the user to delete the buildback", func() {
    83  					runCommand("-f", "my-buildpack")
    84  
    85  					Expect(buildpackRepo.DeleteBuildpackGUID).To(Equal("my-buildpack-guid"))
    86  
    87  					Expect(len(ui.Prompts)).To(Equal(0))
    88  					Expect(ui.Outputs()).To(ContainSubstrings(
    89  						[]string{"Deleting buildpack", "my-buildpack"},
    90  						[]string{"OK"},
    91  					))
    92  				})
    93  			})
    94  		})
    95  
    96  		Context("when multiple buildpacks with the same name exist", func() {
    97  			Context("the stack is not specified", func() {
    98  				BeforeEach(func() {
    99  					ui = &testterm.FakeUI{Inputs: []string{"y"}}
   100  					buildpackRepo.FindByNameAmbiguous = true
   101  				})
   102  				It("deletes the buildpack with the nil stack", func() {
   103  					runCommand("my-buildpack")
   104  
   105  					Expect(buildpackRepo.FindByNameName).To(Equal("my-buildpack"))
   106  
   107  					Expect(ui.Outputs()).To(ContainSubstrings(
   108  						[]string{"Deleting buildpack", "my-buildpack"},
   109  						[]string{"OK"},
   110  					))
   111  				})
   112  				Context("none of those buildpacks has a nil stack", func() {
   113  					BeforeEach(func() {
   114  						buildpackRepo.FindByNameWithNilStackNotFound = true
   115  					})
   116  					It("warns the user to specify the stack if unspecified", func() {
   117  						runCommand("my-buildpack")
   118  
   119  						Expect(buildpackRepo.FindByNameName).To(Equal("my-buildpack"))
   120  
   121  						Expect(ui.Outputs()).To(ContainSubstrings(
   122  							[]string{"FAILED"},
   123  							[]string{"Multiple buildpacks named my-buildpack found"},
   124  							[]string{"Specify the stack (using -s) to disambiguate"},
   125  						))
   126  					})
   127  				})
   128  			})
   129  
   130  			Context("the stack is specified", func() {
   131  				Context("and is found", func() {
   132  					BeforeEach(func() {
   133  						buildpackRepo.FindByNameAndStackBuildpack = models.Buildpack{
   134  							Name:  "my-buildpack",
   135  							Stack: "my-stack",
   136  							GUID:  "my-buildpack-guid",
   137  						}
   138  					})
   139  					It("deletes the buildpack if the stack is specified", func() {
   140  						ui = &testterm.FakeUI{Inputs: []string{"y"}}
   141  
   142  						runCommand("my-buildpack", "-s", "my-stack")
   143  
   144  						Expect(buildpackRepo.FindByNameAndStackName).To(Equal("my-buildpack"))
   145  						Expect(buildpackRepo.FindByNameAndStackStack).To(Equal("my-stack"))
   146  
   147  						Expect(buildpackRepo.DeleteBuildpackGUID).To(Equal("my-buildpack-guid"))
   148  
   149  						Expect(ui.Prompts).To(ContainSubstrings([]string{"delete the buildpack my-buildpack"}))
   150  						Expect(ui.Outputs()).To(ContainSubstrings(
   151  							[]string{"Deleting buildpack", "my-buildpack", "my-stack"},
   152  							[]string{"OK"},
   153  						))
   154  					})
   155  				})
   156  
   157  				Context("buildpack not found", func() {
   158  					BeforeEach(func() {
   159  						ui = &testterm.FakeUI{Inputs: []string{"y"}}
   160  						buildpackRepo.FindByNameAndStackNotFound = true
   161  					})
   162  
   163  					It("warns the user", func() {
   164  						runCommand("my-buildpack", "-s", "my-stack")
   165  
   166  						Expect(buildpackRepo.FindByNameAndStackName).To(Equal("my-buildpack"))
   167  						Expect(buildpackRepo.FindByNameAndStackStack).To(Equal("my-stack"))
   168  
   169  						Expect(ui.Outputs()).To(ContainSubstrings(
   170  							[]string{"Deleting", "my-buildpack", "my-stack"},
   171  							[]string{"OK"},
   172  						))
   173  
   174  						Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"Buildpack 'my-buildpack' with stack 'my-stack' not found."}))
   175  					})
   176  				})
   177  			})
   178  		})
   179  
   180  		Context("when the buildpack provided is not found", func() {
   181  			BeforeEach(func() {
   182  				ui = &testterm.FakeUI{Inputs: []string{"y"}}
   183  				buildpackRepo.FindByNameNotFound = true
   184  			})
   185  
   186  			It("warns the user", func() {
   187  				runCommand("my-buildpack")
   188  
   189  				Expect(buildpackRepo.FindByNameName).To(Equal("my-buildpack"))
   190  
   191  				Expect(ui.Outputs()).To(ContainSubstrings(
   192  					[]string{"Deleting", "my-buildpack"},
   193  					[]string{"OK"},
   194  				))
   195  
   196  				Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"my-buildpack", "does not exist"}))
   197  			})
   198  		})
   199  
   200  		Context("when an error occurs", func() {
   201  			BeforeEach(func() {
   202  				ui = &testterm.FakeUI{Inputs: []string{"y"}}
   203  
   204  				buildpackRepo.FindByNameBuildpack = models.Buildpack{
   205  					Name: "my-buildpack",
   206  					GUID: "my-buildpack-guid",
   207  				}
   208  				buildpackRepo.DeleteAPIResponse = errors.New("failed badly")
   209  			})
   210  
   211  			It("fails with the error", func() {
   212  				runCommand("my-buildpack")
   213  
   214  				Expect(buildpackRepo.DeleteBuildpackGUID).To(Equal("my-buildpack-guid"))
   215  
   216  				Expect(ui.Outputs()).To(ContainSubstrings(
   217  					[]string{"Deleting buildpack", "my-buildpack"},
   218  					[]string{"FAILED"},
   219  					[]string{"my-buildpack"},
   220  					[]string{"failed badly"},
   221  				))
   222  			})
   223  		})
   224  	})
   225  })