-
Notifications
You must be signed in to change notification settings - Fork 62
/
test.js
113 lines (85 loc) · 2.74 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
import {promisify} from 'node:util';
import fs from 'node:fs';
import {dirname} from 'node:path';
import {fileURLToPath} from 'node:url';
import process from 'node:process';
import {execa} from 'execa';
import test from 'ava';
const __dirname = dirname(fileURLToPath(import.meta.url));
process.chdir(__dirname);
const readFile = promisify(fs.readFile);
test('show version', async t => {
const {stdout} = await execa('./cli.js', ['--version']);
t.is(stdout, JSON.parse(await readFile('./package.json')).version);
});
test('optimize a GIF', async t => {
const input = await readFile('fixtures/test.gif');
const {stdout} = await execa('./cli.js', {
input,
encoding: 'buffer',
});
t.true(stdout.length < input.length);
});
test('optimize a JPG', async t => {
const input = await readFile('fixtures/test.jpg');
const {stdout} = await execa('./cli.js', {
input,
encoding: 'buffer',
});
t.true(stdout.length < input.length);
});
test('optimize a PNG', async t => {
const input = await readFile('fixtures/test.png');
const {stdout} = await execa('./cli.js', {
input,
encoding: 'buffer',
});
t.true(stdout.length < input.length);
});
test('optimize a SVG', async t => {
const input = await readFile('fixtures/test.svg');
const {stdout} = await execa('./cli.js', {
input,
encoding: 'buffer',
});
t.true(stdout.length < input.length);
});
test('output error on corrupt images', async t => {
await t.throwsAsync(execa('./cli.js', ['fixtures/test-corrupt.jpg']));
});
test('support plugins', async t => {
const input = await readFile('fixtures/test.png');
const {stdout: data} = await execa('./cli.js', ['--plugin=pngquant'], {
input,
encoding: 'buffer',
});
const {stdout: compareData} = await execa('./cli.js', {
input,
encoding: 'buffer',
});
t.true(data.length < compareData.length);
});
test('error when trying to write multiple files to stdout', async t => {
const error = await t.throwsAsync(execa('./cli.js', ['fixtures/test.{jpg,png}']));
t.is(error.stderr.trim(), 'Cannot write multiple files to stdout, specify `--out-dir`');
});
test('throw on missing plugins', async t => {
const input = await readFile('fixtures/test.png');
const error = await t.throwsAsync(execa('./cli.js', ['--plugin=unicorn'], {
input,
encoding: 'buffer',
}));
t.regex(error.stderr.toString(), /Unknown plugin: unicorn/);
});
test('support plugin options', async t => {
const input = await readFile('fixtures/test.png');
const {stdout: data} = await execa('./cli.js', ['--plugin.pngquant.dithering=1', '--plugin.pngquant.quality=0.1', '--plugin.pngquant.quality=0.4'], {
input,
encoding: 'buffer',
});
const {stdout: compareData} = await execa('./cli.js', {
input,
encoding: 'buffer',
});
t.true(data.length < compareData.length);
});