github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/stack_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	testapi "github.com/cloudfoundry/cli/cf/api/stacks/fakes"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     9  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    10  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    11  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  
    16  	"github.com/cloudfoundry/cli/cf/command_registry"
    17  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    18  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    19  )
    20  
    21  var _ = Describe("stack command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		config              core_config.Repository
    25  		repo                *testapi.FakeStackRepository
    26  		requirementsFactory *testreq.FakeReqFactory
    27  		deps                command_registry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.Ui = ui
    32  		deps.Config = config
    33  		deps.RepoLocator = deps.RepoLocator.SetStackRepository(repo)
    34  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("stack").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	BeforeEach(func() {
    38  		ui = &testterm.FakeUI{}
    39  		config = testconfig.NewRepositoryWithDefaults()
    40  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true}
    41  		repo = &testapi.FakeStackRepository{}
    42  	})
    43  
    44  	Describe("login requirements", func() {
    45  		It("fails if the user is not logged in", func() {
    46  			requirementsFactory.LoginSuccess = false
    47  
    48  			Expect(testcmd.RunCliCommand("stack", []string{}, requirementsFactory, updateCommandDependency, false)).To(BeFalse())
    49  		})
    50  
    51  		It("fails with usage when not provided exactly one arg", func() {
    52  			requirementsFactory.LoginSuccess = true
    53  			Expect(testcmd.RunCliCommand("stack", []string{}, requirementsFactory, updateCommandDependency, false)).To(BeFalse())
    54  			Expect(ui.Outputs).To(ContainSubstrings(
    55  				[]string{"FAILED"},
    56  				[]string{"Incorrect Usage."},
    57  			))
    58  		})
    59  	})
    60  
    61  	It("returns the stack guid when '--guid' flag is provided", func() {
    62  		stack1 := models.Stack{
    63  			Name:        "Stack-1",
    64  			Description: "Stack 1 Description",
    65  			Guid:        "Stack-1-GUID",
    66  		}
    67  
    68  		repo.FindByNameReturns(stack1, nil)
    69  
    70  		testcmd.RunCliCommand("stack", []string{"Stack-1", "--guid"}, requirementsFactory, updateCommandDependency, false)
    71  
    72  		Expect(len(ui.Outputs)).To(Equal(1))
    73  		Expect(ui.Outputs[0]).To(Equal("Stack-1-GUID"))
    74  	})
    75  
    76  	It("returns the empty string as guid when '--guid' flag is provided and stack doesn't exist", func() {
    77  		stack1 := models.Stack{
    78  			Name:        "Stack-1",
    79  			Description: "Stack 1 Description",
    80  			Guid:        "Stack-1-GUID",
    81  		}
    82  
    83  		repo.FindByNameReturns(stack1, nil)
    84  
    85  		testcmd.RunCliCommand("stack", []string{"Stack-1", "--guid"}, requirementsFactory, updateCommandDependency, false)
    86  
    87  		Expect(len(ui.Outputs)).To(Equal(1))
    88  		Expect(ui.Outputs[0]).To(Equal("Stack-1-GUID"))
    89  	})
    90  
    91  	It("lists the stack requested", func() {
    92  		repo.FindByNameReturns(models.Stack{}, errors.New("Stack Stack-1 not found"))
    93  
    94  		testcmd.RunCliCommand("stack", []string{"Stack-1", "--guid"}, requirementsFactory, updateCommandDependency, false)
    95  
    96  		Expect(len(ui.Outputs)).To(Equal(1))
    97  		Expect(ui.Outputs[0]).To(Equal(""))
    98  	})
    99  
   100  	It("informs user if stack is not found", func() {
   101  		repo.FindByNameReturns(models.Stack{}, errors.New("Stack Stack-1 not found"))
   102  
   103  		testcmd.RunCliCommand("stack", []string{"Stack-1"}, requirementsFactory, updateCommandDependency, false)
   104  
   105  		Expect(ui.Outputs).To(BeInDisplayOrder(
   106  			[]string{"FAILED"},
   107  			[]string{"Stack Stack-1 not found"},
   108  		))
   109  	})
   110  })