-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds new Bzip2Archive type - Adds support for bzip2 in generic Archive type
- Loading branch information
1 parent
eea8806
commit b4a9ea2
Showing
7 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package vacation_test | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
dsnetBzip2 "github.com/dsnet/compress/bzip2" | ||
"github.com/paketo-buildpacks/packit/vacation" | ||
"github.com/sclevine/spec" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func testVacationBzip2(t *testing.T, context spec.G, it spec.S) { | ||
var ( | ||
Expect = NewWithT(t).Expect | ||
) | ||
|
||
context("Bzip2Archive.Decompress", func() { | ||
var ( | ||
tempDir string | ||
bzip2Archive vacation.Bzip2Archive | ||
) | ||
|
||
it.Before(func() { | ||
var err error | ||
tempDir, err = os.MkdirTemp("", "vacation") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
buffer := bytes.NewBuffer(nil) | ||
|
||
// Using the dsnet library because the Go compression library does not | ||
// have a writer. There is recent discussion on this issue | ||
// https://github.com/golang/go/issues/4828 to add an encoder. The | ||
// library should be removed once there is a native encoder | ||
bz, err := dsnetBzip2.NewWriter(buffer, nil) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
zw := zip.NewWriter(bz) | ||
|
||
fileHeader := &zip.FileHeader{Name: "symlink"} | ||
fileHeader.SetMode(0755 | os.ModeSymlink) | ||
|
||
symlink, err := zw.CreateHeader(fileHeader) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = symlink.Write([]byte(filepath.Join("some-dir", "some-other-dir", "some-file"))) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// Some archive files will make a relative top level path directory these | ||
// should still successfully decompress. | ||
_, err = zw.Create("./") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = zw.Create("some-dir/") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = zw.Create(fmt.Sprintf("%s/", filepath.Join("some-dir", "some-other-dir"))) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
fileHeader = &zip.FileHeader{Name: filepath.Join("some-dir", "some-other-dir", "some-file")} | ||
fileHeader.SetMode(0644) | ||
|
||
nestedFile, err := zw.CreateHeader(fileHeader) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = nestedFile.Write([]byte("nested file")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
for _, name := range []string{"first", "second", "third"} { | ||
fileHeader := &zip.FileHeader{Name: name} | ||
fileHeader.SetMode(0755) | ||
|
||
f, err := zw.CreateHeader(fileHeader) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = f.Write([]byte(name)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
} | ||
|
||
Expect(zw.Close()).To(Succeed()) | ||
Expect(bz.Close()).To(Succeed()) | ||
|
||
bzip2Archive = vacation.NewBzip2Archive(bytes.NewReader(buffer.Bytes())) | ||
}) | ||
|
||
it.After(func() { | ||
Expect(os.RemoveAll(tempDir)).To(Succeed()) | ||
}) | ||
|
||
it("unpackages the archive into the path", func() { | ||
var err error | ||
err = bzip2Archive.Decompress(tempDir) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
files, err := filepath.Glob(fmt.Sprintf("%s/*", tempDir)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(files).To(ConsistOf([]string{ | ||
filepath.Join(tempDir, "first"), | ||
filepath.Join(tempDir, "second"), | ||
filepath.Join(tempDir, "third"), | ||
filepath.Join(tempDir, "some-dir"), | ||
filepath.Join(tempDir, "symlink"), | ||
})) | ||
|
||
info, err := os.Stat(filepath.Join(tempDir, "first")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(info.Mode()).To(Equal(os.FileMode(0755))) | ||
|
||
Expect(filepath.Join(tempDir, "some-dir", "some-other-dir")).To(BeADirectory()) | ||
Expect(filepath.Join(tempDir, "some-dir", "some-other-dir", "some-file")).To(BeARegularFile()) | ||
|
||
link, err := os.Readlink(filepath.Join(tempDir, "symlink")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(link).To(Equal("some-dir/some-other-dir/some-file")) | ||
|
||
data, err := os.ReadFile(filepath.Join(tempDir, "symlink")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(data).To(Equal([]byte("nested file"))) | ||
}) | ||
}) | ||
} |