-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar_test.go
92 lines (75 loc) · 1.84 KB
/
tar_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
package tar
import (
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
dir string // to be set by TestMain
)
func TestRecursiveTar(t *testing.T) {
a := assert.New(t)
f, e := ioutil.TempFile("", "*.tar.gz")
a.NoError(e)
defer os.RemoveAll(f.Name())
a.NoError(Tar(f, dir, nil, true))
o, e := exec.Command("tar", "-ztf", f.Name()).Output()
a.NoError(e)
a.Equal("a\nb/b\n", string(o))
}
func TestRecursiveTarWithIncludeFileter(t *testing.T) {
a := assert.New(t)
f, e := ioutil.TempFile("", "*.tar.gz")
a.NoError(e)
defer os.RemoveAll(f.Name())
inc := func(dir, relative string, fi os.FileInfo) bool {
level := len(strings.Split(path.Join(relative, fi.Name()), "/")) - 1
return level == 0 // only files in the top directory level.
}
a.NoError(Tar(f, dir, inc, true))
o, e := exec.Command("tar", "-ztf", f.Name()).Output()
a.NoError(e)
a.Equal("a\n", string(o))
}
// Create a directory hierarchy in a temporary directory and returns
// this temporary directory. The hierarchy includes two files:
//
// /a
// /b/b
//
// where /a is composed of 1024 'a's, and /b/b of 1024 'b's.
func createDirectoryHierarchy() (dir string, e error) {
if dir, e = ioutil.TempDir("", ""); e != nil {
return "", e
}
fill := func(a rune, n int) []byte {
b := make([]byte, n)
for i := range b {
b[i] = byte(a)
}
return b
}
fn := filepath.Join(dir, "a")
ioutil.WriteFile(fn, fill('a', 1024), 0700)
if e = os.Mkdir(path.Join(dir, "b"), 0700); e != nil {
return "", e
}
fn = filepath.Join(dir, "b", "b")
ioutil.WriteFile(fn, fill('b', 1024), 0700)
return dir, nil
}
func TestMain(m *testing.M) {
d, e := createDirectoryHierarchy()
if e != nil {
log.Fatalf("Cannot create testing directory hierarchy: %v", e)
}
defer os.RemoveAll(dir)
dir = d
os.Exit(m.Run())
}