-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
96 lines (79 loc) · 3.61 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
const svgo = require('.');
const ava = require('ava');
const File = require('vinyl');
const svg = {
head: '<?xml version="1.0" encoding="UTF-8" standalone="no"?>',
doctype: '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">',
body: '<svg width="100%" height="100%" viewBox="0 0 42 42" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;"> <rect id="some-id" x="0" y="0" width="42" height="42"/> </svg>',
malformed: '<svg width="100%" height="100%" viewBox="0 0 42 42" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;"> <rect id="some-id" x="0" y="0" width="42" height="42"/>'
};
const malformed = `${svg.head} ${svg.doctype} ${svg.malformed}`;
const src = `${svg.head} ${svg.doctype} <!--comment--> ${svg.body}`;
const expected = '<svg viewBox="0 0 42 42" xmlns="http://www.w3.org/2000/svg" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414"><path d="M0 0h42v42H0z"/></svg>';
const expectedWithPrefix = '<svg viewBox="0 0 42 42" xmlns="http://www.w3.org/2000/svg" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414"><path id="some_svg__some-id" d="M0 0h42v42H0z"/></svg>';
function test(msg, stream, file, assertion) {
return ava(msg, t => new Promise(resolve => {
const output = [];
const stderr = process.stderr.write;
const fallback = setTimeout(() => {
process.stderr.write = stderr;
resolve(assertion(t, null, file, output));
}, 32);
stream.on('data', data => {
process.stderr.write = stderr;
clearTimeout(fallback);
resolve(assertion(t, data, file, output));
});
stream.on('error', error => {
process.stderr.write = stderr;
clearTimeout(fallback);
resolve(assertion(t, error, file, output));
});
process.stderr.write = (str, ...args) => {
output.push(str);
stderr.apply(process.stderr, [ str, ...args ]);
};
stream.write(file);
}));
}
test('passes through non-svg files unaltered', svgo(), new File({
path: 'some.jpg',
contents: Buffer.from([])
}), (t, data, file) => {
t.is(data.contents.toString(), file.contents.toString());
});
test('minifies svg', svgo(), new File({
path: 'some.svg',
contents: Buffer.from(src)
}), (t, data, file) => {
t.is(data.contents.toString(), expected);
});
test('handles error for malformed svg', svgo(), new File({
path: `${__dirname}/malformed.svg`,
contents: Buffer.from(malformed)
}), (t, data, file, output) => {
const message = `[33mgulp-svgo:[31m Error in parsing SVG: Unclosed root tag\n\t[0mFile: malformed.svg\n\tLine: 0\n\tColumn: 468\n\tChar:\n`;
const [ error ] = output;
t.is(error, message);
});
test('handles svgo options', svgo({
plugins: [
{ removeDoctype: false }
]
}), new File({
path: 'some.svg',
contents: Buffer.from(src)
}), (t, data, file) => {
t.true(data.contents.toString().includes(svg.doctype));
});
test('passes path for prefixing', svgo({
plugins: [
{ prefixIds: true },
{ cleanupIDs: false }
]
}), new File({
path: 'some.svg',
contents: Buffer.from(src)
}), (t, data, file) => {
t.is(data.contents.toString(), expectedWithPrefix);
});