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