|
| 1 | +import test from 'tape'; |
| 2 | +import {execFile} from 'child_process'; |
| 3 | +import path from 'path'; |
| 4 | + |
| 5 | +Error.stackTraceLimit = Infinity; |
| 6 | + |
| 7 | +function exec (...args) { |
| 8 | + return new Promise((resolve, reject) => { |
| 9 | + execFile('node', [path.join(__dirname, '..', 'lib', 'cli.js'), '-0'].concat(args), { |
| 10 | + cwd: path.join(__dirname, 'resources') |
| 11 | + }, function(err, stdout, stderr) { |
| 12 | + if (err) { |
| 13 | + /* eslint-disable no-console */ |
| 14 | + console.error(stderr); |
| 15 | + /* eslint-enable */ |
| 16 | + return reject(err); |
| 17 | + } |
| 18 | + resolve(stdout); |
| 19 | + }); |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +function occurence(content) { |
| 24 | + let occ = {}; |
| 25 | + occ.import = content.match(/import\sReact/g); |
| 26 | + occ.export = content.match(/export\sdefault/g); |
| 27 | + occ.import = occ.import ? occ.import.length : 0; |
| 28 | + occ.export = occ.export ? occ.export.length : 0; |
| 29 | + return occ; |
| 30 | +} |
| 31 | + |
| 32 | +function testOccurence(t, content, n) { |
| 33 | + let o = occurence(content); |
| 34 | + t.equal(o.import, n); |
| 35 | + t.equal(o.export, n); |
| 36 | +} |
| 37 | + |
| 38 | +test('accept single argument', function(t) { |
| 39 | + exec('dummy.svg') |
| 40 | + .then(r => { |
| 41 | + testOccurence(t, r, 1); |
| 42 | + t.end(); |
| 43 | + }) |
| 44 | + .catch(t.end); |
| 45 | +}); |
| 46 | + |
| 47 | +test('accept multiple arguments', function(t) { |
| 48 | + exec('dummy.svg', 'dummy2.svg') |
| 49 | + .then(r => { |
| 50 | + testOccurence(t, r, 2); |
| 51 | + t.end(); |
| 52 | + }) |
| 53 | + .catch(t.end); |
| 54 | +}); |
| 55 | + |
| 56 | +test('es5 output', function (t) { |
| 57 | + exec('dummy.svg', '--es5') |
| 58 | + .then(r => { |
| 59 | + testOccurence(t, r, 0); |
| 60 | + t.end(); |
| 61 | + }) |
| 62 | + .catch(t.end); |
| 63 | +}); |
| 64 | + |
| 65 | +test('pass options to svgo', function(t) { |
| 66 | + Promise.all([ |
| 67 | + exec('dummy.svg'), |
| 68 | + exec('dummy.svg', '--svgo.js2svg.pretty'), |
| 69 | + exec('dummy2.svg', '--svgo.floatPrecision', '1'), |
| 70 | + exec('dummy2.svg', '--svgo.floatPrecision', '8') |
| 71 | + ]).then(r => { |
| 72 | + t.notEqual(r[0], r[1]); |
| 73 | + t.notEqual(r[2], r[3]); |
| 74 | + t.end(); |
| 75 | + }).catch(t.end); |
| 76 | +}); |
| 77 | + |
| 78 | +test('plugins options in svgo', function(t) { |
| 79 | + Promise.all([ |
| 80 | + exec('dummy.svg'), |
| 81 | + exec('dummy.svg', '--svgo.full') |
| 82 | + ]).then(r => { |
| 83 | + t.notEqual(r[0], r[1]); |
| 84 | + t.end(); |
| 85 | + }).catch(t.end); |
| 86 | +}); |
| 87 | + |
| 88 | +test('accepts yaml/json/js input', function(t) { |
| 89 | + Promise.all([ |
| 90 | + exec('dummy.svg', '--svgo', 'config.yaml'), |
| 91 | + exec('dummy.svg', '--svgo', 'config.json'), |
| 92 | + exec('dummy.svg', '--svgo', 'config.js') |
| 93 | + ]).then(r => { |
| 94 | + t.equal(r[0], r[1]); |
| 95 | + t.equal(r[1], r[2]); |
| 96 | + t.end(); |
| 97 | + }).catch(t.end); |
| 98 | +}); |
0 commit comments