-
Notifications
You must be signed in to change notification settings - Fork 32
/
test-sharp.js
238 lines (213 loc) · 6.77 KB
/
test-sharp.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
const uploadfs = require('./uploadfs.js')();
const fs = require('fs');
const async = require('async');
const Promise = require('bluebird');
const _ = require('lodash');
const path = require('path');
// Test the imagecrunch image backend, written specifically for Macs
const localOptions = {
storage: 'local',
image: 'sharp',
uploadsPath: path.join(__dirname, '/test'),
uploadsUrl: 'http://localhost:3000/test'
};
const imageSizes = [
{
name: 'small',
width: 320,
height: 320
},
{
name: 'medium',
width: 640,
height: 640
},
{
name: 'large',
width: 1140,
height: 1140
}
];
const tempPath = path.join(__dirname, '/temp');
const basePath = '/images/profiles/me';
localOptions.imageSizes = imageSizes;
localOptions.tempPath = tempPath;
localOptions.backend = 'local';
localTestStart(function () {
let filesSeen = false;
console.log('RERUN TESTS WITH TEST OF POSTPROCESSORS');
localOptions.postprocessors = [
{
postprocessor: function(files, folder, options) {
console.log('in a postprocessor');
if (!(options && options.test)) {
console.error('postprocessor did not receive options');
process.exit(1);
}
if (!files) {
console.error('No files array passed to postprocessor');
process.exit(1);
}
if (!files.length) {
return Promise.resolve(true);
}
if (!files[0].match(/\.(gif|jpg|png)$/)) {
console.error('postprocessor invoked for inappropriate file extension');
process.exit(1);
}
if (!fs.existsSync(files[0])) {
console.error('postprocessor invoked for nonexistent file');
process.exit(1);
}
if (require('path').dirname(files[0]) !== folder) {
console.error('folder parameter to postprocessor is incorrect');
}
_.each(localOptions.imageSizes, function(size) {
if (!_.find(files, function(file) {
return file.match(size.name);
})) {
console.error('postprocessor saw no file for the size ' + size.name);
process.exit(1);
}
});
filesSeen = true;
return Promise.resolve(true);
},
extensions: [ 'gif', 'jpg', 'png' ],
options: {
test: true
}
}
];
localTestStart(function () {
if (!filesSeen) {
console.error('postprocessor saw no files');
process.exit(1);
}
console.log('Tests, done');
process.exit(0);
});
});
function localTestStart(cb) {
const options = localOptions;
console.log('Initializing uploadfs for the ' + options.backend + ' storage backend with the imagecrunch image backend');
uploadfs.init(options, function(e) {
if (e) {
console.log('uploadfs.init failed:');
console.log(e);
process.exit(1);
}
console.log('uploadfs.init', this.options);
testCopyImageIn();
});
function testCopyImageIn() {
console.log('testing copyImageIn');
// Note copyImageIn adds an extension for us
uploadfs.copyImageIn('test.jpg', basePath, function(e, info) {
if (e) {
console.log('testCopyImageIn failed:');
console.log(e);
process.exit(1);
}
if (info.basePath !== '/images/profiles/me') {
console.log('info.basePath is incorrect');
process.exit(1);
}
console.log('Testing that returned image dimensions are reoriented');
if ((info.width !== 1936) || (info.height !== 2592)) {
console.log('Width and height missing or not reoriented for web use');
console.log(info);
process.exit(1);
}
if ((info.originalWidth !== 2592) || (info.originalHeight !== 1936)) {
console.log('Original width and height missing or incorrect');
console.log(info);
process.exit(1);
}
const stats = fs.statSync('test/images/profiles/me.jpg');
if (!stats.size) {
console.log('Copied image is empty or missing');
process.exit(1);
}
// We already tested remove, just do it to mop up
console.log('Removing files...');
uploadfs.remove('/images/profiles/me.jpg', function(e) {
async.each(imageSizes, function(size, callback) {
const name = info.basePath + '.' + size.name + '.jpg';
const stats = fs.statSync('test' + name);
if (!stats.size) {
console.log('Scaled and copied image is empty or missing (2)');
process.exit(1);
}
// We already tested remove, just do it to mop up
uploadfs.remove(info.basePath + '.' + size.name + '.jpg', function(e) {
callback();
});
}, function(err) {
if (err) {
console.log('Test failed', err);
process.exit(1);
}
testCopyImageInCrop(cb);
});
}); // remove me.jpg
});
}
function testCopyImageInCrop(cb) {
console.log('testing copyImageIn with cropping');
// Note copyImageIn adds an extension for us
// Should grab the flowers
uploadfs.copyImageIn('test.jpg', '/images/profiles/me-cropped', {
crop: {
top: 830,
left: 890,
width: 500,
height: 500
}
}, function(e, info) {
if (e) {
console.log('testCopyImageIn failed:');
console.log(e);
process.exit(1);
}
if (info.basePath !== '/images/profiles/me-cropped') {
console.log('info.basePath is incorrect');
process.exit(1);
}
console.log('Testing that returned image dimensions are reoriented');
if ((info.width !== 500) || (info.height !== 500)) {
console.log('Reported size does not match crop');
console.log(info);
process.exit(1);
}
const stats = fs.statSync('test/images/profiles/me-cropped.jpg');
if (!stats.size) {
console.log('Copied image is empty or missing');
process.exit(1);
}
// We already tested remove, just do it to mop up
console.log('Removing files...');
uploadfs.remove(`${basePath}-cropped.jpg`, function(e) {
async.each(imageSizes, function(size, callback) {
const name = info.basePath + '.' + size.name + '.jpg';
const stats = fs.statSync('test' + name);
if (!stats.size) {
console.log('Scaled and copied image is empty or missing (2)');
process.exit(1);
}
// We already tested remove, just do it to mop up
uploadfs.remove(info.basePath + '.' + size.name + '.jpg', function(e) {
callback(e);
});
}, function (err) {
if (err) {
console.log('Remove file fails', err);
process.exit(1);
}
console.log('Files removed');
cb();
});
});
});
}
}