forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xz_archive_test.go
128 lines (100 loc) · 3.81 KB
/
xz_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
121
122
123
124
125
126
127
128
package vacation_test
import (
"archive/tar"
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/v2/vacation"
"github.com/sclevine/spec"
"github.com/ulikunitz/xz"
. "github.com/onsi/gomega"
)
func testXZArchive(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
)
context("Decompress", func() {
var (
tempDir string
xzArchive vacation.XZArchive
)
it.Before(func() {
var err error
tempDir, err = os.MkdirTemp("", "vacation")
Expect(err).NotTo(HaveOccurred())
buffer := bytes.NewBuffer(nil)
xzw, err := xz.NewWriter(buffer)
Expect(err).NotTo(HaveOccurred())
tw := tar.NewWriter(xzw)
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(xzw.Close()).To(Succeed())
xzArchive = vacation.NewXZArchive(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 = xzArchive.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 = xzArchive.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())
})
context("failure cases", func() {
context("when it fails to create a xz reader", func() {
it("returns an error", func() {
readyArchive := vacation.NewXZArchive(bytes.NewBuffer([]byte(`something`)))
err := readyArchive.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to create xz reader")))
})
})
})
})
}