github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/buildpack/rename_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("rename-buildpack command", func() {
    19  	var (
    20  		fakeRepo            *apifakes.OldFakeBuildpackRepository
    21  		ui                  *testterm.FakeUI
    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(fakeRepo)
    29  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("rename-buildpack").SetDependency(deps, pluginCall))
    30  	}
    31  
    32  	BeforeEach(func() {
    33  		requirementsFactory = new(requirementsfakes.FakeFactory)
    34  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    35  		requirementsFactory.NewBuildpackRequirementReturns(new(requirementsfakes.FakeBuildpackRequirement))
    36  		ui = new(testterm.FakeUI)
    37  		fakeRepo = new(apifakes.OldFakeBuildpackRepository)
    38  	})
    39  
    40  	runCommand := func(args ...string) bool {
    41  		return testcmd.RunCLICommand("rename-buildpack", args, requirementsFactory, updateCommandDependency, false, ui)
    42  	}
    43  
    44  	It("fails requirements when called without the current name and the new name to use", func() {
    45  		passed := runCommand("my-buildpack-name")
    46  		Expect(ui.Outputs()).To(ContainSubstrings(
    47  			[]string{"Incorrect Usage", "Requires", "arguments"},
    48  		))
    49  		Expect(passed).To(BeFalse())
    50  	})
    51  
    52  	Context("when logged in", func() {
    53  		It("renames a buildpack", func() {
    54  			fakeRepo.FindByNameBuildpack = models.Buildpack{
    55  				Name: "my-buildpack",
    56  				GUID: "my-buildpack-guid",
    57  			}
    58  
    59  			runCommand("my-buildpack", "new-buildpack")
    60  			Expect(ui.Outputs()).To(ContainSubstrings(
    61  				[]string{"Renaming buildpack", "my-buildpack"},
    62  				[]string{"OK"},
    63  			))
    64  		})
    65  
    66  		It("fails when the buildpack does not exist", func() {
    67  			fakeRepo.FindByNameNotFound = true
    68  
    69  			runCommand("my-buildpack1", "new-buildpack")
    70  			Expect(ui.Outputs()).To(ContainSubstrings(
    71  				[]string{"Renaming buildpack", "my-buildpack"},
    72  				[]string{"FAILED"},
    73  				[]string{"Buildpack my-buildpack1 not found"},
    74  			))
    75  		})
    76  
    77  		It("fails when there is an error updating the buildpack", func() {
    78  			fakeRepo.FindByNameBuildpack = models.Buildpack{
    79  				Name: "my-buildpack",
    80  				GUID: "my-buildpack-guid",
    81  			}
    82  			fakeRepo.UpdateBuildpackReturns.Error = errors.New("SAD TROMBONE")
    83  
    84  			runCommand("my-buildpack1", "new-buildpack")
    85  			Expect(ui.Outputs()).To(ContainSubstrings(
    86  				[]string{"Renaming buildpack", "my-buildpack"},
    87  				[]string{"SAD TROMBONE"},
    88  			))
    89  		})
    90  	})
    91  })