forked from kevva/bin-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
157 lines (129 loc) · 4.36 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import fs, {promises as fsP} from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import isexe from 'isexe';
import nock from 'nock';
import {pathExists} from 'path-exists';
import {temporaryDirectory} from 'tempy';
import test from 'ava';
import BinWrapper from './index.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const fixture = path.join.bind(path, __dirname, 'fixtures');
const binary = process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle';
const removeDirs = dirs => Promise.all(
dirs.map(dir => fsP.rm(dir, {force: true, recursive: true})),
);
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 BinWrapper, 'function');
});
test('add a source', t => {
const bin = new BinWrapper().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 BinWrapper().src('http://foo.com', process.platform);
t.is(bin._src[0].os, process.platform);
});
test('set destination directory', t => {
const bin = new BinWrapper().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 BinWrapper().use('foo');
t.is(bin._use, 'foo');
});
test('set a version range to test against', t => {
const bin = new BinWrapper().version('1.0.0');
t.is(bin._version, '1.0.0');
});
test('get the binary path', t => {
const bin = new BinWrapper()
.dest('tmp')
.use('foo');
t.is(bin.path(), path.join('tmp', 'foo'));
});
test('verify that a binary is working', async t => {
const temporaryDir = temporaryDirectory();
const bin = new BinWrapper()
.src('http://foo.com/gifsicle.tar.gz')
.dest(temporaryDir)
.use(binary);
await bin.run();
t.true(await pathExists(bin.path()));
await removeDirs([bin.dest(), temporaryDir]);
});
test('meet the desired version', async t => {
const temporaryDir = temporaryDirectory();
const bin = new BinWrapper()
.src('http://foo.com/gifsicle.tar.gz')
.dest(temporaryDir)
.use(binary)
.version('>=1.71');
await bin.run();
t.true(await pathExists(bin.path()));
await removeDirs([bin.dest(), temporaryDir]);
});
test('download files even if they are not used', async t => {
const temporaryDir = temporaryDirectory();
const bin = new BinWrapper({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(temporaryDir)
.use(binary);
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 removeDirs([bin.dest(), temporaryDir]);
});
test('skip running binary check', async t => {
const temporaryDir = temporaryDirectory();
const bin = new BinWrapper({skipCheck: true})
.src('http://foo.com/gifsicle.tar.gz')
.dest(temporaryDir)
.use(binary);
await bin.run(['--shouldNotFailAnyway']);
t.true(await pathExists(bin.path()));
await removeDirs([bin.dest(), temporaryDir]);
});
test('error if no binary is found and no source is provided', async t => {
const temporaryDir = temporaryDirectory();
const bin = new BinWrapper()
.dest(temporaryDir)
.use(binary);
await t.throwsAsync(
bin.run(),
undefined,
'No binary found matching your system. It\'s probably not supported.',
);
await removeDirs([temporaryDir]);
});
test('downloaded files are set to be executable', async t => {
const temporaryDir = temporaryDirectory();
const bin = new BinWrapper({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(temporaryDir)
.use(binary);
await bin.run();
const files = fs.readdirSync(bin.dest());
await t.true(files.every(async file => isexe(path.join(bin.dest(), file))));
await removeDirs([temporaryDir]);
});