forked from imagemin/imagemin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
147 lines (119 loc) · 4.36 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
const {promisify} = require('util');
const fs = require('fs');
const path = require('path');
const del = require('del');
const imageminJpegtran = require('imagemin-jpegtran');
const imageminWebp = require('imagemin-webp');
const imageminSvgo = require('imagemin-svgo');
const isJpg = require('is-jpg');
const makeDir = require('make-dir');
const tempy = require('tempy');
const test = require('ava');
const imagemin = require('.');
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
test('optimize a file', async t => {
const buffer = await readFile(path.join(__dirname, 'fixture.jpg'));
const files = await imagemin(['fixture.jpg'], {
plugins: [imageminJpegtran()]
});
t.is(files[0].destinationPath, undefined);
t.true(files[0].data.length < buffer.length);
t.true(isJpg(files[0].data));
});
test('optimize a buffer', async t => {
const buffer = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await imagemin.buffer(buffer, {
plugins: [imageminJpegtran()]
});
t.true(data.length < buffer.length);
t.true(isJpg(data));
});
test('output error on corrupt images', async t => {
await t.throwsAsync(imagemin(['fixture-corrupt.jpg'], {
plugins: [imageminJpegtran()]
}), {message: /Corrupt JPEG data/});
});
test('throw on wrong input', async t => {
await t.throwsAsync(imagemin('foo'), {message: /Expected an `Array`, got `string`/});
await t.throwsAsync(imagemin.buffer('foo'), {message: /Expected a `Buffer`, got `string`/});
});
test('return original file if no plugins are defined', async t => {
const buffer = await readFile(path.join(__dirname, 'fixture.jpg'));
const files = await imagemin(['fixture.jpg']);
t.is(files[0].destinationPath, undefined);
t.deepEqual(files[0].data, buffer);
t.true(isJpg(files[0].data));
});
test('return original buffer if no plugins are defined', async t => {
const buffer = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await imagemin.buffer(buffer);
t.deepEqual(data, buffer);
t.true(isJpg(data));
});
test('return processed buffer even it is a bad optimization', async t => {
const buffer = await readFile(path.join(__dirname, 'fixture.svg'));
t.false(buffer.includes('http://www.w3.org/2000/svg'));
const data = await imagemin.buffer(buffer, {
plugins: [
imageminSvgo({
plugins: [{
addAttributesToSVGElement: {
attributes: [{
xmlns: 'http://www.w3.org/2000/svg'
}]
}
}]
})
]
});
t.true(data.includes('xmlns="http://www.w3.org/2000/svg"'));
t.true(data.length > buffer.length);
});
test('output at the specified location', async t => {
const temporary = tempy.directory();
const destinationTemporary = tempy.directory();
const buffer = await readFile(path.join(__dirname, 'fixture.jpg'));
await makeDir(temporary);
await writeFile(path.join(temporary, 'fixture.jpg'), buffer);
const files = await imagemin(['fixture.jpg', `${temporary}/*.jpg`], {
destination: destinationTemporary,
plugins: [imageminJpegtran()]
});
t.true(fs.existsSync(files[0].destinationPath));
t.true(fs.existsSync(files[1].destinationPath));
await del([temporary, destinationTemporary], {force: true});
});
test('set webp ext', async t => {
const temporary = tempy.file();
const files = await imagemin(['fixture.jpg'], {
destination: temporary,
plugins: [imageminWebp()]
});
t.is(path.extname(files[0].destinationPath), '.webp');
await del(temporary, {force: true});
});
test('ignores junk files', async t => {
const temporary = tempy.directory();
const destinationTemporary = tempy.directory();
const buffer = await readFile(path.join(__dirname, 'fixture.jpg'));
await makeDir(temporary);
await writeFile(path.join(temporary, '.DS_Store'), '');
await writeFile(path.join(temporary, 'Thumbs.db'), '');
await writeFile(path.join(temporary, 'fixture.jpg'), buffer);
await t.notThrowsAsync(imagemin([`${temporary}/*`], {
destination: destinationTemporary,
plugins: [imageminJpegtran()]
}));
t.true(fs.existsSync(path.join(destinationTemporary, 'fixture.jpg')));
t.false(fs.existsSync(path.join(destinationTemporary, '.DS_Store')));
t.false(fs.existsSync(path.join(destinationTemporary, 'Thumbs.db')));
await del([temporary, destinationTemporary], {force: true});
});
test('glob option', async t => {
const files = await imagemin(['fixture.jpg'], {
glob: false,
plugins: [imageminJpegtran()]
});
t.true(isJpg(files[0].data));
});