|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import nock from 'nock'; |
| 4 | +import pathExists from 'path-exists'; |
| 5 | +import pify from 'pify'; |
| 6 | +import rimraf from 'rimraf'; |
| 7 | +import test from 'ava'; |
| 8 | +import tempfile from 'tempfile'; |
| 9 | +import Fn from './'; |
| 10 | + |
| 11 | +const fsP = pify(fs); |
| 12 | +const rimrafP = pify(rimraf); |
| 13 | +const fixture = path.join.bind(path, __dirname, 'fixtures'); |
| 14 | + |
| 15 | +test.beforeEach(() => { |
| 16 | + nock('http://foo.com') |
| 17 | + .get('/gifsicle.tar.gz') |
| 18 | + .replyWithFile(200, fixture('gifsicle-' + process.platform + '.tar.gz')) |
| 19 | + .get('/gifsicle-darwin.tar.gz') |
| 20 | + .replyWithFile(200, fixture('gifsicle-darwin.tar.gz')) |
| 21 | + .get('/gifsicle-win32.tar.gz') |
| 22 | + .replyWithFile(200, fixture('gifsicle-win32.tar.gz')) |
| 23 | + .get('/test.js') |
| 24 | + .replyWithFile(200, __filename); |
| 25 | +}); |
| 26 | + |
| 27 | +test('expose a constructor', t => { |
| 28 | + t.is(typeof Fn, 'function'); |
| 29 | +}); |
| 30 | + |
| 31 | +test('add a source', t => { |
| 32 | + const bin = new Fn().src('http://foo.com/bar.tar.gz'); |
| 33 | + t.is(bin._src[0].url, 'http://foo.com/bar.tar.gz'); |
| 34 | +}); |
| 35 | + |
| 36 | +test('add a source to a specific os', t => { |
| 37 | + const bin = new Fn().src('http://foo.com', process.platform); |
| 38 | + t.is(bin._src[0].os, process.platform); |
| 39 | +}); |
| 40 | + |
| 41 | +test('set destination directory', t => { |
| 42 | + const bin = new Fn().dest(path.join(__dirname, 'foo')); |
| 43 | + t.is(bin._dest, path.join(__dirname, 'foo')); |
| 44 | +}); |
| 45 | + |
| 46 | +test('set which file to use as the binary', t => { |
| 47 | + const bin = new Fn().use('foo'); |
| 48 | + t.is(bin._use, 'foo'); |
| 49 | +}); |
| 50 | + |
| 51 | +test('set a version range to test against', t => { |
| 52 | + const bin = new Fn().version('1.0.0'); |
| 53 | + t.is(bin._version, '1.0.0'); |
| 54 | +}); |
| 55 | + |
| 56 | +test('get the binary path', t => { |
| 57 | + const bin = new Fn() |
| 58 | + .dest('tmp') |
| 59 | + .use('foo'); |
| 60 | + |
| 61 | + t.is(bin.path(), path.join('tmp', 'foo')); |
| 62 | +}); |
| 63 | + |
| 64 | +test('verify that a binary is working', async t => { |
| 65 | + const bin = new Fn() |
| 66 | + .src('http://foo.com/gifsicle.tar.gz') |
| 67 | + .dest(tempfile()) |
| 68 | + .use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle'); |
| 69 | + |
| 70 | + await pify(bin.run.bind(bin))(); |
| 71 | + t.true(await pathExists(bin.path())); |
| 72 | + await rimrafP(bin.dest()); |
| 73 | +}); |
| 74 | + |
| 75 | +test('meet the desired version', async t => { |
| 76 | + const bin = new Fn() |
| 77 | + .src('http://foo.com/gifsicle.tar.gz') |
| 78 | + .dest(tempfile()) |
| 79 | + .use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle') |
| 80 | + .version('>=1.71'); |
| 81 | + |
| 82 | + await pify(bin.run.bind(bin))(); |
| 83 | + t.true(await pathExists(bin.path())); |
| 84 | + await rimrafP(bin.dest()); |
| 85 | +}); |
| 86 | + |
| 87 | +test('download files even if they are not used', async t => { |
| 88 | + const bin = new Fn({strip: 0, skipCheck: true}) |
| 89 | + .src('http://foo.com/gifsicle-darwin.tar.gz') |
| 90 | + .src('http://foo.com/gifsicle-win32.tar.gz') |
| 91 | + .src('http://foo.com/test.js') |
| 92 | + .dest(tempfile()) |
| 93 | + .use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle'); |
| 94 | + |
| 95 | + await pify(bin.run.bind(bin))(); |
| 96 | + const files = await fsP.readdirSync(bin.dest()); |
| 97 | + |
| 98 | + t.is(files.length, 3); |
| 99 | + t.is(files[0], 'gifsicle'); |
| 100 | + t.is(files[1], 'gifsicle.exe'); |
| 101 | + t.is(files[2], 'test.js'); |
| 102 | + |
| 103 | + await rimrafP(bin.dest()); |
| 104 | +}); |
| 105 | + |
| 106 | +test('skip running binary check', async t => { |
| 107 | + const bin = new Fn({skipCheck: true}) |
| 108 | + .src('http://foo.com/gifsicle.tar.gz') |
| 109 | + .dest(tempfile()) |
| 110 | + .use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle'); |
| 111 | + |
| 112 | + await pify(bin.run.bind(bin))(['--shouldNotFailAnyway']); |
| 113 | + t.true(await pathExists(bin.path())); |
| 114 | + await rimrafP(bin.dest()); |
| 115 | +}); |
| 116 | + |
| 117 | +test('error if no binary is found and no source is provided', t => { |
| 118 | + const bin = new Fn() |
| 119 | + .dest(tempfile()) |
| 120 | + .use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle'); |
| 121 | + |
| 122 | + t.throws(pify(bin.run.bind(bin))(), 'No binary found matching your system. It\'s probably not supported.'); |
| 123 | +}); |
0 commit comments