diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6747121 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +# Travis CI Configuration File + +services: + - docker + +notifications: + email: false + +before_script: + - npm run build-node-modules + +script: + - npm run test diff --git a/package.json b/package.json index 856cdf8..752d1e1 100755 --- a/package.json +++ b/package.json @@ -16,6 +16,8 @@ "scripts": { "build-node-modules": "rm -R node_modules ; docker run --rm -v `pwd`:/var/task lambci/lambda:build-nodejs10.x npm install", "test-file": "docker run --rm -e S3_BUCKET=hmn-uploads-eu -e S3_REGION=eu-west-1 -v `pwd`:/var/task lambci/lambda:nodejs10.x lambda-handler.handler '{\"path\":\"/'$npm_config_path'\", \"headers\":{}}'", + "test": "docker run --rm -v `pwd`:/var/task -it --entrypoint='node' lambci/lambda:nodejs10.x /var/task/test-filesize/index.js", + "update-test-fixtures": "docker run --rm -v `pwd`:/var/task -it --entrypoint='node' lambci/lambda:nodejs10.x /var/task/test-filesize/index.js --update-fixtures", "build-zip": "rm lambda.zip; zip -r --exclude='node_modules/aws-sdk/*' --exclude='node_modules/animated-gif-detector/test/*' lambda.zip ./node_modules/ index.js proxy-file.js lambda-handler.js", "upload-zip": "aws s3 --region=$npm_config_region cp ./lambda.zip s3://$npm_config_bucket/$npm_config_path", "update-function-code": "aws lambda update-function-code --region $npm_config_region --function-name $npm_config_function_name --zip-file fileb://`pwd`/lambda.zip" diff --git a/test-filesize/fixtures.json b/test-filesize/fixtures.json new file mode 100644 index 0000000..12813c2 --- /dev/null +++ b/test-filesize/fixtures.json @@ -0,0 +1,27 @@ +{ + "briefing-copywriting.jpg-original.jpeg": 115390, + "briefing-copywriting.jpg-small.jpeg": 3092, + "briefing-copywriting.jpg-medium.jpeg": 9584, + "briefing-copywriting.jpg-large.jpeg": 29223, + "briefing-copywriting.jpg-webp.webp": 15660, + "hdr.jpg-original.jpeg": 149042, + "hdr.jpg-small.jpeg": 10589, + "hdr.jpg-medium.jpeg": 24100, + "hdr.jpg-large.jpeg": 87533, + "hdr.jpg-webp.webp": 82784, + "Website.png-original.png": 34589, + "Website.png-small.png": 3420, + "Website.png-medium.png": 13778, + "Website.png-large.png": 34589, + "Website.png-webp.webp": 20288, + "icons.png-original.png": 28026, + "icons.png-small.png": 3948, + "icons.png-medium.png": 11212, + "icons.png-large.png": 26372, + "icons.png-webp.webp": 24816, + "humans.png-original.png": 873684, + "humans.png-small.png": 9175, + "humans.png-medium.png": 56093, + "humans.png-large.png": 279635, + "humans.png-webp.webp": 141340 +} diff --git a/test-filesize/index.js b/test-filesize/index.js index 75b330f..eeae9f7 100644 --- a/test-filesize/index.js +++ b/test-filesize/index.js @@ -3,14 +3,16 @@ const Filesize = require('filesize'); const tachyon = require('../index'); const fs = require('fs'); -let images = fs.readdirSync('./images'); +let images = fs.readdirSync( __dirname + '/images' ); const args = process.argv.slice(2); -if ( args[0] ) { +if ( args[0] && args[0].indexOf( '--' ) !== 0 ) { images = images.filter( file => args[0] === file ); } +const saveFixtured = args.indexOf( '--update-fixtures' ) > -1; + const table = new Table({ head: [ 'Image', @@ -24,6 +26,11 @@ const table = new Table({ colWidths: [30, 15, 20, 15, 15, 15, 20], }); +// Read in existing features for resizes, so we can detect if image resizing +// has lead to a change in file size from previous runs. +const oldFixtures = JSON.parse( fs.readFileSync( __dirname + '/fixtures.json' ) ); +const fixtures = {}; + async function test() { await Promise.all( images.map(async imageName => { @@ -51,7 +58,9 @@ async function test() { // Save each one to the file system for viewing. Object.entries(resized).forEach(([size, image]) => { - fs.writeFile( `${__dirname}/output/${imageName}-${size}.${image.info.format}`, image.data, () => {}); + const imageKey = `${imageName}-${size}.${image.info.format}`; + fixtures[ imageKey ] = image.data.length; + fs.writeFile( `${__dirname}/output/${imageKey}`, image.data, () => {}); }); table.push([ @@ -69,10 +78,36 @@ async function test() { Math.floor(resized.webp.info.size / resized.large.info.size * 100) + '%)', ]); + }) ); + if ( saveFixtured ) { + fs.writeFileSync( __dirname + '/fixtures.json', JSON.stringify( fixtures, null, 4 ) ); + } + console.log(table.toString()); + + let exitCode = 0; + for (const key in fixtures) { + if ( ! oldFixtures[ key ] ) { + exitCode = 1; + console.error( `${ key } not found in existing fixtures.` ); + } + if ( fixtures[ key ] > oldFixtures[ key ] ) { + const diff = fixtures[ key ] / oldFixtures[ key ] * 100; + exitCode = 1; + console.error( `${ key } is larger than image in fixtures (${ fixtures[ key ] - oldFixtures[ key ] } bytes larger, ${ diff }%.)` ); + } + + if ( fixtures[ key ] < oldFixtures[ key ] ) { + const diff = oldFixtures[ key ] / fixtures[ key ] * 100; + console.log( `${ key } is smaller than image in fixtures (${ fixtures[ key ] - oldFixtures[ key ] } bytes smaller, ${ diff }%.)` ); + } + } + // Exit the script if the fixtures have changed in a negative direction. This means + // TravisCI etc will detect the failure correctly. + process.exit(exitCode); } test();