github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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/util/testhelpers/commands" 11 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 15 . "code.cloudfoundry.org/cli/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.DeleteBuildpackGUID).To(Equal("my-buildpack-guid")) 71 72 Expect(ui.Prompts).To(ContainSubstrings([]string{"delete the buildpack my-buildpack"})) 73 Expect(ui.Outputs()).To(ContainSubstrings( 74 []string{"Deleting buildpack", "my-buildpack"}, 75 []string{"OK"}, 76 )) 77 }) 78 79 Context("when the force flag is provided", func() { 80 It("does not prompt the user to delete the buildback", func() { 81 runCommand("-f", "my-buildpack") 82 83 Expect(buildpackRepo.DeleteBuildpackGUID).To(Equal("my-buildpack-guid")) 84 85 Expect(len(ui.Prompts)).To(Equal(0)) 86 Expect(ui.Outputs()).To(ContainSubstrings( 87 []string{"Deleting buildpack", "my-buildpack"}, 88 []string{"OK"}, 89 )) 90 }) 91 }) 92 }) 93 94 Context("when the buildpack provided is not found", func() { 95 BeforeEach(func() { 96 ui = &testterm.FakeUI{Inputs: []string{"y"}} 97 buildpackRepo.FindByNameNotFound = true 98 }) 99 100 It("warns the user", func() { 101 runCommand("my-buildpack") 102 103 Expect(buildpackRepo.FindByNameName).To(Equal("my-buildpack")) 104 Expect(buildpackRepo.FindByNameNotFound).To(BeTrue()) 105 106 Expect(ui.Outputs()).To(ContainSubstrings( 107 []string{"Deleting", "my-buildpack"}, 108 []string{"OK"}, 109 )) 110 111 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"my-buildpack", "does not exist"})) 112 }) 113 }) 114 115 Context("when an error occurs", func() { 116 BeforeEach(func() { 117 ui = &testterm.FakeUI{Inputs: []string{"y"}} 118 119 buildpackRepo.FindByNameBuildpack = models.Buildpack{ 120 Name: "my-buildpack", 121 GUID: "my-buildpack-guid", 122 } 123 buildpackRepo.DeleteAPIResponse = errors.New("failed badly") 124 }) 125 126 It("fails with the error", func() { 127 runCommand("my-buildpack") 128 129 Expect(buildpackRepo.DeleteBuildpackGUID).To(Equal("my-buildpack-guid")) 130 131 Expect(ui.Outputs()).To(ContainSubstrings( 132 []string{"Deleting buildpack", "my-buildpack"}, 133 []string{"FAILED"}, 134 []string{"my-buildpack"}, 135 []string{"failed badly"}, 136 )) 137 }) 138 }) 139 }) 140 })