generated from LinuxSuRen/github-go
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtypes_test.go
235 lines (218 loc) · 5.17 KB
/
types_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package compress
import (
"archive/tar"
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"os"
"path"
"reflect"
"testing"
)
func TestGetCompressor(t *testing.T) {
type args struct {
extension string
additionBinaries []string
}
tests := []struct {
name string
args args
want Compress
}{{
name: "unknown type",
args: args{extension: ".xdf"},
want: nil,
}, {
name: ".zip",
args: args{extension: ".zip"},
want: NewZip(nil),
}, {
name: ".xz",
args: args{extension: ".xz"},
want: NewXz(nil),
}, {
name: ".tar.gz",
args: args{extension: ".tar.gz"},
want: NewGZip(nil),
}, {
name: ".bz2",
args: args{extension: ".bz2"},
want: NewBzip2(nil),
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetCompressor(tt.args.extension, tt.args.additionBinaries); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetCompressor() = %v, want %v", got, tt.want)
} else if got != nil {
err := got.ExtractFiles("", "")
assert.NotNil(t, err)
// test with a regular file
var f *os.File
if f, err = os.CreateTemp(os.TempDir(), "fake"); err != nil {
return
}
assert.Nil(t, err)
assert.NotNil(t, f)
defer func() {
_ = os.RemoveAll(f.Name())
}()
err = got.ExtractFiles(f.Name(), "fake")
assert.NotNil(t, err)
// try to read a non-exist file
err = got.ExtractFiles(path.Join(os.TempDir(), "fake"), "fake")
assert.NotNil(t, err)
}
})
}
}
func TestIsSupport(t *testing.T) {
type args struct {
extension string
}
tests := []struct {
name string
args args
want bool
}{{
name: "supported extension: .tar.gz",
args: args{
extension: path.Ext("test.tar.gz"),
},
want: true,
}, {
name: "supported extension: .xz",
args: args{
extension: path.Ext("test.xz"),
},
want: true,
}, {
name: "supported extension: .zip",
args: args{
extension: path.Ext("test.zip"),
},
want: true,
}, {
name: "supported extension: .gz",
args: args{
extension: path.Ext("test.gz"),
},
want: true,
}, {
name: "supported extension: .tgz",
args: args{
extension: path.Ext("test.tgz"),
},
want: true,
}, {
name: "supported extension: .bz2",
args: args{
extension: path.Ext("test.bz2"),
},
want: true,
}, {
name: "not supported extension: .ab",
args: args{
extension: path.Ext("test.ab"),
},
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsSupport(tt.args.extension); got != tt.want {
t.Errorf("IsSupport() = %v, want %v", got, tt.want)
}
})
}
}
func Test_extraFile(t *testing.T) {
type args struct {
name string
targetName string
tarFile string
header *tar.Header
tarReader *tar.Reader
}
tests := []struct {
name string
args args
wantErr assert.ErrorAssertionFunc
}{{
name: "invalid name",
args: args{
name: "fake",
targetName: "fake.go",
tarFile: "",
header: nil,
tarReader: nil,
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
assert.Nil(t, err)
return true
},
}, {
name: "normal",
args: args{
name: "fake",
targetName: "fake",
tarFile: "fake",
header: &tar.Header{Mode: 400},
tarReader: tar.NewReader(bytes.NewBufferString("fake")),
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
assert.Nil(t, err)
return true
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var file *os.File
var err error
if tt.args.tarFile != "" {
file, err = os.CreateTemp(os.TempDir(), tt.args.tarFile)
assert.Nil(t, err)
assert.NotNil(t, file)
}
if err != nil || file == nil {
return
}
defer func() {
_ = os.RemoveAll(tt.args.tarFile)
}()
err = extraFile(tt.args.name, tt.args.targetName, tt.args.tarFile, tt.args.header, tt.args.tarReader)
tt.wantErr(t, err, fmt.Sprintf("extraFile(%v, %v, %v, %v, %v)", tt.args.name, tt.args.targetName, tt.args.tarFile, tt.args.header, tt.args.tarReader))
})
}
}
func TestExtractFiles(t *testing.T) {
t.Run("test .tar.gz", func(t *testing.T) {
compressor := GetCompressor(".tar.gz", []string{"bb", "cc"})
assert.NotNil(t, compressor)
err := compressor.ExtractFiles("testdata/simple.tar.gz", "aa")
assert.NoError(t, err)
assertFileContentEqual(t, "testdata/aa", "aa\n")
assertFileContentEqual(t, "testdata/bb", "bb\n")
assertFileContentEqual(t, "testdata/cc", "cc\n")
})
t.Run("test .zip", func(t *testing.T) {
compressor := GetCompressor(".zip", []string{"bb", "cc"})
assert.NotNil(t, compressor)
err := compressor.ExtractFiles("testdata/simple.zip", "aa")
assert.NoError(t, err)
assertFileContentEqual(t, "testdata/aa", "aa\n")
assertFileContentEqual(t, "testdata/bb", "bb\n")
assertFileContentEqual(t, "testdata/cc", "cc\n")
// invalid parameters
err = compressor.ExtractFiles("", "")
assert.Error(t, err)
})
}
func assertFileContentEqual(t *testing.T, filePath string, expectedContent string) {
defer func() {
_ = os.RemoveAll(filePath)
}()
if data, err := os.ReadFile(filePath); err == nil {
assert.Equal(t, expectedContent, string(data))
} else {
t.Fatalf("not found %q: %v", filePath, err)
}
}