-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.test.js
197 lines (184 loc) · 7.43 KB
/
index.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import dirTree from 'directory-tree';
import { unlink, rm } from 'fs/promises';
import { resolve as _resolve } from 'path';
import spawn from 'spawn-command';
const APP_ENTRY_PATH = require.resolve('./index');
const SAMPLE_DIRECTORY = 'sample-images';
const getInputImages = (dir = SAMPLE_DIRECTORY) => dirTree(dir, { extensions: /\.(png|jpg)/ });
const getOutputImages = (dir = SAMPLE_DIRECTORY) => dirTree(dir, { extensions: /\.webp/ });
const removeFiles = async (dirTreeObj) => {
if (!('children' in dirTreeObj)) {
await unlink(dirTreeObj.path);
} else {
// eslint-disable-next-line no-restricted-syntax
for (const child of dirTreeObj.children) {
if (!('children' in child)) {
await unlink(child.path);
} else {
await removeFiles(child);
}
}
}
};
const clearOutputs = async (dir = SAMPLE_DIRECTORY) => {
const outputImg = getOutputImages(dir);
await removeFiles(outputImg);
};
const runCLI = (args = '', cwd = process.cwd()) => {
const isRelative = cwd[0] !== '/';
let workingDir = cwd;
if (isRelative) {
workingDir = _resolve(__dirname, cwd);
}
return new Promise((resolve, reject) => {
let stdout = '';
let stderr = '';
const command = `${APP_ENTRY_PATH} ${args}`;
const child = spawn(command, { cwd: workingDir });
child.on('error', (error) => {
reject(error);
});
child.stdout.on('data', (data) => {
stdout += data.toString();
});
child.stderr.on('data', (data) => {
stderr += data.toString();
});
child.on('close', () => {
if (stderr) {
reject(stderr);
} else {
resolve(stdout);
}
});
});
};
test('webpconvert --help', async () => {
const stdout = await runCLI('--help');
expect(stdout).toMatchSnapshot();
});
describe('Convert Images', () => {
beforeEach(async () => {
await clearOutputs();
});
afterEach(async () => {
await clearOutputs();
});
describe('Directory', () => {
test('webpconvert | it convert images in current directory', async () => {
const inputImg = getInputImages();
let outputImg = getOutputImages();
expect(inputImg.children.length).toBe(7);
expect(outputImg.children.length).toBe(1);
const stdout = await runCLI('', SAMPLE_DIRECTORY);
outputImg = getOutputImages();
expect(outputImg.children.length).toBe(7);
expect(stdout.match(/Minified 3 images/g)).toHaveLength(2);
});
test('webpconvert sample-images | it convert images in specified directory', async () => {
const inputImg = getInputImages();
let outputImg = getOutputImages();
expect(inputImg.children.length).toBe(7);
expect(outputImg.children.length).toBe(1);
const stdout = await runCLI(SAMPLE_DIRECTORY);
outputImg = getOutputImages();
expect(outputImg.children.length).toBe(7);
expect(stdout.match(/Minified 3 images/g)).toHaveLength(2);
});
test('webpconvert sample-images output | it convert images from input directory to output directory', async () => {
const inputImg = getInputImages();
let outputImg = getOutputImages('output');
expect(inputImg.children.length).toBe(7);
expect(outputImg).toBe(null);
try {
const stdout = await runCLI(`${SAMPLE_DIRECTORY} output`);
outputImg = getOutputImages('output');
expect(outputImg.children.length).toBe(6);
expect(stdout.match(/Minified 3 images/g)).toHaveLength(2);
} finally {
await clearOutputs('output');
await rm('output', { recursive: true, force: true });
}
});
});
describe('Directory Recursive', () => {
test('webpconvert -r | it convert images in current directory', async () => {
const inputImg = getInputImages();
let outputImg = getOutputImages();
expect(inputImg.children.length).toBe(7);
expect(outputImg.children.length).toBe(1);
const stdout = await runCLI('-r', SAMPLE_DIRECTORY);
outputImg = getOutputImages();
expect(outputImg.children.length).toBe(7);
expect(stdout.match(/Minified 6 images/g)).toHaveLength(2);
});
test('webpconvert -r sample-images | it convert images in specified directory', async () => {
const inputImg = getInputImages();
let outputImg = getOutputImages();
expect(inputImg.children.length).toBe(7);
expect(outputImg.children.length).toBe(1);
const stdout = await runCLI(`-r ${SAMPLE_DIRECTORY}`);
outputImg = getOutputImages();
expect(outputImg.children.length).toBe(7);
expect(stdout.match(/Minified 6 images/g)).toHaveLength(2);
});
test('webpconvert -r sample-images output | it convert images from input directory to output directory', async () => {
const inputImg = getInputImages();
let outputImg = getOutputImages('output');
expect(inputImg.children.length).toBe(7);
expect(outputImg).toBe(null);
try {
const stdout = await runCLI(`-r ${SAMPLE_DIRECTORY} output`);
outputImg = getOutputImages('output');
expect(outputImg.children.length).toBe(7);
expect(stdout.match(/Minified 6 images/g)).toHaveLength(2);
} finally {
await clearOutputs('output');
await rm('output', { recursive: true, force: true });
}
});
});
describe('File', () => {
test('webpconvert sample-images/KittenJPG.jpg | it convert a single image file', async () => {
const inputImg = getInputImages();
let outputImg = getOutputImages();
expect(inputImg.children.length).toBe(7);
expect(outputImg.children.length).toBe(1);
const stdout = await runCLI(`${SAMPLE_DIRECTORY}/KittenJPG.jpg`);
outputImg = getOutputImages();
expect(outputImg.children.length).toBe(2);
expect(stdout.match(/Minified 1 image/g)).toHaveLength(1);
});
test('webpconvert sample-images/KittenPNG.jpg | it convert a single image file with quality 50', async () => {
const inputImg = getInputImages();
let outputImg = getOutputImages();
expect(inputImg.children.length).toBe(7);
expect(outputImg.children.length).toBe(1);
const stdout = await runCLI(`${SAMPLE_DIRECTORY}/KittenPNG.png -q 50`);
outputImg = getOutputImages();
expect(outputImg.children.length).toBe(2);
expect(stdout.match(/Minified 1 image/g)).toHaveLength(1);
});
test('webpconvert sample-images/KittenPNG.jpg | it convert a single image file with prefix and suffix', async () => {
const inputImg = getInputImages();
let outputImg = getOutputImages();
expect(inputImg.children.length).toBe(7);
expect(outputImg.children.length).toBe(1);
const stdout = await runCLI(`${SAMPLE_DIRECTORY}/KittenPNG.png --prefix="img-" --suffix="-compressed"`);
outputImg = getOutputImages();
expect(outputImg.children.length).toBe(2);
expect(outputImg.children[1].name).toMatch('img-KittenPNG-compressed.webp');
expect(stdout.match(/Minified 1 image/g)).toHaveLength(1);
});
test('webpconvert sample-images/UppercaseJPG.jpg | it convert a single image file with uppercase extension', async () => {
const inputImg = getInputImages();
let outputImg = getOutputImages();
expect(inputImg.children.length).toBe(7);
expect(outputImg.children.length).toBe(1);
const stdout = await runCLI(`${SAMPLE_DIRECTORY}/UppercaseJPG.JPG`);
outputImg = getOutputImages();
expect(outputImg.children.length).toBe(2);
expect(stdout.match(/Minified 1 image/g)).toHaveLength(1);
});
});
});