diff --git a/src/tests/cli.test.ts b/src/tests/cli.test.ts index d593636..54b8394 100644 --- a/src/tests/cli.test.ts +++ b/src/tests/cli.test.ts @@ -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' }, ), ); @@ -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' }, ), ); diff --git a/src/tests/filesorter.test.ts b/src/tests/filesorter.test.ts index be1f15d..6ff28ba 100644 --- a/src/tests/filesorter.test.ts +++ b/src/tests/filesorter.test.ts @@ -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', () => { diff --git a/src/tests/index.test.ts b/src/tests/index.test.ts index ca09149..5fa3911 100755 --- a/src/tests/index.test.ts +++ b/src/tests/index.test.ts @@ -2,7 +2,8 @@ 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'; @@ -10,13 +11,19 @@ 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`, @@ -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`, @@ -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, }, @@ -75,7 +82,7 @@ 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, }, @@ -83,7 +90,7 @@ async function generateFontToMemory(options, files?, startUnicode?) { expect((await promise).toString()).toEqual( fs.readFileSync( - path.join('./src/tests', 'expected', `${options.fontName}.svg`), + join('./src/tests', 'expected', `${options.fontName}.svg`), { encoding: 'utf8' }, ), ); @@ -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 = { @@ -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 = { @@ -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 = { @@ -578,10 +583,9 @@ 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', + }), ); }); }); @@ -589,7 +593,7 @@ describe('Passing code points', () => { 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 = { @@ -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 = { @@ -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 = { @@ -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, @@ -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 }); @@ -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 = { diff --git a/src/tests/metadata.test.ts b/src/tests/metadata.test.ts index de008d4..89c4758 100644 --- a/src/tests/metadata.test.ts +++ b/src/tests/metadata.test.ts @@ -1,9 +1,16 @@ import { describe, test, expect } from '@jest/globals'; import { writeFile, readFile, unlink } from 'node:fs/promises'; -import path from 'node:path'; import { promisify } from 'node:util'; import metadata from '../metadata.js'; import { YError } from 'yerror'; +import { mkdir } from 'node:fs/promises'; +import { join } from 'node:path'; + +try { + await mkdir(join('src', 'tests', 'results')); +} catch (err) { + // empty +} describe('Metadata service', () => { describe('for code generation', () => { @@ -29,28 +36,26 @@ describe('Metadata service', () => { }); await writeFile( - path.join('.', 'src', 'tests', 'results', 'plop.svg'), + join('.', 'src', 'tests', 'results', 'plop.svg'), 'plop', 'utf-8', ); const infos = await promisify(metadataService)( - path.join('.', 'src', 'tests', 'results', 'plop.svg'), + join('.', 'src', 'tests', 'results', 'plop.svg'), ); expect(infos).toEqual({ - path: path.join('.', 'src', 'tests', 'results', 'uEA01-plop.svg'), + path: join('.', 'src', 'tests', 'results', 'uEA01-plop.svg'), name: 'plop', unicode: [String.fromCharCode(0xea01)], renamed: true, }); expect( - await readFile( - path.join('.', 'src', 'tests', 'results', 'uEA01-plop.svg'), - ), + await readFile(join('.', 'src', 'tests', 'results', 'uEA01-plop.svg')), ).toBeTruthy(); - unlink(path.join('.', 'src', 'tests', 'results', 'uEA01-plop.svg')); + unlink(join('.', 'src', 'tests', 'results', 'uEA01-plop.svg')); try { - await readFile(path.join('.', 'src', 'tests', 'results', 'plop.svg')); + await readFile(join('.', 'src', 'tests', 'results', 'plop.svg')); throw new YError('E_UNEXPECTED_SUCCESS'); } catch (err) { expect((err as YError).code === 'E_UNEXPECTED_SUCCESS').toBeFalsy(); @@ -69,7 +74,7 @@ describe('Metadata service', () => { try { await promisify(metadataService)( - path.join('.', 'src', 'tests', 'results', 'plop.svg'), + join('.', 'src', 'tests', 'results', 'plop.svg'), ); throw new YError('E_UNEXPECTED_SUCCESS'); @@ -78,9 +83,7 @@ describe('Metadata service', () => { expect((err as YError).code === 'E_UNEXPECTED_SUCCESS').toBeFalsy(); } try { - await readFile( - path.join('.', 'src', 'tests', 'results', 'uEA02-plop.svg'), - ); + await readFile(join('.', 'src', 'tests', 'results', 'uEA02-plop.svg')); throw new YError('E_UNEXPECTED_SUCCESS'); } catch (err) { expect((err as YError).code === 'E_UNEXPECTED_SUCCESS').toBeFalsy();