/
helpers.js
247 lines (199 loc) · 6.7 KB
/
helpers.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
var spawn = require('child_process').spawn;
var path = require('path');
var fs = require('../../lib/utils/fs');
var fstream = require('fstream');
var wrench = require('wrench');
var _ = require('underscore');
var assert = require('assert');
var Meteorite = require('../../lib/meteorite');
// Mike - you need to run mocha .. --verbose
var verbose = require('optimist').argv.verbose;
// delete all data and fake out ENV vars
var prepare = function(fn) {
process.env.PATH = [path.resolve(path.join('spec', 'support', 'bin')), process.env.PATH].join(':');
process.env.HOME = [path.resolve(path.join('spec', 'support', 'home'))];
cleanup(fn);
};
var clearTestFiles = function() {
var appsPath = path.resolve(path.join('spec', 'support', 'apps'));
var apps = fs.readdirSync(appsPath);
_.each(apps, function(app) {
var appPath = path.join(appsPath, app);
var meteoritePath = path.join(appPath, '.meteor', 'meteorite');
fs.ensureDeletedSync(meteoritePath);
var smartJsonPath = path.join(appPath, 'smart.lock');
if (fs.existsSync(smartJsonPath))
fs.unlinkSync(smartJsonPath);
});
};
// delete all data from
// 1. the fake home dir
// 2. the meteorite directories and smart.lock of each app
// 3. the new_apps directory
var cleanup = function(fn) {
// 1.
var root = Meteorite.root();
if (fs.existsSync(root))
wrench.rmdirSyncRecursive(root);
// 2.
clearTestFiles();
// 3. delete and recreate
var newApps = path.resolve(path.join('spec', 'support', 'apps', 'new_apps'));
if (fs.existsSync(newApps))
wrench.rmdirSyncRecursive(newApps);
fs.mkdirSync(newApps);
fn();
};
var copyLockfileToApp = function(lockName, appName) {
var lockData = fs.readFileSync(path.join('spec', 'support', 'resources', 'smart.lock.'+lockName));
fs.writeFileSync(path.join('spec', 'support', 'apps', appName, 'smart.lock'), lockData);
};
var killProcessFamily = function(grandparentId, fn) {
var pids = [grandparentId];
var getChildPidsFromRawPidData = function(parentPid, output) {
var children = [];
_.each(output.split('\n'), function(rawPs) {
var psParts = rawPs.trim().split(/\s+/);
var currentPid = parseInt(psParts[0], 10);
var currentParentPid = parseInt(psParts[1], 10);
if (currentParentPid === parentPid) {
children.push(currentPid);
children = _.uniq(children);
}
});
return children;
};
var getChildPidsFor = function(pid, done) {
var ps = spawn('ps', ['-opid', '-oppid']);
var output = '';
ps.stdout.on('data', function(data) {
output = output + data.toString();
});
ps.on('exit', function() {
var childPids = getChildPidsFromRawPidData(pid, output);
pids.push(childPids);
pids = _.flatten(pids);
if (childPids.length > 0) {
var processed = 0;
_.each(childPids, function(childPid) {
getChildPidsFor(childPid, function() {
processed++;
if (processed >= childPids.length) {
return done();
}
});
});
} else {
done();
}
});
};
getChildPidsFor(grandparentId, function() {
_.each(pids, function(pid) {
try {
process.kill(pid);
} catch(e) {}
});
setTimeout(fn, 0);
});
};
var port = 7777;
var invoke = function(command, directory, options, fn) {
directory = path.resolve(path.join('spec', 'support', 'apps', directory));
var args = command.split(' ');
if (_.first(args) === 'run') {
args.push('--port=' + port);
port = port + 10;
}
args.push('--verbose');
args.push('--repoPort=3333');
args.push('--repoHost=localhost');
var mrt = spawn('mrt', args, { cwd: directory });
if (verbose) {
console.log();
console.log();
mrt.stderr.pipe(process.stderr);
mrt.stdout.pipe(process.stdout);
}
var searchStrings, failStrings;
if (options.waitForOutput)
searchStrings = _.isArray(options.waitForOutput) ? _.clone(options.waitForOutput) : [options.waitForOutput];
if (options.assertNoOutput)
failStrings = _.isArray(options.assertNoOutput) ? _.clone(options.assertNoOutput) : [options.assertNoOutput];
var output = '', matched = false, failed = false;
var processOutput = function(data) {
output = output + data.toString();
if (!matched && searchStrings && matchesOutput(output, searchStrings)) {
matched = true;
killProcessFamily(mrt.pid, fn);
}
if (!failed && failStrings && matchesOutput(output, failStrings)) {
failed = true;
killProcessFamily(mrt.pid, function() {
assert.ok(false, 'Output incorrectly matched' + failStrings.join(','));
});
}
};
var matchesOutput = function(output, strings) {
var i;
// reverse search to ensure no funny buggers
for (i = strings.length - 1; i >= 0; i--) {
var searchString = strings[i];
if (output.indexOf(searchString) >= 0)
strings.splice(i, 1);
}
if (strings.length === 0)
return true;
};
mrt.stdout.on('data', processOutput);
mrt.stderr.on('data', processOutput);
// if it exits of it's own accord
mrt.on('exit', function() {
if (options.waitForOutput && !matched)
assert.ok(false, 'Output never matched: ' + searchStrings.join(','))
if (!matched && !failed)
fn();
});
};
var invokeInNew = function(command, directory, options, fn) {
// copy directory into newApps
var from = path.resolve(path.join('spec', 'support', 'apps', directory));
var to = path.resolve(path.join('spec', 'support', 'apps', 'new_apps', directory));
wrench.mkdirSyncRecursive(to);
var reader = fstream.Reader(from);
var writer = fstream.Writer(to);
writer.on('close', function() {
invoke(command, path.join('new_apps', directory), options, fn);
});
reader.pipe(writer);
}
var getSystemInfo = function(fn) {
var uname = spawn('uname', []);
var unameOutput = '';
uname.stdout.on('data', function(data) {
unameOutput = unameOutput + data.toString().trim();
});
uname.on('exit', function() {
if (unameOutput === 'Darwin')
return fn('Darwin', 'x86_64');
var archOutput = '';
var arch = spawn('uname', ['-m'], function() {});
arch.stdout.on('data', function(data) {
archOutput = archOutput + data.toString().trim();
});
arch.on('exit', function() {
fn(unameOutput, archOutput);
});
});
};
var getDevBundleFileName = function(fn) {
mrt.getSystemInfo(function(uname, arch) {
fn('dev_bundle_' + uname + '_' + arch + '_0.1.5.tar.gz');
});
};
exports.prepare = prepare;
exports.cleanup = cleanup;
exports.copyLockfileToApp = copyLockfileToApp;
exports.invoke = invoke;
exports.invokeInNew = invokeInNew;
exports.getSystemInfo = getSystemInfo;