forked from ipfs/go-ipfs-files
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilewriter_test.go
77 lines (72 loc) · 1.46 KB
/
filewriter_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
package files
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestWriteTo(t *testing.T) {
sf := NewMapDirectory(map[string]Node{
"1": NewBytesFile([]byte("Some text!\n")),
"2": NewBytesFile([]byte("beep")),
"3": NewMapDirectory(nil),
"4": NewBytesFile([]byte("boop")),
"5": NewMapDirectory(map[string]Node{
"a": NewBytesFile([]byte("foobar")),
}),
})
tmppath, err := ioutil.TempDir("", "files-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmppath)
path := tmppath + "/output"
err = WriteTo(sf, path)
if err != nil {
t.Fatal(err)
}
expected := map[string]string{
".": "",
"1": "Some text!\n",
"2": "beep",
"3": "",
"4": "boop",
"5": "",
"5/a": "foobar",
}
err = filepath.Walk(path, func(cpath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
rpath, err := filepath.Rel(path, cpath)
if err != nil {
return err
}
data, ok := expected[rpath]
if !ok {
return fmt.Errorf("expected something at %q", rpath)
}
delete(expected, rpath)
if info.IsDir() {
if data != "" {
return fmt.Errorf("expected a directory at %q", rpath)
}
} else {
actual, err := ioutil.ReadFile(cpath)
if err != nil {
return err
}
if string(actual) != data {
return fmt.Errorf("expected %q, got %q", data, string(actual))
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
if len(expected) > 0 {
t.Fatalf("failed to find: %#v", expected)
}
}