Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
bradbaris committed Feb 3, 2017
1 parent fd5e774 commit 5c57de9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const execBuffer = require('exec-buffer');
const isPng = require('is-png');
const isJpg = require('is-jpg');
const isInt = require('number-is-integer');
const guetzli = require('guetzli');

module.exports = opts => buf => {
Expand All @@ -17,8 +18,9 @@ module.exports = opts => buf => {

const args = [];

if (opts.quality) {
args.push('--quality', opts.quality);
if (isInt(opts.quality) && opts.quality >= 0 && opts.quality <= 100) {
console.log(opts.quality);
args.push('-quality', opts.quality);
}

args.push(execBuffer.input, execBuffer.output);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"exec-buffer": "^3.0.0",
"guetzli": "^0.1.0",
"is-jpg": "^1.0.0",
"is-png": "^1.0.0"
"is-png": "^1.0.0",
"number-is-integer": "^1.0.1"
},
"devDependencies": {
"ava": "*",
Expand Down
18 changes: 9 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ import test from 'ava';
import m from './';

const fsP = pify(fs);
let buf;

test.before(async () => {
buf = await fsP.readFile(path.join(__dirname, 'fixtures/test.png'));
});

test('optimize a PNG', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixtures/test.png'));
const data = await m()(buf);

t.true(data.length < buf.length);
t.true(isJpg(data));
});

test('skip optimizing a non-PNG/JPG file', async t => {
const buf1 = await fsP.readFile(__filename);
const data1 = await m()(buf1);
const buf = await fsP.readFile(__filename);
const data = await m()(buf);

t.deepEqual(data1, buf1);
t.deepEqual(data, buf);
});

test('check if --quality flag works', async t => {
const buf2 = await fsP.readFile(path.join(__dirname, 'fixtures/test.png'));
const data2 = await m({quality: 84})(buf2);
const data3 = await m({quality: 95})(buf2);

t.true(data2.length < data3.length);
t.notThrows(m({quality: 85})(buf));
});

0 comments on commit 5c57de9

Please sign in to comment.