Commit 0a693fb2 authored by Fred Chasen's avatar Fred Chasen

More tests for Renderer

parent b13155f8
const gulp = require('gulp');
const babel = require('gulp-babel');
const plumber = require('gulp-plumber');
gulp.task('build', () => {
return gulp.src('src/*.js')
.pipe(plumber())
.pipe(babel({
presets: ['es2015'],
"plugins": [
......@@ -17,4 +19,4 @@ gulp.task('watch', () => {
gulp.watch('src/*.js', ['build']);
});
gulp.task('default', ['build']);
\ No newline at end of file
gulp.task('default', ['build']);
......@@ -26,7 +26,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
var log = (0, _debug2.default)(_package.name + ':renderer');
var MICRON_TO_MM = 1000;
var MM_TO_MICRON = 1000;
function Renderer(options) {
......@@ -51,7 +51,8 @@ Renderer.prototype.determineSettings = function (_options) {
height: options.height,
debug: options.debug || false,
timeout: options.timeout || 60000 * 5,
strategy: options.strategy || 'dedicated-process'
strategy: options.strategy || 'dedicated-process',
keepAlive: options.keepAlive || false
};
};
......@@ -59,16 +60,30 @@ Renderer.prototype.process = function (inputPath, outputPath, options) {
var _this = this;
var settings = options ? this.determineSettings(options) : this.settings;
var exists = this.fileExists(inputPath);
var result;
if (!exists) {
log("Input cannot be found: %s", inputPath);
if (settings.keepAlive != true) {
this.converter.kill();
}
return new Promise(function (resolve, reject) {
reject(err);
});
}
log("Processing from input %s", inputPath);
var result = this.convert(inputPath, settings);
result = this.convert(inputPath, settings);
result.then(function (result) {
log("Printed %s pages to %s", result.numberOfPages, outputPath);
_this.converter.kill();
if (settings.keepAlive != true) {
_this.converter.kill();
}
if (outputPath) {
_this.output(result.stream, outputPath);
......@@ -77,12 +92,23 @@ Renderer.prototype.process = function (inputPath, outputPath, options) {
log('An error has ocurred in in converting [%s]: %s', inputPath, err.message, err.stack);
_this.converter.kill();
if (settings.keepAlive != true) {
_this.converter.kill();
}
});
return result;
};
Renderer.prototype.fileExists = function (inputPath) {
try {
_fs2.default.accessSync(inputPath, _fs2.default.F_OK);
return true;
} catch (err) {
return false;
}
};
Renderer.prototype.getRendererPath = function () {
var directoryPath = __dirname.replace('lib', '');
return directoryPath + 'renderer/';
......@@ -106,10 +132,11 @@ Renderer.prototype.encodeUrl = function (inputPath) {
Renderer.prototype.settingsToUriComponent = function (settings) {
var url = '';
// ignore page size if width & height are present
if (settings.width && settings.height) {
url += '&width=' + settings.width;
url += '&height=' + settings.height;
} else {
} else if (settings.pageSize) {
url += '&pageSize=' + settings.pageSize;
}
......@@ -129,8 +156,8 @@ Renderer.prototype.getSize = function (settings) {
if (settings.width && settings.height) {
size = {
width: parseFloat(settings.width) * MICRON_TO_MM,
height: parseFloat(settings.height) * MICRON_TO_MM
width: parseFloat(settings.width) * MM_TO_MICRON,
height: parseFloat(settings.height) * MM_TO_MICRON
};
}
......
......@@ -6,7 +6,7 @@ import { name as pkgName } from '../package.json';
const log = debug(`${pkgName}:renderer`);
const MICRON_TO_MM = 1000;
const MM_TO_MICRON = 1000;
function Renderer(options) {
......@@ -32,22 +32,37 @@ Renderer.prototype.determineSettings = function (_options) {
height: options.height,
debug: options.debug || false,
timeout: options.timeout || 60000 * 5,
strategy: options.strategy || 'dedicated-process'
strategy: options.strategy || 'dedicated-process',
keepAlive: options.keepAlive || false
};
};
Renderer.prototype.process = function (inputPath, outputPath, options) {
var settings = options ? this.determineSettings(options) : this.settings;
var exists = this.fileExists(inputPath);
var result;
if (!exists) {
log("Input cannot be found: %s", inputPath);
if (settings.keepAlive != true) {
this.converter.kill();
}
return new Promise(function(resolve, reject) {
reject(err);
});
}
log("Processing from input %s", inputPath);
var result = this.convert(inputPath, settings)
result = this.convert(inputPath, settings);
result.then((result) => {
log("Printed %s pages to %s", result.numberOfPages, outputPath);
this.converter.kill();
if (settings.keepAlive != true) {
this.converter.kill();
}
if (outputPath) {
this.output(result.stream, outputPath);
......@@ -57,12 +72,24 @@ Renderer.prototype.process = function (inputPath, outputPath, options) {
log('An error has ocurred in in converting [%s]: %s', inputPath, err.message, err.stack);
this.converter.kill();
if (settings.keepAlive != true) {
this.converter.kill();
}
});
return result;
};
Renderer.prototype.fileExists = function (inputPath) {
try {
fs.accessSync(inputPath, fs.F_OK);
return true;
} catch (err) {
return false;
}
};
Renderer.prototype.getRendererPath = function () {
var directoryPath = __dirname.replace('lib', '');
return directoryPath + 'renderer/';
......@@ -87,11 +114,11 @@ Renderer.prototype.encodeUrl = function (inputPath) {
Renderer.prototype.settingsToUriComponent = function (settings) {
var url = '';
// ignore page size if width & height are present
if (settings.width && settings.height) {
url += '&width=' + settings.width;
url += '&height=' + settings.height;
} else {
} else if (settings.pageSize) {
url += '&pageSize=' + settings.pageSize;
}
......@@ -111,8 +138,8 @@ Renderer.prototype.getSize = function(settings) {
if (settings.width && settings.height) {
size = {
width: parseFloat(settings.width) * MICRON_TO_MM,
height: parseFloat(settings.height) * MICRON_TO_MM
width: parseFloat(settings.width) * MM_TO_MICRON,
height: parseFloat(settings.height) * MM_TO_MICRON
};
}
......
......@@ -87,14 +87,14 @@ describe('CLI', function() {
cli.on('close', function(code){
fs.access("./test/output/wood.pdf", fs.F_OK, function(err) {
if (err) {
exists = false
} else {
exists = true;
}
assert.equal(exists, true);
done();
if (err) {
exists = false;
} else {
exists = true;
}
assert.equal(exists, true);
done();
});
});
......
var assert = require('assert');
var Renderer = require("../lib/index.js");
var fs = require('fs');
describe('Renderer', function() {
......@@ -10,10 +11,251 @@ describe('Renderer', function() {
});
});
describe('#determineSettings', function() {
var renderer = new Renderer();
it('should return defaults', function() {
var settings = renderer.determineSettings();
assert.deepEqual(settings, {
pageSize: 'A4',
pageMargin: 0,
landscape: false,
width: undefined,
height: undefined,
debug: false,
timeout: 60000 * 5,
strategy: 'dedicated-process',
keepAlive: false
});
});
it('should overrided defaults', function() {
var options = {
pageSize: 'letter',
pageMargin: 10,
landscape: true,
width: 100,
height: 200,
debug: true,
timeout: 1,
strategy: 'electron-ipc',
keepAlive: true
};
var settings = renderer.determineSettings(options);
assert.deepEqual(settings, options);
});
});
describe('#getRendererPath', function() {
var renderer = new Renderer();
it('should return the path to the renderer html', function() {
var path = renderer.getRendererPath();
assert.equal(path, __dirname.replace("test", "renderer/"));
});
});
describe('#getRelativePath', function() {
var renderer = new Renderer();
it('should resolve with a relative path', function() {
var path = renderer.getRelativePath(__dirname, "./test/x/y/z/test.html");
assert.equal(path, "x/y/z/test.html");
});
it('should resolve with a relative path up a lever', function() {
var path = renderer.getRelativePath(__dirname, "../test.html");
assert.equal(path, "../../test.html");
});
});
describe('#encodeUrl', function() {
var renderer = new Renderer();
it('should encode a url', function() {
var inputPath = "./test/samples/wood/index.html";
var path = renderer.encodeUrl(inputPath);
assert.equal(path, "file://" + __dirname.replace("test", "renderer") + "/renderer.html?url=..%2Ftest%2Fsamples%2Fwood%2Findex.html");
});
});
describe('#settingsToUriComponent', function() {
var renderer = new Renderer();
it('should allow for empty settings', function() {
var component = renderer.settingsToUriComponent({});
assert.equal(component, "");
});
it('should turn width & height into a url component', function() {
var settings = {
width: 100,
height: 600
};
var component = renderer.settingsToUriComponent(settings);
assert.equal(component, "&width=100&height=600");
});
it('should set pageSize', function() {
var settings = {
pageSize: "letter"
};
var component = renderer.settingsToUriComponent(settings);
assert.equal(component, "&pageSize=letter");
});
it('should set landscape', function() {
var settings = {
landscape: true
};
var component = renderer.settingsToUriComponent(settings);
assert.equal(component, "&landscape=true");
});
it('should set debug', function() {
var settings = {
debug: true
};
var component = renderer.settingsToUriComponent(settings);
assert.equal(component, "&debug=true");
});
it('should ignore page size if width & height are present', function() {
var settings = {
width: 100,
height: 600,
pageSize: "letter",
landscape: true,
debug: true
};
var component = renderer.settingsToUriComponent(settings);
assert.equal(component, "&width=100&height=600&landscape=true&debug=true");
});
});
describe('#getSize', function() {
var renderer = new Renderer();
it('should return width and height in microns from settings', function() {
var settings = {
width: 100,
height: 600,
pageSize: "letter"
};
var component = renderer.getSize(settings);
assert.deepEqual(component, { width: 100000, height: 600000});
});
it('should return pageSize from settings if no width and height', function() {
var settings = {
pageSize: "letter"
};
var component = renderer.getSize(settings);
assert.equal(component, "letter");
});
});
describe('#fileExists', function() {
var renderer = new Renderer();
it('should find a valid file', function() {
var inputPath = "./test/samples/wood/index.html";
var exists = renderer.fileExists(inputPath);
assert(exists, "File was found");
});
it('should not find an invalid file', function() {
var inputPath = "./test/samples/wood/fake.html";
var exists = renderer.fileExists(inputPath);
assert(exists === false, "File was not found");
});
});
describe('#convert', function() {
this.timeout(15000);
var renderer = new Renderer();
it('should return a result with stream & page numberOfPages', function() {
var inputPath = "./test/samples/wood/index.html";
return renderer.convert(inputPath, {})
.then(function(result){
assert(result != undefined, "Result should exist.");
assert.equal(result.numberOfPages, 5);
assert(result.stream != undefined, "Stream should exist.");
});
});
});
describe('#process', function() {
xit('process input path', function() {
this.timeout(15000);
var renderer = new Renderer({keepAlive: true});
it('should process a valid inputPath', function() {
var inputPath = "./test/samples/wood/index.html";
return renderer.process(inputPath, null)
.then(function(result){
assert(result != undefined, "Result should exist.");
assert.equal(result.numberOfPages, 5);
assert(result.stream != undefined, "Stream should exist.");
});
});
it('should reject an invalid inputPath', function() {
var inputPath = "./test/samples/wood/fake.html";
return renderer.process(inputPath, null)
.then(function(result){
assert(false, "Should not return successfully");
}, function(err) {
assert(err != null, "Should return error");
});
});
it('should output to the provided path', function(done) {
var inputPath = "./test/samples/wood/index.html";
var outputPath = "./test/output/process_test.pdf";
renderer.process(inputPath, outputPath)
.then(function(result){
fs.access(outputPath, fs.F_OK, function(err) {
var exists = false;
if (err) {
exists = false;
} else {
exists = true;
}
assert(exists, "File exists");
done();
});
}).catch(function(err) {
console.error(err);
assert.equal(exists, undefined);
done();
});
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment