-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
_index.js
286 lines (222 loc) · 10.8 KB
/
_index.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict';
const fs = require('fs'),
yaml = require('js-yaml'),
svgo = require(process.env.COVERAGE ?
'../../lib-cov/svgo/coa.js' :
'../../lib/svgo/coa.js').api,
defaults = Object.assign({}, yaml.safeLoad(fs.readFileSync(__dirname + '/../../.svgo.yml', 'utf8'))),
path = require('path'),
svgPath = path.resolve(__dirname, 'test.svg'),
svgFolderPath = path.resolve(__dirname, 'testSvg'),
prefixIdsFolderPath = path.resolve(__dirname, 'testPrefixIds'),
prefixIdsSvgInPath = path.resolve(prefixIdsFolderPath, 'in.svg'),
mpDirPath = path.resolve(__dirname, 'testMultipass'),
mpSvgInPath = path.resolve(mpDirPath, 'in.svg'),
mpSvgExpPath = path.resolve(mpDirPath, 'out.svg'),
mpSvgExp = fs.readFileSync(mpSvgExpPath, 'utf8'),
svgFolderPathRecursively = path.resolve(__dirname, 'testSvgRecursively'),
svgFiles = [path.resolve(__dirname, 'testSvg/test.svg'), path.resolve(__dirname, 'testSvg/test.1.svg')],
tempFolder = 'temp',
fse = require('fs-extra'),
checkIsDir = require('../../lib/svgo/tools.js').checkIsDir,
noop = () => {};
describe('coa', function() {
let output;
beforeEach(function() {
output = '';
fse.emptyDirSync(tempFolder);
});
after(function() {
fse.removeSync(tempFolder);
});
const initialConsoleLog = global.console.log;
function replaceConsoleLog() {
global.console.log = message => { output += message };
}
function restoreConsoleLog() {
global.console.log = initialConsoleLog;
}
const initialConsoleError = global.console.error;
const initialProcessExit = global.process.exit;
function replaceConsoleError() {
global.console.error = message => { output += message };
global.process.exit = noop;
}
function restoreConsoleError() {
global.console.error = initialConsoleError;
global.process.exit = initialProcessExit;
}
function calcFolderSvgWeight(folderPath) {
return fs.readdirSync(folderPath).reduce((initWeight, name) => (
initWeight +
(/.svg/.test(name) ? fs.statSync(path.join(folderPath, name)).size : 0) +
(checkIsDir(path.join(folderPath, name)) ? calcFolderSvgWeight(path.join(folderPath, name)) : 0)
), 0);
}
it('should throw an error if "config" can not be parsed', function(done) {
replaceConsoleError();
svgo({ input: svgPath, config: '{' }).then(onComplete, onComplete);
function onComplete() {
restoreConsoleError();
done(/Error: Couldn't parse config JSON/.test(output) ? null : 'Error was not thrown');
}
});
it('should work properly with string input', function(done) {
svgo({ string: fs.readFileSync(svgPath, 'utf8'), output: 'temp.svg', quiet: true }).then(function() {
done();
fse.removeSync('temp.svg');
}, error => done(error));
});
it('should optimize folder', function(done) {
const initWeight = calcFolderSvgWeight(svgFolderPath);
svgo({ folder: svgFolderPath, output: tempFolder, quiet: true }).then(function() {
const optimizedWeight = calcFolderSvgWeight(svgFolderPath);
done(optimizedWeight > 0 && initWeight <= optimizedWeight ? null : 'Folder was not optimized');
}, error => done(error));
});
it('should optimize folder recursively', function(done) {
const initWeight = calcFolderSvgWeight(svgFolderPathRecursively);
svgo({ folder: svgFolderPathRecursively, output: tempFolder, quiet: true, recursive: true }).then(function() {
const optimizedWeight = calcFolderSvgWeight(svgFolderPathRecursively);
done(optimizedWeight > 0 && initWeight <= optimizedWeight ? null : 'Folder was not optimized');
}, error => done(error));
});
it('should optimize file', function(done) {
const initialFileLength = fs.readFileSync(path.resolve(__dirname, 'test.svg')).length;
svgo({ input: svgPath, output: 'temp.svg', quiet: true }).then(function() {
const optimizedFileLength = fs.readFileSync('temp.svg').length;
done(optimizedFileLength <= initialFileLength ? null : 'File was not optimized');
fse.removeSync('temp.svg');
}, error => done(error));
});
it('should optimize several files', function(done) {
const initWeight = calcFolderSvgWeight(svgFolderPath);
svgo({ input: svgFiles, output: tempFolder, quiet: true }).then(function() {
const optimizedWeight = calcFolderSvgWeight(tempFolder);
done(optimizedWeight > 0 && optimizedWeight <= initWeight ? null : 'Files were not optimized');
fse.removeSync('temp.svg');
}, error => done(error));
});
it('should optimize file from process.stdin', function(done) {
const initialFile = fs.readFileSync(path.resolve(__dirname, 'test.svg'));
const stdin = require('mock-stdin').stdin();
setTimeout(() => { stdin.send(initialFile, 'ascii').end(); }, 0);
svgo({ input: '-', output: 'temp.svg', string: fs.readFileSync(svgPath, 'utf8'), quiet: true })
.then(onComplete, onComplete);
function onComplete() {
const optimizedFileLength = fs.readFileSync('temp.svg').length;
done(optimizedFileLength <= initialFile.length ? null : 'Files were not optimized');
fse.removeSync('temp.svg');
}
});
it('should optimize folder, when it stated in input', function(done) {
const initWeight = calcFolderSvgWeight(svgFolderPath);
svgo({ input: svgFolderPath, output: tempFolder, quiet: true }).then(function() {
let optimizedWeight = calcFolderSvgWeight(svgFolderPath);
done(optimizedWeight <= initWeight ? null : 'Files were not optimized');
}, error => done(error));
});
it('should throw error when stated in input folder does not exist', function(done) {
replaceConsoleError();
svgo({ input: svgFolderPath + 'temp', output: tempFolder }).then(onComplete, onComplete);
function onComplete(err) {
restoreConsoleError();
done(/no such file or directory/.test(err) ? null : 'Error was not thrown');
}
});
it('should pass the filename to the prefixIds plugin', function(done) {
svgo({
input: prefixIdsSvgInPath,
output: 'temp.svg',
quiet: true,
multipass: false,
disable: defaults.plugins, // disable all plugins except ...
enable: [ 'prefixIds' ] // ... prefixIds
}).then(function() {
const svgOut = fs.readFileSync('temp.svg', 'utf8');
done(/in_svg__/.test(svgOut) ? null : 'filename isn\'t passed to prefixIds plugin.');
fse.removeSync('temp.svg');
}, error => done(error));
});
describe('multipass', function() {
it('should optimize using multiple passes with multipass enabled', function(done) {
svgo({
input: mpSvgInPath,
output: 'temp.svg',
quiet: true,
multipass: true
}).then(function() {
const mpSvgOut = fs.readFileSync('temp.svg', 'utf8');
done(mpSvgOut === mpSvgExp ? null : 'Multipass wasn\'t properly used.');
fse.removeSync('temp.svg');
}, error => done(error));
});
it('should allow prefixId plugin to detect subsequent passes with multipass enabled', function(done) {
svgo({
input: mpSvgInPath,
output: 'temp.svg',
quiet: true,
multipass: true,
disable: defaults.plugins, // disable all plugins except ...
enable: [ 'prefixIds' ] // ... prefixIds
}).then(function() {
const mpSvgOut = fs.readFileSync('temp.svg', 'utf8');
done(!/in_svg__in_svg__/.test(mpSvgOut) ? null : 'prefixIds plugin doesn\'t detect subsequent passes with multipass enabled.');
// https://github.com/svg/svgo/issues/659
// https://github.com/svg/svgo/issues/1133
fse.removeSync('temp.svg');
}, error => done(error));
});
it('should allow addAttributesToSVGElement plugin to correctly handle subsequent passes with multipass enabled', function(done) {
svgo({
input: mpSvgInPath,
output: 'temp.svg',
quiet: true,
multipass: true,
config: `{
"plugins": [{ "addAttributesToSVGElement": {
"attribute": "aria-hidden=\\"true\\""
} }]
}`
}).then(function() {
const mpSvgOut = fs.readFileSync('temp.svg', 'utf8');
done(!/aria-hidden="true" aria-hidden='true'/.test(mpSvgOut) ? null : 'addAttributesToSVGElement plugin doesn\'t correctly handle subsequent passes with multipass enabled.');
// https://github.com/svg/svgo/issues/659
// https://github.com/svg/svgo/issues/1133
fse.removeSync('temp.svg');
}, error => done(error));
});
});
describe('stdout', function() {
it('should show file content when no output set', function(done) {
replaceConsoleLog();
svgo({ string: fs.readFileSync(svgPath, 'utf8'), output: '-', datauri: 'unenc' }).then(onComplete, onComplete);
function onComplete() {
restoreConsoleLog();
done(/www.w3.org\/2000\/svg/.test(output) ? null : 'File content was not shown');
}
});
it('should show message when the folder is empty', function(done) {
const emptyFolderPath = path.resolve(__dirname, 'testSvgEmpty');
if (!fs.existsSync(emptyFolderPath)) {
fs.mkdirSync(emptyFolderPath);
}
replaceConsoleError();
svgo({ folder: emptyFolderPath, quiet: true }).then(onComplete, onComplete);
function onComplete() {
restoreConsoleError();
done(/No SVG files/.test(output) ? null : 'Empty folder message was not shown');
}
});
it('should show message when folder does not consists any svg files', function(done) {
replaceConsoleError();
svgo({ folder: path.resolve(__dirname, 'testFolderWithNoSvg'), quiet: true }).then(onComplete, onComplete);
function onComplete() {
restoreConsoleError();
done(/No SVG files have been found/.test(output) ?
null :
'Error "No SVG files have been found" was not shown');
}
});
});
});