forked from imagemin/imagemin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
177 lines (141 loc) · 5.5 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
import fs, {promises as fsPromises} from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import del from 'del';
import imageminJpegtran from 'imagemin-jpegtran';
import imageminWebp from 'imagemin-webp';
import imageminSvgo from 'imagemin-svgo';
import isJpg from 'is-jpg';
import tempy from 'tempy';
import test from 'ava';
import imagemin from './index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
test('optimize a file', async t => {
const buffer = await fsPromises.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 fsPromises.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 fsPromises.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 fsPromises.readFile(path.join(__dirname, 'fixture.jpg'));
const data = await imagemin.buffer(buffer);
t.deepEqual(data, buffer);
t.true(isJpg(data));
});
// TODO: Fix the test.
test.failing('return processed buffer even it is a bad optimization', async t => {
const buffer = await fsPromises.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 fsPromises.readFile(path.join(__dirname, 'fixture.jpg'));
await fsPromises.mkdir(temporary, {recursive: true});
await fsPromises.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('output at the specified location when input paths contain Windows path delimiter', async t => {
const temporary = tempy.directory();
const destinationTemporary = tempy.directory();
const buffer = await fsPromises.readFile(path.join(__dirname, 'fixture.jpg'));
await fsPromises.mkdir(temporary, {recursive: true});
await fsPromises.writeFile(path.join(temporary, 'fixture.jpg'), buffer);
const fileNameWithWindowsPathDelimiter = `${temporary}\\*.jpg`;
const files = await imagemin([fileNameWithWindowsPathDelimiter], {
destination: destinationTemporary,
plugins: [imageminJpegtran()],
});
t.true(fs.existsSync(files[0].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('set svg ext', async t => {
const temporary = tempy.file();
const files = await imagemin(['fixture.svg'], {
destination: temporary,
plugins: [imageminSvgo()],
});
t.is(path.extname(files[0].destinationPath), '.svg');
await del(temporary, {force: true});
});
test('ignores junk files', async t => {
const temporary = tempy.directory();
const destinationTemporary = tempy.directory();
const buffer = await fsPromises.readFile(path.join(__dirname, 'fixture.jpg'));
await fsPromises.mkdir(temporary, {recursive: true});
await fsPromises.writeFile(path.join(temporary, '.DS_Store'), '');
await fsPromises.writeFile(path.join(temporary, 'Thumbs.db'), '');
await fsPromises.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));
});