forked from folbricht/desync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar_test.go
100 lines (90 loc) · 1.92 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
93
94
95
96
97
98
99
100
// +build !windows
package desync
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)
func TestTar(t *testing.T) {
// First make a tempdir and create a few dirs and files in it
base, err := ioutil.TempDir("", "desync-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(base)
dirs := []string{
"dir1/sub11",
"dir1/sub12",
"dir2/sub21",
"dir2/sub22",
}
for _, d := range dirs {
if err = os.MkdirAll(filepath.Join(base, d), 0755); err != nil {
t.Fatal()
}
}
files := []string{
"dir1/sub11/f11",
"dir1/sub11/f12",
}
for i, name := range files {
ioutil.WriteFile(filepath.Join(base, name), []byte(fmt.Sprintf("filecontent%d", i)), 0644)
}
if err = os.Symlink("dir1", filepath.Join(base, "symlink")); err != nil {
t.Fatal(err)
}
// Encode it all into a buffer
b := new(bytes.Buffer)
if err = Tar(context.Background(), b, base, false); err != nil {
t.Fatal(err)
}
// Decode it again
d := NewFormatDecoder(b)
// Define an array of what is expected in the test file
expected := []interface{}{
FormatEntry{},
FormatFilename{}, // "dir1"
FormatEntry{},
FormatFilename{}, // "sub11"
FormatEntry{},
FormatFilename{}, // "f11"
FormatEntry{},
FormatPayload{},
FormatFilename{}, // "f12"
FormatEntry{},
FormatPayload{},
FormatGoodbye{},
FormatFilename{}, // "sub12"
FormatEntry{},
FormatGoodbye{},
FormatGoodbye{},
FormatFilename{}, // "dir2"
FormatEntry{},
FormatFilename{}, // "sub21"
FormatEntry{},
FormatGoodbye{},
FormatFilename{}, // "sub22"
FormatEntry{},
FormatGoodbye{},
FormatGoodbye{},
FormatFilename{}, // "symlink"
FormatEntry{},
FormatSymlink{},
FormatGoodbye{},
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))
}
}
}