Skip to content

Commit

Permalink
Merge pull request #6 from Schibsted-Tech-Polska/es6-refactor
Browse files Browse the repository at this point in the history
Es6 refactor
  • Loading branch information
domeq authored Oct 12, 2017
2 parents 3b41196 + 580db4c commit d03ce29
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 71 deletions.
74 changes: 22 additions & 52 deletions lib/rediff.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const generateJobQueue = () => {
const screenshots = { diff: config.resultsDir + specName + '-' + viewport + '-diff.png' };
let workerJobs = [];

Object.keys(config.environments).forEach(function(env) {
Object.keys(config.environments).forEach(env => {
const outputFile = config.resultsDir + specName + '-' + viewport + '-' + env + '.png';
workerJobs.push({
viewport: {
Expand Down Expand Up @@ -116,7 +116,7 @@ const diffPixel = (pixel1, pixel2, match) => {

const diffImages = (test, input1, input2, output) => {
if (fs.existsSync(input1) && fs.existsSync(input2)) {
return diff.outputDiff(input1, input2, output, diffPixel).then(function(data) {
return diff.outputDiff(input1, input2, output, diffPixel).then(data => {
test.diff = data.metric;
return data.output;
});
Expand All @@ -125,18 +125,16 @@ const diffImages = (test, input1, input2, output) => {
};

const convertImages = pngs => {
return Promise.map(pngs, function(fileToConvert) {
return new Promise(function(resolve) {
return Promise.map(pngs, fileToConvert => {
return new Promise(resolve => {
if (fs.existsSync(fileToConvert)) {
convert(
{
input: fileToConvert,
output: fileToConvert.replace(/\.png$/, '.jpg'),
quality: 80,
},
function() {
resolve(fileToConvert);
}
() => resolve(fileToConvert)
);
} else {
resolve(fileToConvert);
Expand All @@ -160,58 +158,33 @@ const gzipFiles = files => {
};

const removeFiles = files => {
return Promise.map(files, function(fileToRemove) {
return Promise.map(files, fileToRemove => {
if (fs.existsSync(fileToRemove)) {
fs.unlinkSync(fileToRemove);
}
});
};

const processJob = (jobs, test, specName) => {
const images = envNames.concat(['diff']).map(function(env) {
return test.screenshots[env];
});
const images = envNames.concat(['diff']).map(env => test.screenshots[env]);

const loImages = images.map(function(image) {
return image.replace(/\.png$/, '.jpg');
});
const loImages = images.map(image => image.replace(/\.png$/, '.jpg'));

const workerPromises = jobs.map(function(job) {
return worker.addSpec(job, handleError.bind(null, job, 'TestRunner'));
});
const workerPromises = jobs.map(job => worker.addSpec(job, handleError.bind(null, job, 'TestRunner')));

return Promise.all(workerPromises)
.catch(function(err) {
handleError(test, 'Crawler', err);
})
.then(function() {
return diffImages(test, images[0], images[1], images[2]);
})
.catch(function(err) {
handleError(test, 'Comparison', err);
})
.then(function() {
return convertImages(images);
})
.then(function() {
return removeFiles(images);
})
.then(function() {
gzipFiles(loImages);
})
.catch(function(err) {
handleError(test, 'General', err);
})
.finally(function() {
reporter.push(test, specName);
});
.catch(err => handleError(test, 'Crawler', err))
.then(() => diffImages(test, images[0], images[1], images[2]))
.catch(err => handleError(test, 'Comparison', err))
.then(() => convertImages(images))
.then(() => removeFiles(images))
.then(() => gzipFiles(loImages))
.catch(err => handleError(test, 'General', err))
.finally(() => reporter.push(test, specName));
};

const processJobSet = job => {
return Promise.map(job.worker, function(jobs, idx) {
return processJob(job.worker[idx], job.tests[idx], job.spec.name);
});
};
const processJobSet = job =>
Promise.map(job.worker, (jobs, idx) => processJob(job.worker[idx], job.tests[idx], job.spec.name));

const run = (cfg, specList) => {
config = cfg;
Expand All @@ -222,14 +195,11 @@ const run = (cfg, specList) => {

reporter.initialize(
config,
queue.map(function(item) {
return item.spec;
})
queue.map(item => item.spec)
);

Promise.map(queue, processJobSet).then(function() {
console.log('Test completed.');
});
Promise.map(queue, processJobSet)
.then(() => console.log('Test completed.'));
};

module.exports = run;
10 changes: 5 additions & 5 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const report = () => {

const initialize = (cfg, specsData) => {
config = cfg;
specs = specsData.map(function(spec) {
return _.merge(
specs = specsData.map(spec =>
_.merge(
{
// name, url
tests: {
Expand All @@ -36,8 +36,8 @@ const initialize = (cfg, specsData) => {
},
},
spec
);
});
)
);

metadata = {
tests: {
Expand All @@ -61,7 +61,7 @@ const push = (result, specName) => {
metadata.tests.failing++;
}

_.keys(result.screenshots).forEach(function(screenshot) {
_.keys(result.screenshots).forEach(screenshot => {
result.screenshots[screenshot] = result.screenshots[screenshot]
.replace(config.resultsDir, '')
.replace(/\.png$/, '.jpg');
Expand Down
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const getSpecViewports = (viewports, filter) => {
return viewports;
}
let result = {};
filter.forEach(function(name) {
filter.forEach(name => {
if (!viewports[name]) {
console.warn(
'WARNING: You are trying to use viewport "' +
Expand Down
18 changes: 6 additions & 12 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ function Worker(config) {

Worker.prototype = {
addSpec: function(spec, errorHandler) {
return new Promise(
function(resolve, reject) {
this.queue.push([spec, errorHandler, resolve, reject]);
this.handleQueue();
}.bind(this)
);
return new Promise((resolve, reject) => {
this.queue.push([spec, errorHandler, resolve, reject]);
this.handleQueue();
});
},

handleQueue: function() {
Expand All @@ -44,7 +42,7 @@ Worker.prototype = {
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
})
.then(function(response) {
.then(response => {
if (!response.ok || response.status < 200 || response.status >= 400) {
throw new Error(`External service error at ${response.url}/v1/screenshot/${body.url}`, {
status: response.status,
Expand All @@ -66,11 +64,7 @@ Worker.prototype = {
.catch(error => {
reject({ spec: spec, error: error });
})
.then(
function() {
this.handleQueue();
}.bind(this)
);
.then(() => this.handleQueue());
},
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rediff",
"version": "1.0.0",
"version": "1.0.1",
"description": "Perceptual difference testing tool based on Pediff",
"author": "Lukasz Blacha <[email protected]>",
"contributors": [
Expand Down

0 comments on commit d03ce29

Please sign in to comment.