-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
194 lines (177 loc) · 4.74 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict';
const assert = require('assert');
const util = require('util');
const fs = require('fs');
const access = util.promisify(fs.access);
const path = require('path');
const {
mkTestDir,
getQddTree,
getNpmTree,
exec,
runQdd,
clearNodeModules,
tree,
QDD
} = require('./test/util.js');
const test = require('pitesti')({ timeout: 60000 });
let cleanup;
let cacheTree;
test`init test dir`(async () => {
cleanup = await mkTestDir('normal');
});
test`fresh cache trees are equal`(async () => {
await exec(`rm -rf ~/.cache/qdd`);
assert.deepStrictEqual(
await getNpmTree(),
await getQddTree()
);
});
test`fresh cache trees are equal, with another cache`(async () => {
await exec(`rm -rf /tmp/testqddcache`);
assert.deepStrictEqual(
await getNpmTree(),
await getQddTree('--cache /tmp/testqddcache')
);
// we'll use this in the cache-only test
cacheTree = await tree('/tmp/testqddcache');
});
test`no cache trees are equal`(async () => {
assert.deepStrictEqual(
await getNpmTree(),
await getQddTree('--nocache')
);
});
test`primed cache trees are equal`(async () => {
assert.deepStrictEqual(
await getNpmTree(),
await getQddTree()
);
});
test`prod mode trees are equal`(async () => {
assert.deepStrictEqual(
await getNpmTree('--only=prod'),
await getQddTree('--prod')
);
});
test`exit 1 if node_modules still present`(async () => {
let err;
try {
await exec(`node ${path.join(__dirname, 'index.js')}`);
} catch (e) {
err = e;
}
assert(err);
assert.strictEqual(err.code, 1);
assert.strictEqual(
err.stderr.toString(),
'Please delete your node_modules directory before installing.\n'
);
});
test`cache-only works`(async () => {
const cacheDir = '/tmp/testqddcache';
await exec(`rm -rf ${cacheDir}`);
await clearNodeModules();
await runQdd(`--onlycache --cache ${cacheDir}`);
assert.deepEqual(cacheTree, await tree(cacheDir));
let err;
try {
await access(path.join(process.cwd(), 'node_modules'), fs.constants.F_OK);
} catch (e) {
err = e;
}
assert(err);
await exec(`rm -rf ${cacheDir}`);
});
test`debug`(() => {
const debugFile = './lib/debug.js';
const results = [];
process._rawDebug = results.push.bind(results);
{
const debug = require(debugFile);
debug(() => 'hello');
debug(() => 'world');
}
delete require.cache[require.resolve(debugFile)];
delete require.cache[require.resolve('./lib/config.js')];
process.env.QDD_DEBUG = 1;
{
const debug = require(debugFile);
debug(() => 'hola');
debug(() => 'mundo');
}
assert.deepStrictEqual(results, ['QDD: hola', 'QDD: mundo']);
delete process.env.QDD_DEBUG;
});
test`defer to npm`(async () => {
const qddOutput = await exec(`node ${QDD} show qdd`);
const npmOutput = await exec(`npm show qdd`);
assert.strictEqual(qddOutput.stdout, npmOutput.stdout);
assert.strictEqual(qddOutput.stderr, npmOutput.stderr);
});
test`config`(() => {
const absoluteConfig = require.resolve('./lib/config.js');
delete require.cache[absoluteConfig];
let config = require('./lib/config.js');
assert.deepStrictEqual(config, {
cacheDir: `${process.env.HOME}/.cache/qdd`,
debug: false,
concurrency: 10,
production: false,
noCache: false,
cacheOnly: false
});
delete require.cache[absoluteConfig];
process.argv = [
process.execPath,
__filename,
'--cache',
'cacheFoo',
'--debug',
'--concurrency',
'15',
'--prod',
'--nocache',
'--onlycache'
];
config = require('./lib/config.js');
assert.deepStrictEqual(config, {
cacheDir: 'cacheFoo',
debug: true,
concurrency: 15,
production: true,
noCache: true,
cacheOnly: true
});
delete require.cache[absoluteConfig];
process.argv = [process.execPath, __filename, '--production'];
process.env.QDD_CACHE = 'cacheFoo';
process.env.QDD_DEBUG = '1';
process.env.QDD_CONCURRENCY = '15';
process.env.QDD_NOCACHE = '1';
process.env.QDD_ONLYCACHE = '1';
config = require('./lib/config.js');
assert.deepStrictEqual(config, {
cacheDir: 'cacheFoo',
debug: true,
concurrency: 15,
production: true,
noCache: true,
cacheOnly: true
});
delete require.cache[absoluteConfig];
process.argv = [process.execPath, __filename];
process.env = { QDD_PROD: '1' };
assert.strictEqual(config.production, true);
delete require.cache[absoluteConfig];
process.env = { QDD_PRODUCTION: '1' };
assert.strictEqual(config.production, true);
delete require.cache[absoluteConfig];
process.env = { NODE_ENV: 'prod' };
assert.strictEqual(config.production, true);
delete require.cache[absoluteConfig];
process.env = { NODE_ENV: 'production' };
assert.strictEqual(config.production, true);
});
test`cleanup`(() => cleanup());
test();