github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/application/files_test.go (about) 1 package application_test 2 3 import ( 4 testappfiles "github.com/cloudfoundry/cli/cf/api/app_files/fakes" 5 "github.com/cloudfoundry/cli/cf/configuration/core_config" 6 "github.com/cloudfoundry/cli/cf/models" 7 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 8 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 9 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 10 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 11 12 . "github.com/cloudfoundry/cli/cf/commands/application" 13 . "github.com/cloudfoundry/cli/testhelpers/matchers" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("files command", func() { 19 var ( 20 ui *testterm.FakeUI 21 configRepo core_config.ReadWriter 22 requirementsFactory *testreq.FakeReqFactory 23 appFilesRepo *testappfiles.FakeAppFilesRepository 24 ) 25 26 BeforeEach(func() { 27 ui = &testterm.FakeUI{} 28 configRepo = testconfig.NewRepositoryWithDefaults() 29 appFilesRepo = &testappfiles.FakeAppFilesRepository{} 30 requirementsFactory = &testreq.FakeReqFactory{} 31 }) 32 33 runCommand := func(args ...string) bool { 34 return testcmd.RunCommand(NewFiles(ui, configRepo, appFilesRepo), args, requirementsFactory) 35 } 36 37 Describe("requirements", func() { 38 It("fails when not logged in", func() { 39 requirementsFactory.TargetedSpaceSuccess = true 40 runCommand("my-app", "/foo") 41 }) 42 43 It("fails when a space is not targeted", func() { 44 requirementsFactory.LoginSuccess = true 45 Expect(runCommand("my-app", "/foo")).To(BeFalse()) 46 }) 47 48 It("fails with usage when not provided an app name", func() { 49 requirementsFactory.LoginSuccess = true 50 requirementsFactory.TargetedSpaceSuccess = true 51 52 passed := runCommand() 53 Expect(ui.FailedWithUsage).To(BeTrue()) 54 Expect(passed).To(BeFalse()) 55 }) 56 }) 57 58 Context("when logged in, a space is targeted and a valid app name is provided", func() { 59 BeforeEach(func() { 60 app := models.Application{} 61 app.Name = "my-found-app" 62 app.Guid = "my-app-guid" 63 64 requirementsFactory.Application = app 65 requirementsFactory.LoginSuccess = true 66 requirementsFactory.TargetedSpaceSuccess = true 67 appFilesRepo.ListFilesReturns("file 1\nfile 2", nil) 68 }) 69 70 It("it lists files in a directory", func() { 71 runCommand("my-app", "/foo") 72 73 Expect(ui.Outputs).To(ContainSubstrings( 74 []string{"Getting files for app", "my-found-app", "my-org", "my-space", "my-user"}, 75 []string{"OK"}, 76 []string{"file 1"}, 77 []string{"file 2"}, 78 )) 79 80 guid, _, path := appFilesRepo.ListFilesArgsForCall(0) 81 Expect(guid).To(Equal("my-app-guid")) 82 Expect(path).To(Equal("/foo")) 83 }) 84 85 It("does not interpolate or interpret special format characters as though it should be a format string", func() { 86 appFilesRepo.ListFilesReturns("%s %d %i", nil) 87 runCommand("my-app", "/foo") 88 89 Expect(ui.Outputs).To(ContainSubstrings([]string{"%s %d %i"})) 90 }) 91 92 Context("checking for bad flags", func() { 93 It("fails when non-positive value is given for instance", func() { 94 runCommand("-i", "-1", "my-app") 95 96 Expect(ui.Outputs).To(ContainSubstrings( 97 []string{"FAILED"}, 98 []string{"Invalid instance"}, 99 []string{"Instance must be a positive integer"}, 100 )) 101 }) 102 103 It("fails when instance is larger than instance count", func() { 104 runCommand("-i", "5", "my-app") 105 106 Expect(ui.Outputs).To(ContainSubstrings( 107 []string{"FAILED"}, 108 []string{"Invalid instance"}, 109 []string{"Instance must be less than"}, 110 )) 111 }) 112 113 }) 114 115 Context("when there is no file to be listed", func() { 116 BeforeEach(func() { 117 appFilesRepo.ListFilesReturns("", nil) 118 }) 119 120 It("informs user that the directory is empty", func() { 121 runCommand("my-app", "/foo") 122 123 Expect(ui.Outputs).To(ContainSubstrings( 124 []string{"Getting files for app", "my-found-app", "my-org", "my-space", "my-user"}, 125 []string{"OK"}, 126 []string{""}, 127 []string{"No files found"}, 128 )) 129 }) 130 131 }) 132 133 }) 134 })