Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(icons-to-woff.js): enforce deterministic output #64

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/icons-to-woff.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ module.exports = function createIconFont (fs, icons, options) {
name: options.name,
normalize: true,
fontHeight: options.enforcedSvgHeight ? options.enforcedSvgHeight : undefined,
log: function () {},
error: /** @param {any} err */function (err) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error property does not appear to be a supported option in svgicons2svgfont. I wasn't able to find a valid code path in that project that would call this function, so to support 100% test coverage, I opted to drop it here. The .on('error') callback on the stream below is sufficient for handling all errors raised by svgicons2svgfont.

reject(err);
}
log: function () {}
});
let svgFont = '';
fontStream
Expand Down Expand Up @@ -58,7 +55,7 @@ module.exports = function createIconFont (fs, icons, options) {
});
fontStream.end();
})
.then((svgFont) => svg2ttf(svgFont, {}).buffer)
.then((svgFont) => svg2ttf(svgFont, { ts: 0 }).buffer)
.then((ttfFont) => ttf2woff(ttfFont).buffer)
.then((woffFont) => Buffer.from(woffFont).toString('base64'));
};
1 change: 1 addition & 0 deletions test/fixtures/malformed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions test/icons-to-woff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';
/* eslint max-len: off, quotes:off, arrow-parens:off */
import createIconFont from '../lib/icons-to-woff.js';
import fs from 'fs';
import test from 'ava';

test('should throw an error when an svg cannot be found', async (t) => {
let error;
try {
await createIconFont(fs, [
'./test/fixtures/does-not-exist.svg'
], {
name: 'test'
});
} catch (e) {
error = e.message;
}

t.is(error.substring(0, 33), 'ENOENT: no such file or directory');
t.pass();
});

test('should throw an error when a malformed svg is provided', async (t) => {
let error;
try {
await createIconFont(fs, [
'./test/fixtures/malformed.svg'
], {
name: 'test'
});
} catch (e) {
error = e.message;
}

t.is(error.substring(0, 32), 'Non-whitespace before first tag.');
t.pass();
});

test('should produce the same output when receiving the same input and configuration', async (t) => {
const svgs = [
'./test/fixtures/account-494x512.svg',
'./test/fixtures/account-986x1024.svg'
];

const test1 = await createIconFont(fs, svgs, {
name: 'test'
});

await new Promise((resolve) => setTimeout(resolve, 1000));

const test2 = await createIconFont(fs, svgs, {
name: 'test'
});

t.is(test1, test2);
t.pass();
});

test('should produce different output when receiving the same input but different fontHeight', async (t) => {
const svgs = [
'./test/fixtures/account-494x512.svg',
'./test/fixtures/account-986x1024.svg'
];

const test1 = await createIconFont(fs, svgs, {
enforcedSvgHeight: 32
});

await new Promise((resolve) => setTimeout(resolve, 1000));

const test2 = await createIconFont(fs, svgs, {
enforcedSvgHeight: 16
});

t.not(test1, test2);
t.pass();
});