-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrunner.js
171 lines (140 loc) · 5.65 KB
/
runner.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
var fs = require('fs'),
path = require('path'),
url = require('url'),
vow = require('vow'),
util = require('util'),
Logger = require('enb/lib/logger'),
logger = new Logger(),
oldTargets = [],
NEED_COVERAGE = process.env.ISTANBUL_COVERAGE;
if (NEED_COVERAGE) {
var istanbul = require('istanbul'),
instrumenter = new (istanbul.Instrumenter)(),
covCollector = new (istanbul.Collector)(),
covIdx = 0;
}
exports.run = function (targets, root, sources) {
var isWinOS = path.sep === '\\',
exec = require('child_process').exec,
MOCHA_PHANTOM_BIN = require.resolve('.bin/mocha-phantomjs' + (isWinOS ? '.cmd' : '')),
MOCHA_PHANTOM_REPORTER = process.env.MOCHA_PHANTOM_REPORTER || 'spec',
MOCHA_PHANTOM_MAX_COUNT = parseInt(process.env.MOCHA_PHANTOM_MAX_COUNT, 10) || 10,
MOCHA_PHANTOM_HOOK = path.resolve(__dirname, './hooks/mocha-phantomjs.js'),
phantomCount = 0,
phantomQueue = [],
errorCount = 0,
toRun = [],
needRun = false;
targets = targets.filter(function (target) {
return target.indexOf('.bundles') === -1;
});
targets.forEach(function (target) {
if (oldTargets.indexOf(target) === -1) {
oldTargets.push(target);
toRun.push(target);
needRun = true;
}
});
return needRun ? vow.allResolved(toRun.map(function (nodePath) {
var nodeName = path.basename(nodePath),
target = nodeName + '.html',
targetPath = path.join(nodePath, target),
fullpath = path.join(root, nodePath, target),
args = '--reporter ' + MOCHA_PHANTOM_REPORTER,
fileurl = url.format({
protocol: 'file',
host: '/',
pathname: fullpath.replace(/\\/g, '/')
}),
deferer = vow.defer(),
covBufFile;
// On Windows OS, after the function 'url.format', the URL becomes file:////...
// But it should be file:///... (3 slahes)
fileurl = isWinOS ? fileurl.replace('/', '') : fileurl;
if (NEED_COVERAGE) {
covBufFile = path.join(root, util.format(
'__coverage-%s-%s-%s.json',
targetPath.replace(/[^A-Za-z0-9_. ]/g, '_'),
Date.now() - 0,
covIdx++));
args += ' --hooks ' + MOCHA_PHANTOM_HOOK;
args += ' --setting coverage-file=' + covBufFile;
}
phantomCount < MOCHA_PHANTOM_MAX_COUNT ?
runMochaPhantom() :
phantomQueue.push(runMochaPhantom);
function getCovData() {
var data = {},
exists = fs.existsSync(covBufFile);
if (exists) {
logger.logAction('coverage', path.relative(root, covBufFile));
data = JSON.parse(fs.readFileSync(covBufFile, 'utf8'));
dropCovBuffer();
} else {
logger.logErrorAction('coverage', path.relative(root, covBufFile));
}
return data;
}
function dropCovBuffer() {
try {
fs.unlinkSync(covBufFile);
} catch (e) {
/* ignore error */
}
}
function runMochaPhantom() {
phantomCount++;
exec([MOCHA_PHANTOM_BIN, args, fileurl].join(' '), function (err, stdout) {
--phantomCount;
phantomQueue.length && phantomQueue.shift()();
var passed = err === null;
if (passed) {
if (NEED_COVERAGE) {
covCollector.add(getCovData());
phantomCount || storeFinalCoverage();
}
logger.logAction('spec', targetPath);
deferer.resolve();
} else {
logger.logErrorAction('spec', targetPath);
++errorCount;
deferer.reject(err);
}
console.log(stdout);
});
}
function storeFinalCoverage() {
var covFile = path.resolve(root, 'coverage.json'),
store = covCollector.store,
transformer = instrumenter.instrumentSync.bind(instrumenter),
config = istanbul.config.loadFile(),
data = {};
// add all files not covered by specs with zero coverage
istanbul.matcherFor({
root: root,
includes: sources,
excludes: store.keys().concat(config.instrumentation.excludes())
}, function (err, matcher) {
matcher.files.forEach(function (file) {
// As implemented here:
// https://github.com/gotwarlost/istanbul/blob/master/lib/command/common/run-with-cover.js#L222
var filename = path.relative(root, file);
transformer(fs.readFileSync(file, 'utf-8'), filename);
Object.keys(instrumenter.coverState.s).forEach(function (key) {
instrumenter.coverState.s[key] = 0;
});
data[filename] = instrumenter.coverState;
});
covCollector.add(data);
fs.writeFileSync(covFile, JSON.stringify(covCollector.getFinalCoverage()), 'utf8');
covCollector.dispose();
});
}
return deferer.promise();
}))
.then(function () {
if (errorCount) {
return vow.reject(new Error('specs: ' + errorCount + ' failing'));
}
}) : vow.resolve([]);
};