forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bzip2_archive_test.go
120 lines (95 loc) · 3.77 KB
/
bzip2_archive_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package vacation_test
import (
"archive/tar"
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
dsnetBzip2 "github.com/dsnet/compress/bzip2"
"github.com/paketo-buildpacks/packit/v2/vacation"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testBzip2Archive(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
)
context("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())
tw := tar.NewWriter(bz)
Expect(tw.WriteHeader(&tar.Header{Name: "some-dir", Mode: 0755, Typeflag: tar.TypeDir})).To(Succeed())
_, err = tw.Write(nil)
Expect(err).NotTo(HaveOccurred())
Expect(tw.WriteHeader(&tar.Header{Name: filepath.Join("some-dir", "some-other-dir"), Mode: 0755, Typeflag: tar.TypeDir})).To(Succeed())
_, err = tw.Write(nil)
Expect(err).NotTo(HaveOccurred())
nestedFile := filepath.Join("some-dir", "some-other-dir", "some-file")
Expect(tw.WriteHeader(&tar.Header{Name: nestedFile, Mode: 0755, Size: int64(len(nestedFile))})).To(Succeed())
_, err = tw.Write([]byte(nestedFile))
Expect(err).NotTo(HaveOccurred())
for _, file := range []string{"first", "second", "third"} {
Expect(tw.WriteHeader(&tar.Header{Name: file, Mode: 0755, Size: int64(len(file))})).To(Succeed())
_, err = tw.Write([]byte(file))
Expect(err).NotTo(HaveOccurred())
}
Expect(tw.WriteHeader(&tar.Header{Name: "symlink", Mode: 0777, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: "first"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.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())
data, err := os.ReadFile(filepath.Join(tempDir, "symlink"))
Expect(err).NotTo(HaveOccurred())
Expect(data).To(Equal([]byte(`first`)))
})
it("unpackages the archive into the path but also strips the first component", func() {
var err error
err = bzip2Archive.StripComponents(1).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, "some-other-dir"),
}))
Expect(filepath.Join(tempDir, "some-other-dir")).To(BeADirectory())
Expect(filepath.Join(tempDir, "some-other-dir", "some-file")).To(BeARegularFile())
})
})
}