forked from golift/xtractr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
170 lines (134 loc) · 4.13 KB
/
queue_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
package xtractr_test
import (
"log"
"os"
"path/filepath"
"testing"
"github.com/gvcgo/xtractr"
"github.com/stretchr/testify/assert"
)
//nolint:gochecknoglobals
var filesInTestArchive = []string{
"doc.go",
"files.go",
"queue.go",
"rar.go",
"start.go",
"zip.go",
}
const (
testFile = "test_data/archive.rar"
testDataSize = int64(20770)
)
type testLogger struct{ t *testing.T }
func (l *testLogger) Debugf(msg string, format ...interface{}) {
l.t.Helper()
msg = "[DEBUG] " + msg
// l.t.Logf(msg, format...)
log.Printf(msg, format...)
}
func (l *testLogger) Printf(msg string, format ...interface{}) {
l.t.Helper()
msg = "[INFO] " + msg
// l.t.Logf(msg, format...)
log.Printf(msg, format...)
}
func TestWithTempFolder(t *testing.T) {
t.Parallel()
queue := xtractr.NewQueue(&xtractr.Config{Logger: &testLogger{t: t}})
defer queue.Stop()
xFile := &xtractr.Xtract{
Name: "SomeItem",
Filter: xtractr.Filter{Path: testSetupTestDir(t)},
TempFolder: true,
DeleteOrig: false,
Password: "some_password",
LogFile: true,
CBChannel: make(chan *xtractr.Response),
}
depth, err := queue.Extract(xFile)
assert.Equal(t, 1, depth, "there should be 1 item queued now")
assert.NoError(t, err, "why is there an error?!")
for resp := range xFile.CBChannel {
assert.NoError(t, resp.Error, "the test archives should extract without any error")
assert.Equal(t, 4, len(resp.Archives), "four directories have archives in them")
if resp.Done {
assert.Equal(t, len(filesInTestArchive)*4+4, len(resp.NewFiles),
"wrong count of files were extracted, log files must be written too!")
assert.Equal(t, testDataSize*4, resp.Size, "wrong amount of data was written")
break
}
}
// test written files here?
// each directory should have its own files.
os.RemoveAll(xFile.Path)
os.RemoveAll(xFile.Path + xtractr.DefaultSuffix)
}
func TestNoTempFolder(t *testing.T) {
t.Parallel()
queue := xtractr.NewQueue(&xtractr.Config{Logger: &testLogger{t: t}})
defer queue.Stop()
xFile := &xtractr.Xtract{
Name: "SomeItem",
Filter: xtractr.Filter{Path: testSetupTestDir(t)},
TempFolder: false,
DeleteOrig: true,
Password: "some_password",
LogFile: false,
CBChannel: make(chan *xtractr.Response),
}
depth, err := queue.Extract(xFile)
assert.Equal(t, 1, depth, "there should be 1 item queued now")
assert.NoError(t, err, "why is there an error?!")
for resp := range xFile.CBChannel {
assert.NoError(t, resp.Error, "the test archives should extract without any error")
assert.Equal(t, 4, len(resp.Archives), "four directories have archives in them")
if resp.Done {
assert.Equal(t, len(filesInTestArchive)*4, len(resp.NewFiles), "wrong count of files were extracted")
assert.Equal(t, testDataSize*4, resp.Size, "wrong amount of data was written")
break
}
}
// test written files here?
// each directory should have its own files.
os.RemoveAll(xFile.Path)
os.RemoveAll(xFile.Path + xtractr.DefaultSuffix)
}
// testSetupTestDir creates a temp directory with 4 copies of a rar archive in it.
func testSetupTestDir(t *testing.T) string {
t.Helper()
name, err := os.MkdirTemp(".", "xtractr_test_*_data")
if err != nil {
t.Fatalf("could not make temporary directory: %v", err)
}
testFileData, err := os.ReadFile(testFile)
if err != nil {
t.Fatalf("could not read test data file: %v", err)
}
for _, sub := range []string{"subDir1", "subDir2", "subDir3"} {
err = os.MkdirAll(filepath.Join(name, "subDirectory", sub), xtractr.DefaultDirMode)
if err != nil {
t.Fatalf("could not make temporary directory: %v", err)
}
fileName := filepath.Join(name, "subDirectory", sub, sub+"_archive.rar")
err := makeFile(testFileData, fileName)
if err != nil {
t.Fatalf("creating test archive: %v", err)
}
}
err = makeFile(testFileData, filepath.Join(name, "subDirectory", "primary_arechive.rar"))
if err != nil {
t.Fatalf("creating test archive: %v", err)
}
return name
}
//nolint:wrapcheck
func makeFile(data []byte, fileName string) error {
openFile, err := os.Create(fileName)
if err != nil {
return err
}
defer openFile.Close()
_, err = openFile.Write(data)
return err
}