github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/construct/archive/archive_test.go (about)

     1  package archive_test
     2  
     3  import (
     4  	"github.com/cloudfoundry-incubator/stembuild/assets"
     5  	archive2 "github.com/cloudfoundry-incubator/stembuild/construct/archive"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("Zip", func() {
    11  
    12  	zip := new(archive2.Zip)
    13  
    14  	Describe("Unzip", func() {
    15  		It("should return byte array of the file when it is found in the archive", func() {
    16  			fileArchive, err := assets.Asset("StemcellAutomation.zip")
    17  			Expect(err).ToNot(HaveOccurred())
    18  			Expect(fileArchive).ToNot(BeNil())
    19  
    20  			r, err := zip.Unzip(fileArchive, "Setup.ps1")
    21  			Expect(err).ToNot(HaveOccurred())
    22  			Expect(r).ToNot(BeNil())
    23  		})
    24  
    25  		It("should return an error if the file cannot be found in the archive", func() {
    26  			fileArchive, err := assets.Asset("StemcellAutomation.zip")
    27  			Expect(err).ToNot(HaveOccurred())
    28  			Expect(fileArchive).ToNot(BeNil())
    29  
    30  			r, err := zip.Unzip(fileArchive, "Setup2.ps1")
    31  			Expect(err).To(HaveOccurred())
    32  			Expect(err).To(MatchError("could not find Setup2.ps1 in zip archive"))
    33  			Expect(r).To(BeNil())
    34  
    35  		})
    36  
    37  		It("should return an error if the fileArchive is not a zip file", func() {
    38  			fileArchive := []byte("invalid byte archive")
    39  
    40  			r, err := zip.Unzip(fileArchive, "Setup.ps1")
    41  			Expect(err).To(HaveOccurred())
    42  			Expect(err.Error()).To(HavePrefix("invalid zip archive: "))
    43  			Expect(r).To(BeNil())
    44  		})
    45  
    46  		It("should return an error if fileArchive is nil", func() {
    47  			_, err := zip.Unzip(nil, "Setup.ps1")
    48  			Expect(err).To(HaveOccurred())
    49  			Expect(err.Error()).To(HavePrefix("invalid zip archive: "))
    50  		})
    51  	})
    52  })