forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar_builder_test.go
80 lines (66 loc) · 1.95 KB
/
tar_builder_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
package archive_test
import (
"archive/tar"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"testing"
"time"
"github.com/buildpacks/pack/pkg/archive"
h "github.com/buildpacks/pack/testhelpers"
"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)
func TestTarBuilder(t *testing.T) {
color.Disable(true)
defer color.Disable(false)
rand.Seed(time.Now().UTC().UnixNano())
spec.Run(t, "TarBuilder", testTarBuilder, spec.Sequential(), spec.Report(report.Terminal{}))
}
func testTarBuilder(t *testing.T, when spec.G, it spec.S) {
var (
tmpDir string
tarBuilder archive.TarBuilder
)
it.Before(func() {
var err error
tmpDir, err = ioutil.TempDir("", "tar-builder-test")
h.AssertNil(t, err)
tarBuilder = archive.TarBuilder{}
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(tmpDir))
})
when("#AddFile", func() {
it("adds file", func() {
tarBuilder.AddFile("file1", 0777, archive.NormalizedDateTime, []byte("file-1 content"))
reader := tarBuilder.Reader(archive.DefaultTarWriterFactory())
tr := tar.NewReader(reader)
verify := h.NewTarVerifier(t, tr, 0, 0)
verify.NextFile("file1", "file-1 content", int64(os.ModePerm))
verify.NoMoreFilesExist()
})
})
when("#AddDir", func() {
it("adds dir", func() {
tarBuilder.AddDir("path/of/dir", 0777, archive.NormalizedDateTime)
reader := tarBuilder.Reader(archive.DefaultTarWriterFactory())
tr := tar.NewReader(reader)
verify := h.NewTarVerifier(t, tr, 0, 0)
verify.NextDirectory("path/of/dir", int64(os.ModePerm))
verify.NoMoreFilesExist()
})
})
when("#WriteToPath", func() {
it("writes to path", func() {
path := filepath.Join(tmpDir, "some.txt")
h.AssertNil(t, tarBuilder.WriteToPath(path, archive.DefaultTarWriterFactory()))
})
it("fails if dir doesn't exist", func() {
path := "dir/some.txt"
h.AssertError(t, tarBuilder.WriteToPath(path, archive.DefaultTarWriterFactory()), "create file for tar")
})
})
}