import fs from 'fs/promises'; import path from 'path'; import { spawn } from 'child_process'; import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); /** * @param {import('child_process').ChildProcessWithoutNullStreams} proc * @returns {Promise} */ const waitStdout = (proc) => { return new Promise((resolve) => { proc.stdout.on('data', (data) => { resolve(data.toString()); }); }); }; /** * @param {import('child_process').ChildProcessWithoutNullStreams} proc * @returns {Promise} */ const waitClose = (proc) => { return new Promise((resolve) => { proc.on('close', () => { resolve(); }); }); }; test('shows plugins when flag specified', async () => { const proc = spawn( 'node', ['../../bin/svgo', '--no-color', '--show-plugins'], { cwd: __dirname }, ); const stdout = await waitStdout(proc); expect(stdout).toMatch(/Currently available plugins:/); }); test('accepts svg as input stream', async () => { const proc = spawn('node', ['../../bin/svgo', '--no-color', '-'], { cwd: __dirname, }); proc.stdin.write('Created with Love'); proc.stdin.end(); const stdout = await waitStdout(proc); expect(stdout).toBe(''); }); test('accepts svg as string', async () => { const input = 'Created with Love'; const proc = spawn( 'node', ['../../bin/svgo', '--no-color', '--string', input], { cwd: __dirname }, ); const stdout = await waitStdout(proc); expect(stdout).toBe(''); }); test('accepts svg as filename', async () => { const proc = spawn( 'node', ['../../bin/svgo', '--no-color', 'single.svg', '-o', 'output/single.svg'], { cwd: __dirname }, ); await waitClose(proc); const output = await fs.readFile( path.join(__dirname, 'output/single.svg'), 'utf-8', ); expect(output).toBe(''); }); test('output as stream when "-" is specified', async () => { const proc = spawn( 'node', ['../../bin/svgo', '--no-color', 'single.svg', '-o', '-'], { cwd: __dirname }, ); const stdout = await waitStdout(proc); expect(stdout).toBe(''); }); test('should exit with 1 code on syntax error', async () => { const proc = spawn('node', ['../../bin/svgo', '--no-color', 'invalid.svg'], { cwd: __dirname, }); const [code, stderr] = await Promise.all([ new Promise((resolve) => { proc.on('close', (code) => { resolve(code); }); }), new Promise((resolve) => { proc.stderr.on('data', (error) => { resolve(error.toString()); }); }), ]); expect(code).toBe(1); expect(stderr) .toBe(`SvgoParserError: invalid.svg:2:27: Unquoted attribute value 1 | > 2 | | ^ 3 | 4 | `); });