-
Notifications
You must be signed in to change notification settings - Fork 43
/
archive_test.go
96 lines (87 loc) · 2.28 KB
/
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
package desync
import (
"os"
"path"
"reflect"
"testing"
)
func TestArchiveDecoderTypes(t *testing.T) {
f, err := os.Open("testdata/flat.catar")
if err != nil {
t.Fatal(err)
}
defer f.Close()
d := NewArchiveDecoder(f)
// Define an array of what is expected in the test file
expected := []interface{}{
NodeDirectory{},
NodeDevice{},
NodeFile{},
NodeFile{},
NodeSymlink{},
nil,
}
for _, exp := range expected {
v, err := d.Next()
if err != nil {
t.Fatal(err)
}
if reflect.TypeOf(exp) != reflect.TypeOf(v) {
t.Fatalf("expected %s, got %s", reflect.TypeOf(exp), reflect.TypeOf(v))
}
}
}
func TestArchiveDecoderNesting(t *testing.T) {
f, err := os.Open("testdata/nested.catar")
if err != nil {
t.Fatal(err)
}
defer f.Close()
d := NewArchiveDecoder(f)
// Define an array of what is expected in the test file
expected := []struct {
Type interface{}
Name string
UID int
GID int
}{
{Type: NodeDirectory{}, Name: ".", UID: 500, GID: 500},
{Type: NodeDirectory{}, Name: "dir1", UID: 500, GID: 500},
{Type: NodeDirectory{}, Name: path.Join("dir1", "sub11"), UID: 500, GID: 500},
{Type: NodeFile{}, Name: path.Join("dir1", "sub11", "f11"), UID: 500, GID: 500},
{Type: NodeFile{}, Name: path.Join("dir1", "sub11", "f12"), UID: 500, GID: 500},
{Type: NodeDirectory{}, Name: path.Join("dir1", "sub12"), UID: 500, GID: 500},
{Type: NodeDirectory{}, Name: "dir2", UID: 500, GID: 500},
{Type: NodeDirectory{}, Name: path.Join("dir2", "sub21"), UID: 500, GID: 500},
{Type: NodeDirectory{}, Name: path.Join("dir2", "sub22"), UID: 500, GID: 500},
{Type: nil},
}
for _, e := range expected {
v, err := d.Next()
if err != nil {
t.Fatal(err)
}
if reflect.TypeOf(e.Type) != reflect.TypeOf(v) {
t.Fatalf("expected %s, got %s", reflect.TypeOf(e.Type), reflect.TypeOf(v))
}
if e.Type == nil {
break
}
switch val := v.(type) {
case NodeDirectory:
if val.Name != e.Name {
t.Fatalf("expected name '%s', got '%s'", e.Name, val.Name)
}
if val.UID != e.UID {
t.Fatalf("expected uid '%d', got '%d'", e.UID, val.UID)
}
case NodeFile:
if val.Name != e.Name {
t.Fatalf("expected name '%s', got '%s'", e.Name, val.Name)
}
if val.UID != e.UID {
t.Fatalf("expected uid '%d', got '%d'", e.UID, val.UID)
}
}
}
}