github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/buildpack/buildpacks_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/flags"
     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  
    13  	"code.cloudfoundry.org/cli/cf/commands/buildpack"
    14  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("ListBuildpacks", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		buildpackRepo       *apifakes.OldFakeBuildpackRepository
    23  		requirementsFactory *requirementsfakes.FakeFactory
    24  		deps                commandregistry.Dependency
    25  	)
    26  
    27  	updateCommandDependency := func(pluginCall bool) {
    28  		deps.UI = ui
    29  		deps.RepoLocator = deps.RepoLocator.SetBuildpackRepository(buildpackRepo)
    30  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("buildpacks").SetDependency(deps, pluginCall))
    31  	}
    32  
    33  	BeforeEach(func() {
    34  		ui = &testterm.FakeUI{}
    35  		buildpackRepo = new(apifakes.OldFakeBuildpackRepository)
    36  		requirementsFactory = new(requirementsfakes.FakeFactory)
    37  	})
    38  
    39  	runCommand := func(args ...string) bool {
    40  		return testcmd.RunCLICommand("buildpacks", args, requirementsFactory, updateCommandDependency, false, ui)
    41  	}
    42  
    43  	Context("when arguments are provided", func() {
    44  		var cmd commandregistry.Command
    45  		var flagContext flags.FlagContext
    46  
    47  		BeforeEach(func() {
    48  			cmd = &buildpack.ListBuildpacks{}
    49  			cmd.SetDependency(deps, false)
    50  			flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    51  		})
    52  
    53  		It("should fail with usage", func() {
    54  			flagContext.Parse("blahblah")
    55  
    56  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    57  			Expect(err).NotTo(HaveOccurred())
    58  
    59  			err = testcmd.RunRequirements(reqs)
    60  			Expect(err).To(HaveOccurred())
    61  			Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    62  			Expect(err.Error()).To(ContainSubstring("No argument required"))
    63  		})
    64  	})
    65  
    66  	It("fails requirements when login fails", func() {
    67  		requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    68  		Expect(runCommand()).To(BeFalse())
    69  	})
    70  
    71  	Context("when logged in", func() {
    72  		BeforeEach(func() {
    73  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    74  		})
    75  
    76  		It("lists buildpacks", func() {
    77  			p1 := 5
    78  			p2 := 10
    79  			p3 := 15
    80  			t := true
    81  			f := false
    82  			linux := "cflinuxfs2"
    83  
    84  			buildpackRepo.Buildpacks = []models.Buildpack{
    85  				{Name: "Buildpack-1", Stack: linux, Position: &p1, Enabled: &t, Locked: &f},
    86  				{Name: "Buildpack-2", Stack: linux, Position: &p2, Enabled: &f, Locked: &t},
    87  				{Name: "Buildpack-3", Stack: linux, Position: &p3, Enabled: &t, Locked: &f},
    88  			}
    89  
    90  			runCommand()
    91  
    92  			Expect(ui.Outputs()).To(ContainSubstrings(
    93  				[]string{"Getting buildpacks"},
    94  				[]string{"buildpack", "stack", "position", "enabled"},
    95  				[]string{"Buildpack-1", "cflinuxfs2", "5", "true", "false"},
    96  				[]string{"Buildpack-2", "cflinuxfs2", "10", "false", "true"},
    97  				[]string{"Buildpack-3", "cflinuxfs2", "15", "true", "false"},
    98  			))
    99  		})
   100  
   101  		It("tells the user if no build packs exist", func() {
   102  			runCommand()
   103  			Expect(ui.Outputs()).To(ContainSubstrings(
   104  				[]string{"Getting buildpacks"},
   105  				[]string{"No buildpacks found"},
   106  			))
   107  		})
   108  	})
   109  
   110  })