github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/util/manifestparser/locator_test.go (about)

     1  package manifestparser_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	. "code.cloudfoundry.org/cli/util/manifestparser"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Locator", func() {
    16  	var locator *Locator
    17  
    18  	BeforeEach(func() {
    19  		locator = NewLocator()
    20  	})
    21  
    22  	Describe("Path", func() {
    23  		var (
    24  			filepathOrDirectory string
    25  
    26  			expectedPath string
    27  			exists       bool
    28  			executeErr   error
    29  
    30  			workingDir string
    31  		)
    32  
    33  		BeforeEach(func() {
    34  			var err error
    35  			workingDir, err = ioutil.TempDir("", "manifest-locator-working-dir")
    36  			Expect(err).ToNot(HaveOccurred())
    37  		})
    38  
    39  		AfterEach(func() {
    40  			Expect(os.RemoveAll(workingDir)).ToNot(HaveOccurred())
    41  		})
    42  
    43  		JustBeforeEach(func() {
    44  			expectedPath, exists, executeErr = locator.Path(filepathOrDirectory)
    45  		})
    46  
    47  		When("given a file path", func() {
    48  			BeforeEach(func() {
    49  				filepathOrDirectory = filepath.Join(workingDir, "some-manifest.yml")
    50  				Expect(ioutil.WriteFile(filepathOrDirectory, nil, 0600)).To(Succeed())
    51  			})
    52  
    53  			It("returns the path and true", func() {
    54  				Expect(executeErr).ToNot(HaveOccurred())
    55  				Expect(expectedPath).To(Equal(filepathOrDirectory))
    56  				Expect(exists).To(BeTrue())
    57  			})
    58  		})
    59  
    60  		When("given a directory", func() {
    61  			BeforeEach(func() {
    62  				filepathOrDirectory = workingDir
    63  			})
    64  
    65  			When("a manifest.yml exists in the directory", func() {
    66  				BeforeEach(func() {
    67  					Expect(ioutil.WriteFile(filepath.Join(workingDir, "manifest.yml"), nil, 0600)).To(Succeed())
    68  				})
    69  
    70  				It("returns the path and true", func() {
    71  					Expect(executeErr).ToNot(HaveOccurred())
    72  					Expect(expectedPath).To(Equal(filepath.Join(workingDir, "manifest.yml")))
    73  					Expect(exists).To(BeTrue())
    74  				})
    75  			})
    76  
    77  			When("a manifest.yaml exists in the directory", func() {
    78  				BeforeEach(func() {
    79  					Expect(ioutil.WriteFile(filepath.Join(workingDir, "manifest.yaml"), nil, 0600)).To(Succeed())
    80  				})
    81  
    82  				It("returns the path and true", func() {
    83  					Expect(executeErr).ToNot(HaveOccurred())
    84  					Expect(expectedPath).To(Equal(filepath.Join(workingDir, "manifest.yaml")))
    85  					Expect(exists).To(BeTrue())
    86  				})
    87  			})
    88  
    89  			When("no manifest exists in the directory", func() {
    90  				It("returns empty and false", func() {
    91  					Expect(executeErr).ToNot(HaveOccurred())
    92  					Expect(expectedPath).To(BeEmpty())
    93  					Expect(exists).To(BeFalse())
    94  				})
    95  			})
    96  
    97  			When("there's an error detecting the manifest", func() {
    98  				BeforeEach(func() {
    99  					locator.FilesToCheckFor = []string{strings.Repeat("a", 10000)}
   100  				})
   101  
   102  				It("returns the error", func() {
   103  					Expect(executeErr).To(HaveOccurred())
   104  				})
   105  			})
   106  		})
   107  
   108  		When("the provided filepathOrDirectory does not exist", func() {
   109  			BeforeEach(func() {
   110  				filepathOrDirectory = "/does/not/exist"
   111  			})
   112  
   113  			It("returns empty and false", func() {
   114  				Expect(executeErr).ToNot(HaveOccurred())
   115  				Expect(expectedPath).To(BeEmpty())
   116  				Expect(exists).To(BeFalse())
   117  			})
   118  		})
   119  
   120  		When("there's an error detecting the manifest", func() {
   121  			BeforeEach(func() {
   122  				filepathOrDirectory = filepath.Join(workingDir, strings.Repeat("a", 10000))
   123  			})
   124  
   125  			It("returns the error", func() {
   126  				Expect(executeErr).To(HaveOccurred())
   127  				Expect(expectedPath).To(BeEmpty())
   128  				Expect(exists).To(BeFalse())
   129  			})
   130  		})
   131  	})
   132  })