github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+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 Context("buildpack not found", func() { 157 BeforeEach(func() { 158 ui = &testterm.FakeUI{Inputs: []string{"y"}} 159 buildpackRepo.FindByNameAndStackNotFound = true 160 }) 161 162 It("warns the user", func() { 163 runCommand("my-buildpack", "-s", "my-stack") 164 165 Expect(buildpackRepo.FindByNameAndStackName).To(Equal("my-buildpack")) 166 Expect(buildpackRepo.FindByNameAndStackStack).To(Equal("my-stack")) 167 168 Expect(ui.Outputs()).To(ContainSubstrings( 169 []string{"Deleting", "my-buildpack", "my-stack"}, 170 []string{"OK"}, 171 )) 172 173 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"my-buildpack", "does not exist"})) 174 }) 175 }) 176 }) 177 }) 178 179 Context("when the buildpack provided is not found", func() { 180 BeforeEach(func() { 181 ui = &testterm.FakeUI{Inputs: []string{"y"}} 182 buildpackRepo.FindByNameNotFound = true 183 }) 184 185 It("warns the user", func() { 186 runCommand("my-buildpack") 187 188 Expect(buildpackRepo.FindByNameName).To(Equal("my-buildpack")) 189 190 Expect(ui.Outputs()).To(ContainSubstrings( 191 []string{"Deleting", "my-buildpack"}, 192 []string{"OK"}, 193 )) 194 195 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"my-buildpack", "does not exist"})) 196 }) 197 }) 198 199 Context("when an error occurs", func() { 200 BeforeEach(func() { 201 ui = &testterm.FakeUI{Inputs: []string{"y"}} 202 203 buildpackRepo.FindByNameBuildpack = models.Buildpack{ 204 Name: "my-buildpack", 205 GUID: "my-buildpack-guid", 206 } 207 buildpackRepo.DeleteAPIResponse = errors.New("failed badly") 208 }) 209 210 It("fails with the error", func() { 211 runCommand("my-buildpack") 212 213 Expect(buildpackRepo.DeleteBuildpackGUID).To(Equal("my-buildpack-guid")) 214 215 Expect(ui.Outputs()).To(ContainSubstrings( 216 []string{"Deleting buildpack", "my-buildpack"}, 217 []string{"FAILED"}, 218 []string{"my-buildpack"}, 219 []string{"failed badly"}, 220 )) 221 }) 222 }) 223 }) 224 })