Skip to content

Commit

Permalink
fix(tests): fix ci tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Jul 29, 2024
1 parent e84a5ce commit 9c4152f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 79 deletions.
74 changes: 37 additions & 37 deletions src/tests/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import { describe, test, expect } from '@jest/globals';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { readFile, mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { exec } from 'node:child_process';
import { promisify } from 'node:util';

try {
await mkdir(join('src', 'tests', 'results'));
} catch (err) {
// empty
}

describe('Testing CLI', () => {
test('should work for simple SVG', async () => {
const command =
`${'node' + ' '}${path.join('bin', 'svgicons2svgfont.js')} -o ${path.join(
`${'node' + ' '}${join('bin', 'svgicons2svgfont.js')} -o ${join(
'src',
'tests',
'results',
'originalicons-cli.svg',
)} -s 0xE001` +
` ${path.join('src', 'tests', 'fixtures', 'originalicons', '*.svg')}`;
` ${join('src', 'tests', 'fixtures', 'originalicons', '*.svg')}`;

await promisify(exec)(command);

expect(
await readFile(
path.join('src', 'tests', 'results', 'originalicons-cli.svg'),
{ encoding: 'utf8' },
),
await readFile(join('src', 'tests', 'results', 'originalicons-cli.svg'), {
encoding: 'utf8',
}),
).toEqual(
await readFile(
path.join('src', 'tests', 'expected', 'originalicons-cli.svg'),
join('src', 'tests', 'expected', 'originalicons-cli.svg'),
{ encoding: 'utf8' },
),
);
Expand All @@ -34,67 +39,62 @@ describe('Testing CLI', () => {
const command =
'node' +
' ' +
path.join('bin', 'svgicons2svgfont.js') +
join('bin', 'svgicons2svgfont.js') +
' -o ' +
path.join('src', 'tests', 'results', 'lotoficons-cli.svg') +
join('src', 'tests', 'results', 'lotoficons-cli.svg') +
' -s 0xE001' +
' -r 1e4' +
' ' +
path.join('src', 'tests', 'fixtures', 'cleanicons', '*.svg') +
join('src', 'tests', 'fixtures', 'cleanicons', '*.svg') +
' ' +
path.join('src', 'tests', 'fixtures', 'hiddenpathesicons', '*.svg') +
join('src', 'tests', 'fixtures', 'hiddenpathesicons', '*.svg') +
' ' +
path.join('src', 'tests', 'fixtures', 'multipathicons', 'kikoolol.svg') +
join('src', 'tests', 'fixtures', 'multipathicons', 'kikoolol.svg') +
' ' +
path.join('src', 'tests', 'fixtures', 'originalicons', '*.svg') +
join('src', 'tests', 'fixtures', 'originalicons', '*.svg') +
' ' +
path.join('src', 'tests', 'fixtures', 'realicons', '*.svg') +
join('src', 'tests', 'fixtures', 'realicons', '*.svg') +
' ' +
path.join('src', 'tests', 'fixtures', 'roundedcorners', '*.svg') +
join('src', 'tests', 'fixtures', 'roundedcorners', '*.svg') +
' ' +
path.join('src', 'tests', 'fixtures', 'shapeicons', '*.svg') +
join('src', 'tests', 'fixtures', 'shapeicons', '*.svg') +
' ' +
path.join('src', 'tests', 'fixtures', 'tocentericons', '*.svg');
join('src', 'tests', 'fixtures', 'tocentericons', '*.svg');

await promisify(exec)(command);

expect(
await readFile(
path.join('src', 'tests', 'results', 'lotoficons-cli.svg'),
{
encoding: 'utf8',
},
),
await readFile(join('src', 'tests', 'results', 'lotoficons-cli.svg'), {
encoding: 'utf8',
}),
).toEqual(
await readFile(
path.join('src', 'tests', 'expected', 'lotoficons-cli.svg'),
{ encoding: 'utf8' },
),
await readFile(join('src', 'tests', 'expected', 'lotoficons-cli.svg'), {
encoding: 'utf8',
}),
);
});

describe('with nested icons', () => {
test('should work', async () => {
const command = `${'node' + ' '}${path.join(
const command = `${'node' + ' '}${join(
'bin',
'svgicons2svgfont.js',
)} -o ${path.join(
)} -o ${join(
'src',
'tests',
'results',
'nestedicons-cli.svg',
)} ${path.join('src', 'tests', 'fixtures', 'nestedicons', '*.svg')}`;
)} ${join('src', 'tests', 'fixtures', 'nestedicons', '*.svg')}`;

await promisify(exec)(command);

expect(
await readFile(
path.join('src', 'tests', 'results', 'nestedicons-cli.svg'),
{ encoding: 'utf8' },
),
await readFile(join('src', 'tests', 'results', 'nestedicons-cli.svg'), {
encoding: 'utf8',
}),
).toEqual(
await readFile(
path.join('src', 'tests', 'expected', 'nestedicons-cli.svg'),
join('src', 'tests', 'expected', 'nestedicons-cli.svg'),
{ encoding: 'utf8' },
),
);
Expand Down
8 changes: 8 additions & 0 deletions src/tests/filesorter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { describe, test, expect } from '@jest/globals';
import { fileSorter } from '../filesorter.js';
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';

try {
await mkdir(join('src', 'tests', 'results'));
} catch (err) {
// empty
}

describe('fileSorter', () => {
test('should sort files per filename', () => {
Expand Down
62 changes: 33 additions & 29 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ import { describe, test, expect } from '@jest/globals';
import assert from 'assert';
import { Readable } from 'node:stream';
import fs from 'node:fs';
import path from 'node:path';
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { ucs2 as ucs2 } from 'punycode';

import SVGIcons2SVGFontStream from '../index.js';
import SVGIconsDirStream, { type SVGIconStream } from '../iconsdir.js';
import streamtest from 'streamtest';
import { BufferStream } from 'bufferstreams';

try {
await mkdir(join('src', 'tests', 'results'));
} catch (err) {
// empty
}

const codepoint = JSON.parse(
fs.readFileSync('./src/tests/expected/test-codepoint.json').toString(),
);

// Helpers
async function generateFontToFile(options, fileSuffix?, startUnicode?, files?) {
const dest = path.join(
const dest = join(
'./src/tests',
'results',
`${options.fontName + (fileSuffix || '')}.svg`,
Expand All @@ -37,7 +44,7 @@ async function generateFontToFile(options, fileSuffix?, startUnicode?, files?) {
try {
expect(fs.readFileSync(dest, { encoding: 'utf8' })).toEqual(
fs.readFileSync(
path.join(
join(
'./src/tests',
'expected',
`${options.fontName + (fileSuffix || '')}.svg`,
Expand All @@ -52,7 +59,7 @@ async function generateFontToFile(options, fileSuffix?, startUnicode?, files?) {
});

new SVGIconsDirStream(
files || path.join('src', 'tests', 'fixtures', options.fontName),
files || join('src', 'tests', 'fixtures', options.fontName),
{
startUnicode: startUnicode || 0xe001,
},
Expand All @@ -75,15 +82,15 @@ async function generateFontToMemory(options, files?, startUnicode?) {
const promise = bufferStream(svgFontStream);

new SVGIconsDirStream(
files || path.join('./src/tests', 'fixtures', options.fontName),
files || join('./src/tests', 'fixtures', options.fontName),
{
startUnicode: startUnicode || 0xe001,
},
).pipe(svgFontStream);

expect((await promise).toString()).toEqual(
fs.readFileSync(
path.join('./src/tests', 'expected', `${options.fontName}.svg`),
join('./src/tests', 'expected', `${options.fontName}.svg`),
{ encoding: 'utf8' },
),
);
Expand Down Expand Up @@ -514,7 +521,7 @@ describe('Passing code points', () => {
test('should work with multiple unicode values for a single icon', async () => {
const svgFontStream = new SVGIcons2SVGFontStream({ round: 1e3 });
const svgIconStream = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;

svgIconStream.metadata = {
Expand All @@ -529,17 +536,16 @@ describe('Passing code points', () => {

assert.equal(
await promise,
fs.readFileSync(
path.join('./src/tests', 'expected', 'cleanicons-multi.svg'),
{ encoding: 'utf8' },
),
fs.readFileSync(join('./src/tests', 'expected', 'cleanicons-multi.svg'), {
encoding: 'utf8',
}),
);
});

test('should work with ligatures', async () => {
const svgFontStream = new SVGIcons2SVGFontStream({ round: 1e3 });
const svgIconStream = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;

svgIconStream.metadata = {
Expand All @@ -553,17 +559,16 @@ describe('Passing code points', () => {
svgFontStream.end();
assert.equal(
await promise,
fs.readFileSync(
path.join('./src/tests', 'expected', 'cleanicons-lig.svg'),
{ encoding: 'utf8' },
),
fs.readFileSync(join('./src/tests', 'expected', 'cleanicons-lig.svg'), {
encoding: 'utf8',
}),
);
});

test('should work with high code points', async () => {
const svgFontStream = new SVGIcons2SVGFontStream({ round: 1e3 });
const svgIconStream = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;

svgIconStream.metadata = {
Expand All @@ -578,18 +583,17 @@ describe('Passing code points', () => {

assert.equal(
(await promise).toString(),
fs.readFileSync(
path.join('src/tests', 'expected', 'cleanicons-high.svg'),
{ encoding: 'utf8' },
),
fs.readFileSync(join('src/tests', 'expected', 'cleanicons-high.svg'), {
encoding: 'utf8',
}),
);
});
});

describe('Providing bad glyphs', () => {
test('should fail when not providing glyph name', async () => {
const svgIconStream = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;

svgIconStream.metadata = {
Expand All @@ -609,7 +613,7 @@ describe('Providing bad glyphs', () => {

test('should fail when not providing codepoints', async () => {
const svgIconStream = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;

svgIconStream.metadata = {
Expand All @@ -629,7 +633,7 @@ describe('Providing bad glyphs', () => {

test('should fail when providing unicode value with duplicates', async () => {
const svgIconStream = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;

svgIconStream.metadata = {
Expand All @@ -649,10 +653,10 @@ describe('Providing bad glyphs', () => {

test('should fail when providing the same codepoint twice', async () => {
const svgIconStream = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
const svgIconStream2 = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
const svgFontStream = new SVGIcons2SVGFontStream({
round: 1e3,
Expand All @@ -679,10 +683,10 @@ describe('Providing bad glyphs', () => {

test('should fail when providing the same name twice', async () => {
const svgIconStream = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
const svgIconStream2 = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
join('./src/tests', 'fixtures', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
const svgFontStream = new SVGIcons2SVGFontStream({ round: 1e3 });

Expand All @@ -704,7 +708,7 @@ describe('Providing bad glyphs', () => {

test('should fail when providing bad pathdata', async () => {
const svgIconStream = fs.createReadStream(
path.join('./src/tests', 'fixtures', 'badicons', 'pathdata.svg'),
join('./src/tests', 'fixtures', 'badicons', 'pathdata.svg'),
) as unknown as SVGIconStream;

svgIconStream.metadata = {
Expand Down
Loading

0 comments on commit 9c4152f

Please sign in to comment.