'use strict'; /** * @typedef {import('child_process').ChildProcessWithoutNullStreams} ChildProcessWithoutNullStreams */ const fs = require('fs'); const path = require('path'); const { spawn } = require('child_process'); /** * @type {(proc: ChildProcessWithoutNullStreams) => Promise} */ const waitStdout = (proc) => { return new Promise((resolve) => { proc.stdout.on('data', (data) => { resolve(data.toString()); }); }); }; /** * @type {(proc: ChildProcessWithoutNullStreams) => 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('stdin'); proc.stdin.end(); const stdout = await waitStdout(proc); expect(stdout).toEqual(''); }); test('accepts svg as string', async () => { const input = 'string'; const proc = spawn( 'node', ['../../bin/svgo', '--no-color', '--string', input], { cwd: __dirname } ); const stdout = await waitStdout(proc); expect(stdout).toEqual(''); }); 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 = fs.readFileSync( path.join(__dirname, 'output/single.svg'), 'utf-8' ); expect(output).toEqual(''); }); 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).toEqual(''); }); 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).toEqual(1); expect(stderr) .toEqual(`SvgoParserError: invalid.svg:2:27: Unquoted attribute value 1 | > 2 | | ^ 3 | 4 | `); });