github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/application/files_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/commands/application"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  
    14  	"code.cloudfoundry.org/cli/cf/api/appfiles/appfilesfakes"
    15  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    16  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    17  
    18  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("Files", func() {
    24  	var (
    25  		ui           *testterm.FakeUI
    26  		configRepo   coreconfig.Repository
    27  		appFilesRepo *appfilesfakes.FakeAppFilesRepository
    28  
    29  		cmd         commandregistry.Command
    30  		deps        commandregistry.Dependency
    31  		factory     *requirementsfakes.FakeFactory
    32  		flagContext flags.FlagContext
    33  
    34  		loginRequirement          requirements.Requirement
    35  		targetedSpaceRequirement  requirements.Requirement
    36  		deaApplicationRequirement *requirementsfakes.FakeDEAApplicationRequirement
    37  	)
    38  
    39  	BeforeEach(func() {
    40  		ui = &testterm.FakeUI{}
    41  
    42  		configRepo = testconfig.NewRepositoryWithDefaults()
    43  		appFilesRepo = new(appfilesfakes.FakeAppFilesRepository)
    44  		repoLocator := deps.RepoLocator.SetAppFileRepository(appFilesRepo)
    45  
    46  		deps = commandregistry.Dependency{
    47  			UI:          ui,
    48  			Config:      configRepo,
    49  			RepoLocator: repoLocator,
    50  		}
    51  
    52  		cmd = &application.Files{}
    53  		cmd.SetDependency(deps, false)
    54  
    55  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    56  
    57  		factory = new(requirementsfakes.FakeFactory)
    58  
    59  		loginRequirement = &passingRequirement{Name: "login-requirement"}
    60  		factory.NewLoginRequirementReturns(loginRequirement)
    61  
    62  		targetedSpaceRequirement = &passingRequirement{}
    63  		factory.NewTargetedSpaceRequirementReturns(targetedSpaceRequirement)
    64  
    65  		deaApplicationRequirement = new(requirementsfakes.FakeDEAApplicationRequirement)
    66  		factory.NewDEAApplicationRequirementReturns(deaApplicationRequirement)
    67  		app := models.Application{}
    68  		app.InstanceCount = 1
    69  		app.GUID = "app-guid"
    70  		app.Name = "app-name"
    71  		deaApplicationRequirement.GetApplicationReturns(app)
    72  	})
    73  
    74  	Describe("Requirements", func() {
    75  		Context("when not provided one or two args", func() {
    76  			BeforeEach(func() {
    77  				flagContext.Parse("app-name", "the-path", "extra-arg")
    78  			})
    79  
    80  			It("fails with usage", func() {
    81  				_, err := cmd.Requirements(factory, flagContext)
    82  				Expect(err).To(HaveOccurred())
    83  				Expect(ui.Outputs()).To(ContainSubstrings(
    84  					[]string{"FAILED"},
    85  					[]string{"Incorrect Usage. Requires an argument"},
    86  				))
    87  			})
    88  		})
    89  
    90  		Context("when provided exactly one arg", func() {
    91  			BeforeEach(func() {
    92  				flagContext.Parse("app-name")
    93  			})
    94  
    95  			It("returns a LoginRequirement", func() {
    96  				actualRequirements, err := cmd.Requirements(factory, flagContext)
    97  				Expect(err).NotTo(HaveOccurred())
    98  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
    99  				Expect(actualRequirements).To(ContainElement(loginRequirement))
   100  			})
   101  
   102  			It("returns a TargetedSpaceRequirement", func() {
   103  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   104  				Expect(err).NotTo(HaveOccurred())
   105  				Expect(factory.NewTargetedSpaceRequirementCallCount()).To(Equal(1))
   106  
   107  				Expect(actualRequirements).To(ContainElement(targetedSpaceRequirement))
   108  			})
   109  
   110  			It("returns an DEAApplicationRequirement", func() {
   111  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   112  				Expect(err).NotTo(HaveOccurred())
   113  				Expect(factory.NewDEAApplicationRequirementCallCount()).To(Equal(1))
   114  				Expect(factory.NewDEAApplicationRequirementArgsForCall(0)).To(Equal("app-name"))
   115  				Expect(actualRequirements).To(ContainElement(deaApplicationRequirement))
   116  			})
   117  		})
   118  
   119  		Context("when provided exactly two args", func() {
   120  			BeforeEach(func() {
   121  				flagContext.Parse("app-name", "the-path")
   122  			})
   123  
   124  			It("returns a LoginRequirement", func() {
   125  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   126  				Expect(err).NotTo(HaveOccurred())
   127  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
   128  				Expect(actualRequirements).To(ContainElement(loginRequirement))
   129  			})
   130  
   131  			It("returns a TargetedSpaceRequirement", func() {
   132  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   133  				Expect(err).NotTo(HaveOccurred())
   134  				Expect(factory.NewTargetedSpaceRequirementCallCount()).To(Equal(1))
   135  
   136  				Expect(actualRequirements).To(ContainElement(targetedSpaceRequirement))
   137  			})
   138  
   139  			It("returns an DEAApplicationRequirement", func() {
   140  				actualRequirements, err := cmd.Requirements(factory, flagContext)
   141  				Expect(err).NotTo(HaveOccurred())
   142  				Expect(factory.NewDEAApplicationRequirementCallCount()).To(Equal(1))
   143  				Expect(factory.NewDEAApplicationRequirementArgsForCall(0)).To(Equal("app-name"))
   144  				Expect(actualRequirements).To(ContainElement(deaApplicationRequirement))
   145  			})
   146  		})
   147  	})
   148  
   149  	Describe("Execute", func() {
   150  		var (
   151  			args []string
   152  			err  error
   153  		)
   154  
   155  		JustBeforeEach(func() {
   156  			err = flagContext.Parse(args...)
   157  			Expect(err).NotTo(HaveOccurred())
   158  			cmd.Requirements(factory, flagContext)
   159  			err = cmd.Execute(flagContext)
   160  		})
   161  
   162  		Context("when given a valid instance", func() {
   163  			BeforeEach(func() {
   164  				args = []string{"app-name", "-i", "0"}
   165  			})
   166  
   167  			It("tells the user it is getting the files", func() {
   168  				Expect(err).NotTo(HaveOccurred())
   169  				Expect(ui.Outputs()).To(ContainSubstrings(
   170  					[]string{"Getting files for app app-name"},
   171  				))
   172  			})
   173  
   174  			It("tries to list the files", func() {
   175  				Expect(err).NotTo(HaveOccurred())
   176  				Expect(appFilesRepo.ListFilesCallCount()).To(Equal(1))
   177  				appGUID, instance, path := appFilesRepo.ListFilesArgsForCall(0)
   178  				Expect(appGUID).To(Equal("app-guid"))
   179  				Expect(instance).To(Equal(0))
   180  				Expect(path).To(Equal("/"))
   181  			})
   182  
   183  			Context("when listing the files succeeds", func() {
   184  				BeforeEach(func() {
   185  					appFilesRepo.ListFilesReturns("files", nil)
   186  				})
   187  
   188  				It("tells the user OK", func() {
   189  					Expect(err).NotTo(HaveOccurred())
   190  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"}))
   191  				})
   192  
   193  				Context("when the files are empty", func() {
   194  					BeforeEach(func() {
   195  						appFilesRepo.ListFilesReturns("", nil)
   196  					})
   197  
   198  					It("tells the user empty file or no files were found", func() {
   199  						Expect(err).NotTo(HaveOccurred())
   200  						Expect(ui.Outputs()).To(ContainSubstrings([]string{"Empty file or folder"}))
   201  					})
   202  				})
   203  
   204  				Context("when the files are not empty", func() {
   205  					BeforeEach(func() {
   206  						appFilesRepo.ListFilesReturns("the-files", nil)
   207  					})
   208  
   209  					It("tells the user which files were found", func() {
   210  						Expect(err).NotTo(HaveOccurred())
   211  						Expect(ui.Outputs()).To(ContainSubstrings([]string{"the-files"}))
   212  					})
   213  				})
   214  			})
   215  
   216  			Context("when listing the files fails with an error", func() {
   217  				BeforeEach(func() {
   218  					appFilesRepo.ListFilesReturns("", errors.New("list-files-err"))
   219  				})
   220  
   221  				It("fails with error", func() {
   222  					Expect(err).To(HaveOccurred())
   223  					Expect(err.Error()).To(Equal("list-files-err"))
   224  				})
   225  			})
   226  		})
   227  
   228  		Context("when given a negative instance", func() {
   229  			BeforeEach(func() {
   230  				args = []string{"app-name", "-i", "-1"}
   231  			})
   232  
   233  			It("fails with error", func() {
   234  				Expect(err).To(HaveOccurred())
   235  				Expect(err.Error()).To(ContainSubstring("Instance must be a positive integer"))
   236  			})
   237  		})
   238  
   239  		Context("when given an instance greater than the app's instance count", func() {
   240  			BeforeEach(func() {
   241  				args = []string{"app-name", "-i", "2"}
   242  			})
   243  
   244  			It("fails with error", func() {
   245  				Expect(err).To(HaveOccurred())
   246  				errStr := err.Error()
   247  				Expect(errStr).To(ContainSubstring("Invalid instance: 2"))
   248  				Expect(errStr).To(ContainSubstring("Instance must be less than 1"))
   249  			})
   250  		})
   251  
   252  		Context("when given a path", func() {
   253  			BeforeEach(func() {
   254  				args = []string{"app-name", "the-path"}
   255  			})
   256  
   257  			It("lists the files with the given path", func() {
   258  				Expect(err).NotTo(HaveOccurred())
   259  				_, _, path := appFilesRepo.ListFilesArgsForCall(0)
   260  				Expect(path).To(Equal("the-path"))
   261  			})
   262  		})
   263  	})
   264  })