github.com/cloudfoundry/cli@v7.1.0+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  			originalFilepathOrDirectory string
    25  			filepathOrDirectory         string
    26  
    27  			expectedPath string
    28  			exists       bool
    29  			executeErr   error
    30  
    31  			workingDir string
    32  		)
    33  
    34  		BeforeEach(func() {
    35  			var err error
    36  			workingDir, err = ioutil.TempDir("", "manifest-locator-working-dir")
    37  			Expect(err).ToNot(HaveOccurred())
    38  			workingDir, err = filepath.EvalSymlinks(workingDir)
    39  			Expect(err).ToNot(HaveOccurred())
    40  		})
    41  
    42  		AfterEach(func() {
    43  			Expect(os.RemoveAll(workingDir)).ToNot(HaveOccurred())
    44  		})
    45  
    46  		JustBeforeEach(func() {
    47  			expectedPath, exists, executeErr = locator.Path(filepathOrDirectory)
    48  		})
    49  
    50  		When("given a file path", func() {
    51  			BeforeEach(func() {
    52  				filepathOrDirectory = filepath.Join(workingDir, "some-manifest.yml")
    53  				Expect(ioutil.WriteFile(filepathOrDirectory, nil, 0600)).To(Succeed())
    54  			})
    55  
    56  			It("returns the path and true", func() {
    57  				Expect(executeErr).ToNot(HaveOccurred())
    58  				Expect(expectedPath).To(Equal(filepathOrDirectory))
    59  				Expect(exists).To(BeTrue())
    60  			})
    61  		})
    62  
    63  		When("given a directory", func() {
    64  			BeforeEach(func() {
    65  				filepathOrDirectory = workingDir
    66  			})
    67  
    68  			When("a manifest.yml exists in the directory", func() {
    69  				BeforeEach(func() {
    70  					Expect(ioutil.WriteFile(filepath.Join(workingDir, "manifest.yml"), nil, 0600)).To(Succeed())
    71  				})
    72  
    73  				It("returns the path and true", func() {
    74  					Expect(executeErr).ToNot(HaveOccurred())
    75  					Expect(expectedPath).To(Equal(filepath.Join(workingDir, "manifest.yml")))
    76  					Expect(exists).To(BeTrue())
    77  				})
    78  			})
    79  
    80  			When("a manifest.yaml exists in the directory", func() {
    81  				BeforeEach(func() {
    82  					Expect(ioutil.WriteFile(filepath.Join(workingDir, "manifest.yaml"), nil, 0600)).To(Succeed())
    83  				})
    84  
    85  				It("returns the path and true", func() {
    86  					Expect(executeErr).ToNot(HaveOccurred())
    87  					Expect(expectedPath).To(Equal(filepath.Join(workingDir, "manifest.yaml")))
    88  					Expect(exists).To(BeTrue())
    89  				})
    90  			})
    91  
    92  			When("no manifest exists in the directory", func() {
    93  				It("returns empty and false", func() {
    94  					Expect(executeErr).ToNot(HaveOccurred())
    95  					Expect(expectedPath).To(BeEmpty())
    96  					Expect(exists).To(BeFalse())
    97  				})
    98  			})
    99  
   100  			When("there's an error detecting the manifest", func() {
   101  				BeforeEach(func() {
   102  					locator.FilesToCheckFor = []string{strings.Repeat("a", 10000)}
   103  				})
   104  
   105  				It("returns the error", func() {
   106  					Expect(executeErr).To(HaveOccurred())
   107  				})
   108  			})
   109  		})
   110  
   111  		When("the provided filepathOrDirectory does not exist", func() {
   112  			BeforeEach(func() {
   113  				filepathOrDirectory = "/does/not/exist"
   114  			})
   115  
   116  			It("returns empty and false", func() {
   117  				Expect(executeErr).ToNot(HaveOccurred())
   118  				Expect(expectedPath).To(BeEmpty())
   119  				Expect(exists).To(BeFalse())
   120  			})
   121  		})
   122  
   123  		When("there's an error detecting the manifest", func() {
   124  			BeforeEach(func() {
   125  				filepathOrDirectory = filepath.Join(workingDir, strings.Repeat("a", 10000))
   126  			})
   127  
   128  			It("returns the error", func() {
   129  				Expect(executeErr).To(HaveOccurred())
   130  				Expect(expectedPath).To(BeEmpty())
   131  				Expect(exists).To(BeFalse())
   132  			})
   133  		})
   134  
   135  		When("the path to the manifest is a symbolic link", func() {
   136  			BeforeEach(func() {
   137  				originalFilepathOrDirectory = filepath.Join(workingDir, "some-manifest.yml")
   138  				Expect(ioutil.WriteFile(originalFilepathOrDirectory, nil, 0600)).To(Succeed())
   139  				filepathOrDirectory = filepath.Join(workingDir, "link-to-some-manifest.yml")
   140  				err := os.Symlink(originalFilepathOrDirectory, filepathOrDirectory)
   141  				Expect(err).To(BeNil())
   142  			})
   143  
   144  			It("returns the absolute path, not the symbolic link", func() {
   145  				Expect(executeErr).ToNot(HaveOccurred())
   146  				Expect(expectedPath).To(Equal(originalFilepathOrDirectory))
   147  				Expect(exists).To(BeTrue())
   148  			})
   149  		})
   150  	})
   151  })