This repository has been archived by the owner on Feb 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
140 lines (115 loc) · 4.12 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 test from 'ava';
import path from 'path';
import fs from 'fs-extra';
import spawn from 'execa';
import { v4 } from 'uuid';
import proxyquire from 'proxyquire';
// Helper
// ---------------
const testDir = path.resolve(process.cwd(), '.tmp');
const getRandomDir = () => path.resolve(testDir, v4());
test.before('reset all the things', () => {
fs.removeSync(testDir);
});
// Setup
// ---------------
// Strict call throw so we can proxy the require(bootstrap)
proxyquire.noCallThru();
test.beforeEach(t => {
t.context.dir = getRandomDir();
t.context.yarnAvailable = true;
t.context.lastSpawn = null;
t.context.resolveSpawn = true;
t.context.bootstrapArgs = null;
const bootstrap = path.resolve(
t.context.dir,
'node_modules',
'melodrama-scripts',
'index.js'
);
// Stub in context so we can run tests in parallel.
t.context.lib = proxyquire('./', {
'command-exists': (cmd, cb) => cb(null, t.context.yarnAvailable),
'execa': (cmd, args) => {
t.context.lastSpawn = {cmd, args, stdout: null };
let promise = t.context.resolveSpawn ? Promise.resolve() : Promise.reject('whoops');
promise.stdout = { pipe: (buffer) => {t.context.lastSpawn.stdout = buffer;} };
return promise;
},
'ora': () => {
// Silence ora
return {
start: () => {},
fail: () => {},
succeed: () => {}
};
},
// We are not installing this module while testing.
[bootstrap]: function () { t.context.bootstrapArgs = arguments; }
});
});
// CLI
// ---------------
test('require input path', async t => {
const r = await spawn('node', ['cli.js', '--skip-install']);
t.is(r.code, 0);
});
// Lib
// ---------------
test('prepare directory', async t => {
await t.context.lib.prepareDirectory(t.context.dir);
const pkg = fs.readJsonSync(`${t.context.dir}/package.json`);
t.true(fs.existsSync(t.context.dir));
t.is(pkg.name, path.basename(t.context.dir));
t.is(typeof pkg.version, 'string');
t.is(pkg.private, true);
});
test('fail if directory has conflicted files', async t => {
fs.outputFileSync(path.join(t.context.dir, 'index.js'), 'console.log("nope");');
t.throws(t.context.lib.prepareDirectory(t.context.dir));
});
test('some files are ok', async t => {
fs.outputFileSync(path.join(t.context.dir, 'LICENSE'), '');
fs.ensureDirSync(path.join(t.context.dir, '.git'));
fs.ensureDirSync(path.join(t.context.dir, '.gitignore'));
await t.context.lib.prepareDirectory(t.context.dir);
t.true(fs.existsSync(t.context.dir));
t.true(fs.existsSync(`${t.context.dir}/package.json`));
});
test('prepare install command (yarn)', async t => {
const {cmd, args} = await t.context.lib.prepareInstallCommand();
t.is(cmd, 'yarn');
t.deepEqual(args, ['add', '--dev', '--exact']);
});
test('prepare install command (npm)', async t => {
t.context.yarnAvailable = false;
let {cmd, args} = await t.context.lib.prepareInstallCommand();
t.is(cmd, 'npm');
t.deepEqual(args, ['install', '--DE']);
({cmd, args} = await t.context.lib.prepareInstallCommand(true));
t.is(cmd, 'npm');
t.deepEqual(args, ['install', '--DE', '--verbose']);
});
test('install dependencies', async t => {
await t.context.lib.installDependencies('.', 'cmd', ['arg0'], 'dep');
t.is(t.context.lastSpawn.cmd, 'cmd');
t.deepEqual(t.context.lastSpawn.args, ['arg0', 'dep']);
t.is(t.context.lastSpawn.stdout, null);
});
test('install dependencies (verbose)', async t => {
await t.context.lib.installDependencies('.', 'cmd', ['arg0'], 'dep', true);
t.is(t.context.lastSpawn.cmd, 'cmd');
t.deepEqual(t.context.lastSpawn.args, ['arg0', 'dep']);
t.not(t.context.lastSpawn.stdout, null);
});
test('install dependencies (reject)', async t => {
t.context.resolveSpawn = false;
t.throws(t.context.lib.installDependencies('.', 'cmd', ['arg0'], 'dep'));
});
test('run all and invoke bootstrap', async t => {
await t.context.lib.run(t.context.dir, { verbose: false });
t.true(fs.existsSync(`${t.context.dir}/package.json`));
t.is(typeof t.context.lastSpawn.cmd, 'string');
t.is(t.context.bootstrapArgs[0], t.context.dir);
t.deepEqual(t.context.bootstrapArgs[1], { verbose: false });
});