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