-
Notifications
You must be signed in to change notification settings - Fork 8
/
unpack_test.go
109 lines (102 loc) · 2.21 KB
/
unpack_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
105
106
107
108
109
package crx3
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestUnpack(t *testing.T) {
basePath, err := os.MkdirTemp("", "unpacktest")
require.NoError(t, err)
defer os.RemoveAll(basePath)
dst := filepath.Join(basePath, "dodyDol.crx")
src := "./testdata/dodyDol.crx"
_, err = CopyFile(src, dst)
require.NoError(t, err)
type args struct {
filename string
}
tests := []struct {
name string
args args
assert func()
wantErr bool
}{
{
name: "should return error when filename is empty",
wantErr: true,
},
{
name: "should return error when file is not in crx format",
args: args{
filename: "./testdata/bobbyMol.zip",
},
wantErr: true,
},
{
name: "should not return error when all params are valid",
args: args{
filename: dst,
},
assert: func() {
expected := filepath.Join(basePath, "dodyDol")
require.True(t, isDir(expected))
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Unpack(tt.args.filename); (err != nil) != tt.wantErr {
t.Errorf("Unpack() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.assert != nil {
tt.assert()
}
})
}
}
func TestUnpackTo(t *testing.T) {
basePath, err := os.MkdirTemp("", "unpacktotest")
require.NoError(t, err)
defer os.RemoveAll(basePath)
type args struct {
filename string
dirname string
}
tests := []struct {
name string
args args
assert func()
wantErr bool
}{
{
name: "should return error when dir does not exists and create failed",
args: args{
filename: "./testdata/dodyDol.crx",
dirname: "/not/not/not",
},
wantErr: true,
},
{
name: "should not return error when all params are valid",
args: args{
filename: "./testdata/dodyDol.crx",
dirname: basePath,
},
assert: func() {
expected := filepath.Join(basePath, "dodyDol")
require.True(t, isDir(expected))
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UnpackTo(tt.args.filename, tt.args.dirname); (err != nil) != tt.wantErr {
t.Errorf("UnpackTo() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.assert != nil {
tt.assert()
}
})
}
}