Skip to content

Commit

Permalink
Merge pull request #129 from humanmade/travis
Browse files Browse the repository at this point in the history
Add TravisCI testing
  • Loading branch information
joehoyle authored Dec 20, 2020
2 parents 56f3cd9 + 5595b50 commit 218ce65
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Travis CI Configuration File

services:
- docker

notifications:
email: false

before_script:
- npm run build-node-modules

script:
- npm run test
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 27 additions & 0 deletions test-filesize/fixtures.json
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 38 additions & 3 deletions test-filesize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 => {
Expand Down Expand Up @@ -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([
Expand All @@ -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();

0 comments on commit 218ce65

Please sign in to comment.