Skip to content

Commit

Permalink
Make build tests more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasw committed Apr 19, 2020
1 parent 6ae3831 commit 0cb9705
Show file tree
Hide file tree
Showing 27 changed files with 124 additions and 56 deletions.
2 changes: 1 addition & 1 deletion bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function checkConfig() {
if (dependentHasExifReaderConfig()) {
if (!isDependenciesInstalled()) {
console.log('Installing ExifReader custom build dependencies...'); // eslint-disable-line no-console
execSync('npm install --no-optional --no-package-lock --no-save @babel/core @babel/preset-env @babel/register babel-loader cross-env string-replace-loader webpack webpack-cli', {stdio: 'inherit'});
execSync('npm install --loglevel=error --no-optional --no-package-lock --no-save @babel/core @babel/preset-env @babel/register babel-loader cross-env string-replace-loader webpack webpack-cli', {stdio: 'inherit'});
console.log('Done.'); // eslint-disable-line no-console
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion dist/exif-reader.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"start": "webpack-dev-server",
"test": "cross-env BABEL_ENV=test mocha --require @babel/register --recursive \"test/unit/\"",
"test:build": "mocha test/build/test.js",
"test:build:custom": "mocha test/build/test-custom.js",
"test:build:custom": "mocha --bail test/build/test-custom.js",
"test:build:update": "node test/build/update.js",
"test:e2e": "cypress run",
"test:all": "npm-run-all lint test test:e2e test:build test:build:custom",
Expand Down
3 changes: 3 additions & 0 deletions test/build/custom-builds.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"id": "everything",
"rebuild": true,
"config": {
"include": {
"file": true,
Expand Down Expand Up @@ -54,6 +55,7 @@
},
{
"id": "partial-jpeg",
"rebuild": true,
"config": {
"include": {
"jpeg": true,
Expand All @@ -64,6 +66,7 @@
},
{
"id": "full-png",
"rebuild": true,
"config": {
"include": {
"png": true,
Expand Down
61 changes: 53 additions & 8 deletions test/build/exif.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');

module.exports = {
parse
Expand All @@ -13,15 +14,9 @@ function parse(imagePath, libraryDir = '../..') {

try {
const result = {
combined: ExifReader.load(data),
expanded: ExifReader.load(data, {expanded: true})
combined: hashDetails(ExifReader.load(data)),
expanded: hashGroupDetails(ExifReader.load(data, {expanded: true}))
};
if (result.combined.Thumbnail && result.combined.Thumbnail.image) {
result.combined.Thumbnail.image = (new Uint8Array(result.combined.Thumbnail.image)).map((char) => Number(char)).toString();
}
if (result.expanded.Thumbnail && result.expanded.Thumbnail.image) {
result.expanded.Thumbnail.image = (new Uint8Array(result.expanded.Thumbnail.image)).map((char) => Number(char)).toString();
}
return result;
} catch (error) {
if (error instanceof ExifReader.errors.MetadataMissingError) {
Expand All @@ -30,3 +25,53 @@ function parse(imagePath, libraryDir = '../..') {
return error.toString();
}
}

function hashDetails(tags) {
for (const tagName of Object.keys(tags)) {
if (tagName === 'Thumbnail') {
if (tags[tagName].image) {
tags[tagName].image = hash(tags[tagName].image);
tags[tagName].base64 = hash(tags[tagName].base64);
}
} else if (Array.isArray(tags[tagName])) {
tags[tagName].map((item) => {
item.value = hash(item.value);
item.description = hash(item.description);
});
} else {
tags[tagName].value = hash(tags[tagName].value);
tags[tagName].description = hash(tags[tagName].description);
if (tags[tagName].attributes) {
tags[tagName].attributes = hash(tags[tagName].attributes);
}
}
}

return tags;
}

function hashGroupDetails(tags) {
for (const tagGroupName of Object.keys(tags)) {
if (tagGroupName === 'Thumbnail') {
hashDetails({Thumbnail: tags[tagGroupName]});
} else {
hashDetails(tags[tagGroupName]);
}
}

return tags;
}

function hash(value) {
if (value instanceof ArrayBuffer) {
value = (new Uint8Array(value)).map((byte) => Number(byte));
}
const stringified = JSON.stringify(value);
if (stringified.length > 200) {
return crypto.createHash('sha1').update(stringified).digest('base64');
}
if (Array.isArray(value)) {
return value.join(', ');
}
return value;
}
40 changes: 25 additions & 15 deletions test/build/test-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ describe('custom configuration image outputs', () => {
const TEMP_PROJECT_DIR = path.join(__dirname, 'tmp');
const PACKAGE = path.join(__dirname, `../../exifreader-${getVersion()}.tgz`);

const filter = getFilter(process.argv);

before(() => {
cleanUp();
});

configurations.forEach((configuration) => {
configurations.filter((configuration) => !filter || (configuration.id === filter)).forEach((configuration) => {
describe(`configuration "${configuration.id}"`, function () {
this.timeout(60000);

Expand All @@ -39,27 +41,35 @@ describe('custom configuration image outputs', () => {
});
});

describe('rebuild', () => {
before(() => {
setUp();
execSync(`npm install --loglevel=error ${PACKAGE}`, {stdio: 'ignore'});
updatePackageJson(configuration.config);
execSync('npm rebuild exifreader', {stdio: 'ignore'});
});
if (configuration.rebuild) {
describe('rebuild', () => {
before(() => {
setUp();
execSync(`npm install --loglevel=error ${PACKAGE}`, {stdio: 'ignore'});
updatePackageJson(configuration.config);
execSync('npm rebuild exifreader', {stdio: 'ignore'});
});

after(() => {
cleanUp();
});
after(() => {
cleanUp();
});

fs.readdirSync(path.join(FIXTURES_PATH, 'images')).forEach((imageName) => {
it(`matches stored image output for ${imageName}`, () => {
testFile(imageName, configuration);
fs.readdirSync(path.join(FIXTURES_PATH, 'images')).forEach((imageName) => {
it(`matches stored image output for ${imageName}`, () => {
testFile(imageName, configuration);
});
});
});
});
}
});
});

function getFilter(argv) {
return argv
.filter((arg) => arg.startsWith('--name='))
.map((arg) => arg.replace(/^--name=/, ''))[0];
}

function getVersion() {
try {
const version = require(path.join(__dirname, '../../package.json')).version;
Expand Down
30 changes: 20 additions & 10 deletions test/build/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ const Exif = require('./exif');
const FIXTURES_PATH = path.join(__dirname, '..', 'fixtures');

describe('image outputs', () => {
const filter = getFilter(process.argv);

fs.readdirSync(path.join(FIXTURES_PATH, 'images')).forEach((imageName) => {
it(`matches stored image output for "${imageName}"`, () => {
const storedResult = JSON.parse(fs.readFileSync(path.join(FIXTURES_PATH, 'outputs', `${imageName}.out`)));
// The process needs to be the same as when the stored result was
// created and now retrieved, i.e. first stringified and then parsed,
// since some values can't be correctly represented in JSON format.
const result = JSON.parse(JSON.stringify(
Exif.parse(path.join(FIXTURES_PATH, 'images', imageName))
));
if (!filter || (path.resolve(path.join(FIXTURES_PATH, 'images', imageName)) === filter)) {
it(`matches stored image output for "${imageName}"`, () => {
const storedResult = JSON.parse(fs.readFileSync(path.join(FIXTURES_PATH, 'outputs', `${imageName}.out`)));
// The process needs to be the same as when the stored result was
// created and now retrieved, i.e. first stringified and then parsed,
// since some values can't be correctly represented in JSON format.
const result = JSON.parse(JSON.stringify(
Exif.parse(path.join(FIXTURES_PATH, 'images', imageName))
));

expect(result).to.deep.equal(storedResult);
});
expect(result).to.deep.equal(storedResult);
});
}
});

function getFilter(argv) {
return argv
.filter((arg) => arg.startsWith('--image='))
.map((arg) => path.resolve(arg.replace(/^--image=/, '')))[0];
}
});
2 changes: 1 addition & 1 deletion test/fixtures/outputs/test-iptc.jpg.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/fixtures/outputs/test-iptc.jpg_everything.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/fixtures/outputs/test-iptc.jpg_full-jpeg.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/fixtures/outputs/test-iptc.jpg_jpeg-heic.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"combined":{"XResolution":{"id":282,"value":[72,1],"description":"72"},"YResolution":{"id":283,"value":[72,1],"description":"72"},"ResolutionUnit":{"id":296,"value":2,"description":"inches"},"YCbCrPositioning":{"id":531,"value":1,"description":"centered"},"Exif IFD Pointer":{"id":34665,"value":90,"description":90},"ExifVersion":{"id":36864,"value":[48,50,51,50],"description":"0232"},"ComponentsConfiguration":{"id":37121,"value":[1,2,3,0],"description":""},"FlashpixVersion":{"id":40960,"value":[48,49,48,48],"description":"0100"},"ColorSpace":{"id":40961,"value":65535,"description":"Uncalibrated"},"LensModel":{"id":42036,"value":["EF24-105mm f/4L IS USM"],"description":"EF24-105mm f/4L IS USM"},"Thumbnail":{"Compression":{"id":259,"value":6,"description":6},"XResolution":{"id":282,"value":[72,1],"description":"72"},"YResolution":{"id":283,"value":[72,1],"description":"72"},"ResolutionUnit":{"id":296,"value":2,"description":"inches"},"JPEGInterchangeFormat":{"id":513,"value":274,"description":274},"JPEGInterchangeFormatLength":{"id":514,"value":1212,"description":1212}}},"expanded":{"exif":{"XResolution":{"id":282,"value":[72,1],"description":"72"},"YResolution":{"id":283,"value":[72,1],"description":"72"},"ResolutionUnit":{"id":296,"value":2,"description":"inches"},"YCbCrPositioning":{"id":531,"value":1,"description":"centered"},"Exif IFD Pointer":{"id":34665,"value":90,"description":90},"ExifVersion":{"id":36864,"value":[48,50,51,50],"description":"0232"},"ComponentsConfiguration":{"id":37121,"value":[1,2,3,0],"description":""},"FlashpixVersion":{"id":40960,"value":[48,49,48,48],"description":"0100"},"ColorSpace":{"id":40961,"value":65535,"description":"Uncalibrated"},"LensModel":{"id":42036,"value":["EF24-105mm f/4L IS USM"],"description":"EF24-105mm f/4L IS USM"}},"Thumbnail":{"Compression":{"id":259,"value":6,"description":6},"XResolution":{"id":282,"value":[72,1],"description":"72"},"YResolution":{"id":283,"value":[72,1],"description":"72"},"ResolutionUnit":{"id":296,"value":2,"description":"inches"},"JPEGInterchangeFormat":{"id":513,"value":274,"description":274},"JPEGInterchangeFormatLength":{"id":514,"value":1212,"description":1212}}}}
{"combined":{"XResolution":{"id":282,"value":"72, 1","description":"72"},"YResolution":{"id":283,"value":"72, 1","description":"72"},"ResolutionUnit":{"id":296,"value":2,"description":"inches"},"YCbCrPositioning":{"id":531,"value":1,"description":"centered"},"Exif IFD Pointer":{"id":34665,"value":90,"description":90},"ExifVersion":{"id":36864,"value":"48, 50, 51, 50","description":"0232"},"ComponentsConfiguration":{"id":37121,"value":"1, 2, 3, 0","description":""},"FlashpixVersion":{"id":40960,"value":"48, 49, 48, 48","description":"0100"},"ColorSpace":{"id":40961,"value":65535,"description":"Uncalibrated"},"LensModel":{"id":42036,"value":"EF24-105mm f/4L IS USM","description":"EF24-105mm f/4L IS USM"},"Thumbnail":{"Compression":{"id":259,"value":6,"description":6},"XResolution":{"id":282,"value":[72,1],"description":"72"},"YResolution":{"id":283,"value":[72,1],"description":"72"},"ResolutionUnit":{"id":296,"value":2,"description":"inches"},"JPEGInterchangeFormat":{"id":513,"value":274,"description":274},"JPEGInterchangeFormatLength":{"id":514,"value":1212,"description":1212}}},"expanded":{"exif":{"XResolution":{"id":282,"value":"72, 1","description":"72"},"YResolution":{"id":283,"value":"72, 1","description":"72"},"ResolutionUnit":{"id":296,"value":2,"description":"inches"},"YCbCrPositioning":{"id":531,"value":1,"description":"centered"},"Exif IFD Pointer":{"id":34665,"value":90,"description":90},"ExifVersion":{"id":36864,"value":"48, 50, 51, 50","description":"0232"},"ComponentsConfiguration":{"id":37121,"value":"1, 2, 3, 0","description":""},"FlashpixVersion":{"id":40960,"value":"48, 49, 48, 48","description":"0100"},"ColorSpace":{"id":40961,"value":65535,"description":"Uncalibrated"},"LensModel":{"id":42036,"value":"EF24-105mm f/4L IS USM","description":"EF24-105mm f/4L IS USM"}},"Thumbnail":{"Compression":{"id":259,"value":6,"description":6},"XResolution":{"id":282,"value":[72,1],"description":"72"},"YResolution":{"id":283,"value":[72,1],"description":"72"},"ResolutionUnit":{"id":296,"value":2,"description":"inches"},"JPEGInterchangeFormat":{"id":513,"value":274,"description":274},"JPEGInterchangeFormatLength":{"id":514,"value":1212,"description":1212}}}}
2 changes: 1 addition & 1 deletion test/fixtures/outputs/test-iptc.jpg_partial-jpeg.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"combined":{"undefined-282":{"id":282,"value":[72,1],"description":[72,1]},"undefined-283":{"id":283,"value":[72,1],"description":[72,1]},"undefined-296":{"id":296,"value":2,"description":2},"undefined-531":{"id":531,"value":1,"description":1},"Exif IFD Pointer":{"id":34665,"value":90,"description":90},"undefined-36864":{"id":36864,"value":[48,50,51,50],"description":[48,50,51,50]},"ComponentsConfiguration":{"id":37121,"value":[1,2,3,0],"description":""},"undefined-40960":{"id":40960,"value":[48,49,48,48],"description":[48,49,48,48]},"undefined-40961":{"id":40961,"value":65535,"description":65535},"undefined-42036":{"id":42036,"value":["EF24-105mm f/4L IS USM"],"description":["EF24-105mm f/4L IS USM"]},"Thumbnail":{"undefined-259":{"id":259,"value":6,"description":6},"undefined-282":{"id":282,"value":[72,1],"description":[72,1]},"undefined-283":{"id":283,"value":[72,1],"description":[72,1]},"undefined-296":{"id":296,"value":2,"description":2},"undefined-513":{"id":513,"value":274,"description":274},"undefined-514":{"id":514,"value":1212,"description":1212}},"undefined-604":{"id":604,"value":[77,121,32,83,117,98,108,111,99,97,116,105,111,110],"description":[77,121,32,83,117,98,108,111,99,97,116,105,111,110]},"undefined-512":{"id":512,"value":[0,4],"description":[0,4]}},"expanded":{"exif":{"undefined-282":{"id":282,"value":[72,1],"description":[72,1]},"undefined-283":{"id":283,"value":[72,1],"description":[72,1]},"undefined-296":{"id":296,"value":2,"description":2},"undefined-531":{"id":531,"value":1,"description":1},"Exif IFD Pointer":{"id":34665,"value":90,"description":90},"undefined-36864":{"id":36864,"value":[48,50,51,50],"description":[48,50,51,50]},"ComponentsConfiguration":{"id":37121,"value":[1,2,3,0],"description":""},"undefined-40960":{"id":40960,"value":[48,49,48,48],"description":[48,49,48,48]},"undefined-40961":{"id":40961,"value":65535,"description":65535},"undefined-42036":{"id":42036,"value":["EF24-105mm f/4L IS USM"],"description":["EF24-105mm f/4L IS USM"]}},"Thumbnail":{"undefined-259":{"id":259,"value":6,"description":6},"undefined-282":{"id":282,"value":[72,1],"description":[72,1]},"undefined-283":{"id":283,"value":[72,1],"description":[72,1]},"undefined-296":{"id":296,"value":2,"description":2},"undefined-513":{"id":513,"value":274,"description":274},"undefined-514":{"id":514,"value":1212,"description":1212}},"iptc":{"undefined-604":{"id":604,"value":[77,121,32,83,117,98,108,111,99,97,116,105,111,110],"description":[77,121,32,83,117,98,108,111,99,97,116,105,111,110]},"undefined-512":{"id":512,"value":[0,4],"description":[0,4]}}}}
{"combined":{"undefined-282":{"id":282,"value":"72, 1","description":"72, 1"},"undefined-283":{"id":283,"value":"72, 1","description":"72, 1"},"undefined-296":{"id":296,"value":2,"description":2},"undefined-531":{"id":531,"value":1,"description":1},"Exif IFD Pointer":{"id":34665,"value":90,"description":90},"undefined-36864":{"id":36864,"value":"48, 50, 51, 50","description":"48, 50, 51, 50"},"ComponentsConfiguration":{"id":37121,"value":"1, 2, 3, 0","description":""},"undefined-40960":{"id":40960,"value":"48, 49, 48, 48","description":"48, 49, 48, 48"},"undefined-40961":{"id":40961,"value":65535,"description":65535},"undefined-42036":{"id":42036,"value":"EF24-105mm f/4L IS USM","description":"EF24-105mm f/4L IS USM"},"Thumbnail":{"undefined-259":{"id":259,"value":6,"description":6},"undefined-282":{"id":282,"value":[72,1],"description":[72,1]},"undefined-283":{"id":283,"value":[72,1],"description":[72,1]},"undefined-296":{"id":296,"value":2,"description":2},"undefined-513":{"id":513,"value":274,"description":274},"undefined-514":{"id":514,"value":1212,"description":1212}},"undefined-604":{"id":604,"value":"77, 121, 32, 83, 117, 98, 108, 111, 99, 97, 116, 105, 111, 110","description":"77, 121, 32, 83, 117, 98, 108, 111, 99, 97, 116, 105, 111, 110"},"undefined-512":{"id":512,"value":"0, 4","description":"0, 4"}},"expanded":{"exif":{"undefined-282":{"id":282,"value":"72, 1","description":"72, 1"},"undefined-283":{"id":283,"value":"72, 1","description":"72, 1"},"undefined-296":{"id":296,"value":2,"description":2},"undefined-531":{"id":531,"value":1,"description":1},"Exif IFD Pointer":{"id":34665,"value":90,"description":90},"undefined-36864":{"id":36864,"value":"48, 50, 51, 50","description":"48, 50, 51, 50"},"ComponentsConfiguration":{"id":37121,"value":"1, 2, 3, 0","description":""},"undefined-40960":{"id":40960,"value":"48, 49, 48, 48","description":"48, 49, 48, 48"},"undefined-40961":{"id":40961,"value":65535,"description":65535},"undefined-42036":{"id":42036,"value":"EF24-105mm f/4L IS USM","description":"EF24-105mm f/4L IS USM"}},"Thumbnail":{"undefined-259":{"id":259,"value":6,"description":6},"undefined-282":{"id":282,"value":[72,1],"description":[72,1]},"undefined-283":{"id":283,"value":[72,1],"description":[72,1]},"undefined-296":{"id":296,"value":2,"description":2},"undefined-513":{"id":513,"value":274,"description":274},"undefined-514":{"id":514,"value":1212,"description":1212}},"iptc":{"undefined-604":{"id":604,"value":"77, 121, 32, 83, 117, 98, 108, 111, 99, 97, 116, 105, 111, 110","description":"77, 121, 32, 83, 117, 98, 108, 111, 99, 97, 116, 105, 111, 110"},"undefined-512":{"id":512,"value":"0, 4","description":"0, 4"}}}}
Loading

0 comments on commit 0cb9705

Please sign in to comment.