-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder_test.go
91 lines (80 loc) · 1.57 KB
/
decoder_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
package asar // import "github.com/yyq2013/asar"
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestExtractThis(t *testing.T) {
f, err := os.Open("testdata/extractthis.asar")
if err != nil {
t.Fatal(err)
}
defer f.Close()
root, err := Decode(f)
if err != nil {
t.Fatal(err)
}
if root.Flags&FlagDir == 0 {
t.Fatal("expecting root directory to have FlagDir")
}
{
f1 := root.Find("dir1", "file1.txt")
if f1 == nil {
t.Fatal("could not find dir1/file1.txt")
}
if f1.Path() != "dir1/file1.txt" {
t.Fatal("unexpected path")
}
body, err := ioutil.ReadAll(f1.Open())
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(body, []byte(`file one.`)) {
t.Fatalf("dir1/file1.txt body is incorrect (got %s)", body)
}
}
{
f2 := root.Find("dir2").Find("file3.txt")
if f2 == nil {
t.Fatal("could not find dir2/file3.txt")
}
s := f2.String()
if s != `123` {
t.Fatalf("dir2/file3.txt body is incorrect (got %s)", s)
}
}
{
empty := root.Find("emptyfile.txt")
if empty == nil {
t.Fatal("could not find emptyfile.txt")
}
if len(empty.Bytes()) != 0 {
t.Fatal("expecting emptyfile.txt to be empty")
}
}
{
var i int
root.Walk(func(_ string, _ os.FileInfo, _ error) error {
i++
return nil
})
if i != 7 {
t.Fatalf("expected to walk over 7 items, got %d", i)
}
}
{
var i int
root.Walk(func(_ string, fi os.FileInfo, _ error) error {
i++
if fi.IsDir() {
return filepath.SkipDir
}
return nil
})
if i != 4 {
t.Fatalf("expected to walk over 4 items, got %d", i)
}
}
}