Skip to content
This repository has been archived by the owner on May 29, 2020. It is now read-only.

Commit

Permalink
Add tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Anikin committed May 18, 2017
1 parent 4ded02f commit d99c305
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
31 changes: 31 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion tasks/engines/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tasks/webfont.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
50 changes: 49 additions & 1 deletion test/webfont_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
},

};

0 comments on commit d99c305

Please sign in to comment.