From d99c305894f774c8be47ab8e4fc7f1ff3780033b Mon Sep 17 00:00:00 2001 From: Igor Anikin Date: Thu, 18 May 2017 12:12:50 +0300 Subject: [PATCH] Add tests and docs --- Gruntfile.js | 31 +++++++++++++++++++++++++++ Readme.md | 6 ++++++ package.json | 1 + tasks/engines/node.js | 2 +- tasks/webfont.js | 2 +- test/webfont_test.js | 50 ++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 89 insertions(+), 3 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index cf394fe..7e14e0b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -339,6 +339,37 @@ module.exports = function(grunt) { }] } }, + timestamp_same_1: { + src: 'test/src/*.svg', + dest: 'test/tmp/timestamp_same', + options: { + fontFilename: 'same_1', + timestamp: 0 + } + }, + timestamp_same_2: { + src: 'test/src/*.svg', + dest: 'test/tmp/timestamp_same', + options: { + fontFilename: 'same_2', + timestamp: 0 + } + }, + timestamp_different_1: { + src: 'test/src/*.svg', + dest: 'test/tmp/timestamp_different', + options: { + fontFilename: 'different_1', + timestamp: 0 + } + }, + timestamp_different_2: { + src: 'test/src/*.svg', + dest: 'test/tmp/timestamp_different', + options: { + fontFilename: 'different_2' + } + }, }, nodeunit: { all: ['test/webfont_test.js'] diff --git a/Readme.md b/Readme.md index a6dab8d..b003740 100644 --- a/Readme.md +++ b/Readme.md @@ -466,6 +466,12 @@ At compile-time each template will have access to the same context as the compil #### execMaxBuffer If you get stderr maxBuffer exceeded warning message, engine probably logged a lot of warning messages. To see this warnings run grunt in verbose mode `grunt --verbose`. To go over this warning you can try to increase buffer size by this option. Default value is `1024 * 200` +#### timestamp + +Type: `number` Default: `Date.now()` +Unix timestamp (in seconds) to override font creation time +NOTE: Work only with node engine + ### Config Examples #### Simple font generation diff --git a/package.json b/package.json index d1eb069..996d146 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "grunt-contrib-watch": "~0.6.1", "grunt-jscs": "~1.0.0", "load-grunt-tasks": "~3.4.0", + "promise": "^7.1.1", "stylus": "~0.53.0", "xml2js": "~0.4.16" }, diff --git a/tasks/engines/node.js b/tasks/engines/node.js index f232b96..39b6649 100644 --- a/tasks/engines/node.js +++ b/tasks/engines/node.js @@ -55,7 +55,7 @@ module.exports = function(o, allDone) { ttf: function(done) { getFont('svg', function(svgFont) { var font = svg2ttf(svgFont, { - ts: o.ts + ts: o.timestamp }); font = new Buffer(font.buffer); autohintTtfFont(font, function(hintedFont) { diff --git a/tasks/webfont.js b/tasks/webfont.js index ab4e97a..779a46d 100755 --- a/tasks/webfont.js +++ b/tasks/webfont.js @@ -117,7 +117,7 @@ module.exports = function(grunt) { callback: options.callback, customOutputs: options.customOutputs, execMaxBuffer: options.execMaxBuffer || 1024 * 200, - ts: (options.ts === 0) ? 0 : options.ts || Math.floor(Date.now() / 1000) + timestamp: (options.timestamp === 0) ? 0 : options.timestamp || Math.floor(Date.now() / 1000) }; o = _.extend(o, { diff --git a/test/webfont_test.js b/test/webfont_test.js index f918068..0475c62 100644 --- a/test/webfont_test.js +++ b/test/webfont_test.js @@ -6,6 +6,8 @@ var path = require('path'); var grunt = require('grunt'); var parseXMLString = require('xml2js').parseString; var wf = require('../tasks/util/util'); +var crypto = require('crypto'); +var Promise = require('promise'); function find(haystack, needle) { return haystack.indexOf(needle) !== -1; @@ -896,6 +898,52 @@ exports.webfont = { test.ok(fs.existsSync('test/tmp/custom_output/context-test.html')); test.done(); - } + }, + + // If font created multiple times with same value as timestamp option + // then md5 of created files must be the same too + timestamp_same: function (test) { + var promise_1 = new Promise(function(resolve, reject) { + fs.readFile('test/tmp/timestamp_same/same_1.ttf', function (err, data) { + resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex')); + }); + }); + + var promise_2 = new Promise(function(resolve, reject) { + fs.readFile('test/tmp/timestamp_same/same_2.ttf', function (err, data) { + resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex')); + }); + }); + + Promise.all([promise_1, promise_2]) + .then(function(data) { + // md5 must be the same + test.ok(data[0] === data[1]); + test.done(); + }); + }, + + // If font created multiple times with different value as timestamp option + // then md5 of created files must be different + timestamp_different: function (test) { + var promise_1 = new Promise(function(resolve, reject) { + fs.readFile('test/tmp/timestamp_different/different_1.ttf', function (err, data) { + resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex')); + }); + }); + + var promise_2 = new Promise(function(resolve, reject) { + fs.readFile('test/tmp/timestamp_different/different_2.ttf', function (err, data) { + resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex')); + }); + }); + + Promise.all([promise_1, promise_2]) + .then(function(data) { + // md5 must be the different + test.ok(data[0] !== data[1]); + test.done(); + }); + }, };