Repository navigation
Expand file tree
/
Copy pathbin.test.js
More file actions
80 lines (63 loc) · 2.59 KB
/
Copy pathbin.test.js
File metadata and controls
80 lines (63 loc) · 2.59 KB
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
/* eslint-env mocha */
const { spawn } = require('child_process');
const path = require('path');
const { expect } = require('chai');
const fs = require('fs-extra');
const eos = require('end-of-stream');
const root = require('rootrequire');
const isPng = require('is-png');
const jimp = require('jimp');
describe('pngmin-cli', () => {
const exec = async (args, options = {}, input = Buffer.from('')) => {
return await Promise.resolve().then(async () => {
const proc = spawn(process.execPath, ['bin'].concat(args), Object.assign({}, options, {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: root,
windowsHide: true
}));
const stdout = [];
const stderr = [];
proc.stdout.on('data', chunk => stdout.push(chunk));
proc.stderr.on('data', chunk => stderr.push(chunk));
proc.stdin.end(input);
const [code] = await Promise.all([
new Promise(resolve => proc.on('exit', code => resolve(code))),
new Promise(resolve => eos(proc.stdout, () => resolve())),
new Promise(resolve => eos(proc.stderr, () => resolve())),
]);
return {
err: { code },
stdout: Buffer.concat(stdout),
stderr: Buffer.concat(stderr)
};
});
};
it('reads from stdin and writes to stdout', async () => {
const input = await fs.readFile(path.resolve(root, 'temp', '0002.png'));
const { stdout, stderr, err } = await exec([], {}, input);
expect(isPng(stdout)).to.equal(true, 'output was not a PNG image');
// the test image goes from 36K to 14K
expect(stdout.length).to.be.below(input.length / 2);
const inImage = await jimp.read(input);
const outImage = await jimp.read(stdout);
expect(outImage.hash())
.to.equal(inImage.hash())
.and.to.equal('ci06j0qw3Ew');
expect(stderr.toString()).to.equal('');
expect(err).to.have.property('code', 0);
});
it('errors if given a non-png image', async () => {
const input = await fs.readFile(path.resolve(root, 'temp', '0001.jpg'));
const { stdout, stderr, err } = await exec([], {}, input);
expect(stderr.toString()).to.include('Error: the provided input is not a PNG image');
expect(stdout.toString()).to.equal('');
expect(err).to.have.property('code', 1);
});
it('errors if given garbage content', async () => {
const input = Buffer.from('I am but a wee string');
const { stdout, stderr, err } = await exec([], {}, input);
expect(stderr.toString()).to.include('Error: the provided input is not a PNG image');
expect(stdout.toString()).to.equal('');
expect(err).to.have.property('code', 1);
});
});