-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgz_test.go
104 lines (88 loc) · 2.06 KB
/
tgz_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
package tgz
import (
"fmt"
"os"
"path/filepath"
"testing"
)
const (
purgeTestData = true
dataDir = "test_data"
dataTar = "x.tar.gz"
)
func TestUnpackAndPackArchiveDot(t *testing.T) {
err := unpackAndPackArchive("./", "test_unpack_dot")
if err != nil {
t.Fatalf("Failed to: %v", err)
}
}
func TestUnpackAndPackArchivePrefix(t *testing.T) {
err := unpackAndPackArchive("asd/qwe/", "test_unpack_prefix")
if err != nil {
t.Fatalf("Failed to: %v", err)
}
}
func TestUnpackAndPackArchive(t *testing.T) {
err := unpackAndPackArchive("", "test_unpack")
if err != nil {
t.Fatalf("Failed to: %v", err)
}
}
func TestPackAndUnpackDirDot(t *testing.T) {
err := packAndUnpackDir("x", "./", "test_pack_dot")
if err != nil {
t.Fatalf("Failed to: %v", err)
}
}
func TestPackAndUnpackDirPrefix(t *testing.T) {
err := packAndUnpackDir("x/1", "asd/qwe/", "test_pack_prefix")
if err != nil {
t.Fatalf("Failed to: %v", err)
}
}
func TestPackAndUnpackDir(t *testing.T) {
err := packAndUnpackDir("x", "", "test_pack")
if err != nil {
t.Fatalf("Failed to: %v", err)
}
}
func unpackAndPackArchive(prefix, testPrefix string) error {
srcTar := filepath.Join(dataDir, dataTar)
dstDir := filepath.Join(dataDir, testPrefix)
tar := dstDir + ".tar.gz"
err := Unpack(srcTar, dstDir)
if err != nil {
return fmt.Errorf("unpack archive: %v", err)
}
if purgeTestData {
defer os.RemoveAll(dstDir)
}
err = PackWithPrefix(dstDir, tar, prefix, -1)
if err != nil {
return fmt.Errorf("pack archive: %v", err)
}
if purgeTestData {
defer os.Remove(tar)
}
return nil
}
func packAndUnpackDir(srcDir, prefix, testPrefix string) error {
srcDir = filepath.Join(dataDir, srcDir)
dstDir := filepath.Join(dataDir, testPrefix)
tar := dstDir + ".tar.gz"
err := PackWithPrefix(srcDir, tar, prefix, -1)
if err != nil {
return fmt.Errorf("pack archive: %v", err)
}
if purgeTestData {
defer os.Remove(tar)
}
err = Unpack(tar, dstDir)
if err != nil {
return fmt.Errorf("unpack archive: %v", err)
}
if purgeTestData {
defer os.RemoveAll(dstDir)
}
return nil
}