github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/v7action/buildpack_unix_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package v7action_test
     5  
     6  import (
     7  	"fmt"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  
    15  	. "code.cloudfoundry.org/cli/actor/v7action"
    16  	"code.cloudfoundry.org/cli/integration/helpers"
    17  	"code.cloudfoundry.org/ykk"
    18  )
    19  
    20  var _ = Describe("Buildpack", func() {
    21  	Describe("Zipit", func() {
    22  		var (
    23  			source string
    24  			target string
    25  
    26  			executeErr error
    27  		)
    28  
    29  		JustBeforeEach(func() {
    30  			executeErr = Zipit(source, target, "testzip-")
    31  		})
    32  
    33  		When("the source directory exists", func() {
    34  			var subDir string
    35  
    36  			BeforeEach(func() {
    37  				var err error
    38  
    39  				source, err = ioutil.TempDir("", "zipit-source-")
    40  				Expect(err).ToNot(HaveOccurred())
    41  
    42  				Expect(ioutil.WriteFile(filepath.Join(source, "file1"), []byte{}, 0700)).To(Succeed())
    43  				Expect(ioutil.WriteFile(filepath.Join(source, "file2"), []byte{}, 0644)).To(Succeed())
    44  				subDir, err = ioutil.TempDir(source, "zipit-subdir-")
    45  				Expect(err).ToNot(HaveOccurred())
    46  				Expect(ioutil.WriteFile(filepath.Join(subDir, "file3"), []byte{}, 0755)).To(Succeed())
    47  
    48  				p := filepath.FromSlash(fmt.Sprintf("buildpack-%s.zip", helpers.RandomName()))
    49  				target, err = filepath.Abs(p)
    50  				Expect(err).ToNot(HaveOccurred())
    51  			})
    52  
    53  			AfterEach(func() {
    54  				Expect(os.RemoveAll(source)).ToNot(HaveOccurred())
    55  				Expect(os.RemoveAll(target)).ToNot(HaveOccurred())
    56  			})
    57  
    58  			It("preserves the file permissions in the zip", func() {
    59  				Expect(executeErr).ToNot(HaveOccurred())
    60  				zipFile, err := os.Open(target)
    61  				Expect(err).ToNot(HaveOccurred())
    62  				defer zipFile.Close()
    63  
    64  				zipStat, err := zipFile.Stat()
    65  				Expect(err).ToNot(HaveOccurred())
    66  				reader, err := ykk.NewReader(zipFile, zipStat.Size())
    67  				Expect(err).ToNot(HaveOccurred())
    68  
    69  				Expect(reader.File).To(HaveLen(4))
    70  				Expect(reader.File[0].Name).To(Equal("file1"))
    71  				Expect(reader.File[0].Mode()).To(Equal(os.FileMode(0700)))
    72  
    73  				Expect(reader.File[1].Name).To(Equal("file2"))
    74  				Expect(reader.File[1].Mode()).To(Equal(os.FileMode(0644)))
    75  
    76  				dirName := fmt.Sprintf("%s/", filepath.Base(subDir))
    77  				Expect(reader.File[2].Name).To(Equal(dirName))
    78  				Expect(reader.File[2].Mode()).To(Equal(os.ModeDir | 0700))
    79  
    80  				Expect(reader.File[3].Name).To(Equal(filepath.Join(dirName, "file3")))
    81  				Expect(reader.File[3].Mode()).To(Equal(os.FileMode(0755)))
    82  			})
    83  		})
    84  	})
    85  })