-
Notifications
You must be signed in to change notification settings - Fork 11
/
queue_test.go
161 lines (125 loc) · 4.08 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
package xtractr_test
import (
"log"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golift.io/xtractr"
)
//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)
require.NoError(t, err, "why is there an error?!")
assert.Equal(t, 1, depth, "there should be 1 item queued now")
for resp := range xFile.CBChannel {
require.NoError(t, resp.Error, "the test archives should extract without any error")
assert.Len(t, resp.Archives, 4, "four directories have archives in them")
if resp.Done {
assert.Len(t, resp.NewFiles, len(filesInTestArchive)*4+4,
"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)
require.NoError(t, err, "why is there an error?!")
assert.Equal(t, 1, depth, "there should be 1 item queued now")
for resp := range xFile.CBChannel {
require.NoError(t, resp.Error, "the test archives should extract without any error")
assert.Len(t, resp.Archives, 4, "four directories have archives in them")
if resp.Done {
assert.Len(t, resp.NewFiles, len(filesInTestArchive)*4, "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")
require.NoError(t, err, "creating temp directory failed")
testFileData, err := os.ReadFile(testFile)
require.NoError(t, err, "reading test data file failed")
for _, sub := range []string{"subDir1", "subDir2", "subDir3"} {
err = os.MkdirAll(filepath.Join(name, "subDirectory", sub), xtractr.DefaultDirMode)
require.NoError(t, err, "creating temp directory failed")
fileName := filepath.Join(name, "subDirectory", sub, sub+"_archive.rar")
require.NoError(t, makeFile(t, testFileData, fileName), "creating test archive failed")
}
err = makeFile(t, testFileData, filepath.Join(name, "subDirectory", "primary_arechive.rar"))
require.NoError(t, err, "creating test archive failed")
return name
}
//nolint:wrapcheck
func makeFile(t *testing.T, data []byte, fileName string) error {
t.Helper()
openFile, err := os.Create(fileName)
if err != nil {
return err
}
defer openFile.Close()
_, err = openFile.Write(data)
return err
}