-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
265 lines (203 loc) · 6.4 KB
/
test.js
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
var fs = require('fs');
var path = require('path');
var testing = require('testing');
var mkdirp = require('mkdirp');
var pdfmerger = require(`./distribution/index.js`);
var trash = require('trash');
var resDir = 'res' + path.sep;
var outDir = resDir + 'out' + path.sep;
var singleDir = resDir + 'single' + path.sep;
var pdf1 = resDir + 'pdf1.pdf';
var pdf2 = resDir + 'pdf2.pdf';
// Should work
// Array of file paths as input, writing to file using fs.
function filesAndStream(callback) {
var fileCombined = outDir + 'filesAndStream.pdf';
var writeStream = fs.createWriteStream(fileCombined);
var pdfStream = pdfmerger([pdf1, pdf2]);
pdfStream.on('error', function(error) {
testing.failure(error.message, callback);
});
writeStream.on('error', function(error) {
testing.failure('Could not write file', callback);
});
writeStream.on('finish', function() {
fs.readFile(fileCombined, function(error, result) {
if (error) {
testing.failure('File not read', callback);
}
testing.assert(result, 'Empty file', callback);
testing.success(callback);
});
});
pdfStream.pipe(writeStream);
}
function maxAndMinHeap(callback) {
var fileCombined = outDir + 'maxAndMinHeap.pdf';
var writeStream = fs.createWriteStream(fileCombined);
var pdfStream = pdfmerger([pdf1, pdf2], null, {
maxHeap: 512,
minHeap: 32
});
pdfStream.on('error', function(error) {
testing.failure(error.message, callback);
});
writeStream.on('error', function(error) {
testing.failure('Could not write file', callback);
});
writeStream.on('finish', function() {
fs.readFile(fileCombined, function(error, result) {
if (error) {
testing.failure('File not read', callback);
}
testing.assert(result, 'Empty file', callback);
testing.success(callback);
});
});
pdfStream.pipe(writeStream);
}
// Directory path as input, writing to file using fs.
function dirAndStream(callback) {
var fileCombined = outDir + 'dirAndStream.pdf';
var writeStream = fs.createWriteStream(fileCombined);
var pdfStream = pdfmerger(resDir);
pdfStream.on('error', function(error) {
testing.failure(error.message, callback);
});
writeStream.on('error', function(error) {
if (error) {
testing.failure('Could not write file', callback);
}
});
writeStream.on('finish', function() {
fs.readFile(fileCombined, function(error, result) {
if (error) {
testing.failure('File not read', callback);
}
testing.assert(result, 'Empty file', callback);
testing.success(callback);
});
});
pdfStream.pipe(writeStream);
}
// Files and destination path as input
function filesAndDestPath(callback) {
var fileCombined = outDir + 'filesAndDestPath.pdf';
pdfmerger([pdf1, pdf2], fileCombined, {}, function(error) {
if (error) {
return testing.failure(error.message, callback);
}
fs.readFile(fileCombined, function(error, result) {
if (error) {
testing.failure('File not read', callback);
}
testing.assert(result, 'Empty file', callback);
testing.success(callback);
});
});
}
// directory and destination path as input
function dirAndDestPath(callback) {
var fileCombined = outDir + 'dirAndDestPath.pdf';
pdfmerger(resDir, fileCombined, {}, function(error) {
if (error) {
return testing.failure(error.message, callback);
}
fs.readFile(fileCombined, function(error, result) {
if (error) {
testing.failure('File not read', callback);
}
testing.assert(result, 'Empty file', callback);
testing.success(callback);
});
});
}
// -- Should fail ----------------------------------------------------------------------------------
// file not found
function fileNotFoundStream(callback) {
var pdfStream = pdfmerger([pdf1, 'abcdefghijklmnopqrstuvwxyz0123456789.pdf']);
pdfStream.on('error', function(error) {
testing.contains(error, 'Error: File not found or accessible', callback);
testing.success(callback);
});
}
function fileNotFound(callback) {
var fileCombined = outDir + 'fileNotFound.pdf';
pdfmerger([pdf1, 'abcdefghijklmnopqrstuvwxyz0123456789.pdf'], fileCombined, {}, function(error) {
testing.contains(error.message, 'Error: File not found or accessible', callback);
testing.success(callback);
});
}
// not enough input files
function invalidAmountOfInputFiles(callback) {
var fileCombined = outDir + 'fileNotFound.pdf';
try {
pdfmerger([pdf1]);
} catch(error) {
testing.contains(error.message, 'must be at least 2 paths in the source array', callback);
testing.success(callback);
}
}
// not a valid input directory
function dirNotFound(callback) {
var pdfStream = pdfmerger('abcdefghijklmnopqrstuvwxyz0123456789');
pdfStream.on('error', function(error) {
testing.contains(error, 'The provided directory path is not a directory', callback);
testing.success(callback);
});
}
function dirIsFile(callback) {
var pdfStream = pdfmerger(pdf1);
pdfStream.on('error', function(error) {
testing.contains(error, 'The provided directory path is not a directory', callback);
testing.success(callback);
});
}
// not a valid output directory
function outputDirNotFound(callback) {
pdfmerger([pdf1, pdf2], '/abcdefghijklmnopqrstuvwxyz0123456789/test.pdf', {}, function(error) {
testing.contains(error.message, 'Output file path not accessible', callback);
testing.success(callback);
});
}
// not sufficient amount of pdfs in input directory
function dirIsEmpty(callback) {
var pdfStream = pdfmerger(singleDir);
pdfStream.on('error', function(error) {
testing.contains(error, 'Missing sources', callback);
testing.success(callback);
});
}
exports.test = function(callback) {
var tests = [
// Should pass
filesAndStream,
dirAndStream,
filesAndDestPath,
dirAndDestPath,
maxAndMinHeap,
// Should fail
fileNotFoundStream,
fileNotFound,
invalidAmountOfInputFiles,
dirNotFound,
dirIsFile,
outputDirNotFound,
dirIsEmpty
];
testing.run(tests, 30000, callback);
};
// run tests if invoked directly
if (__filename == process.argv[1]) {
function cleanupAndShow(err, res) {
trash([outDir + '*']).then(() => {
testing.show(err, res);
});
}
mkdirp(outDir, function (err) {
if (err) {
throw err;
}
exports.test(cleanupAndShow);
});
}